C Programming Interview Questions and Answers
C Programming is called a high level, compiler language. The aim of any high level computer language is to provide an easy and natural way of giving a programm of instructions to a computer (a computer program). The language of the raw computer is a stream of numbers called machine code. We have listed of C Programming Interview Questions and Answers that have been designed for C Programming professionals who are preparing interviews on C Language.
Top C Programming Interview Questions and Answers
Here are a few C Programming Interview Questions that will help you crack an interview for a dream Job
What is C language?
The C programming language is a standardized programming language developed in the early 1970s by Ken Thompson and Dennis Ritchie for use on the UNIX operating system. It has since spread to many other operating systems, and is one of the most widely used programming languages.
What is hashing in C?
Hashing is the process of mapping strings to integers, usually in a relatively small range. A ”hash function” maps a string (or some other data structure) to a bounded number (the “hash bucket”) which can more easily be used as an index in an array, or for performing repeated comparisons. (Obviously, a mapping from a potentially huge set of strings to a small set of integers will not be unique. Any algorithm using hashing therefore has to deal with the possibility of —collisions.”)
How can I call FORTRAN?
How can I call FORTRAN (C++, BASIC, Pascal, Ada, LISP) functions from C? (And vice versa?)
The answer is entirely dependent on the machine and the specific calling sequences of the various compilers in use, and may not be possible at all. Read your compiler documentation very carefully; sometimes there is a “mixed-language programming guide,” although the techniques for passing arguments and ensuring correct run-time startup are often arcane. Besides arranging calling sequences correctly, you may also have to conspire between the various languages to get aggregate data structures declared compatibly.
In C++, a “C” modifier in an external function declaration indicates that the function is to be called using C calling conventions. In Ada, you can use the Export and Convention pragmas, and types from the package Interfaces.C, to arrange for C-compatible calls, parameters, and data structures.
What is assert and when would I use it?
It is a macro, defined in <assert.h>, for testing —assertions”. An assertion essentially documents an assumption being made by the programmer, an assumption which, if violated, would indicate a serious programming error. For example, a function which was supposed to be called with a non-null pointer could write assert(p != NULL);
Why doesnt C have nested functions?
It’s not trivial to implement nested functions such that they have the proper access to local variables in the containing function(s), so they were deliberately left out of C as a simplification. (gcc does allow them, as an extension.) For many potential uses of nested functions (e.g. qsort comparison functions), an adequate if slightly cumbersome solution is to use an adjacent function with static declaration, communicating if necessary via a few static variables. (A cleaner solution, though unsupported by qsort, is to pass around a pointer to a structure containing the necessary context.)
Does C have an equivalent to Pascals with statement?
No. The way in C to get quick and easy access to the fields of a structure is to declare a little local structure pointer variable (which, it must be admitted, is not quite as notationally convenient as a with statement and doesn’t save quite as many keystrokes, though it is probably safer).
Is C a great language, or what?
Is C a great language, or what? Where else could you write something like a+++++b ?
Well, you can’t meaningfully write it in C, either. The rule for lexical analysis is that at each point during a straightforward left-to-right scan, the longest possible token is determined, without regard to whether the resulting sequence of tokens makes sense. The fragment in the question is therefore interpreted as a ++ ++ + b and cannot be parsed as a valid expression.
Does C have circular shift operators?
No. (Part of the reason why is that the sizes of C’s types aren’t precisely defined—-but a circular shift makes most sense when applied to a word of a particular known size.)
You can implement a circular shift using two regular shifts and a bitwise OR:
(x << 13) (x > >3) /* circular shift left 13 in 16 bits */
There seem to be a few missing operators …
There seem to be a few missing operators, like AA &&=, and ->=.
A logical exclusive-or operator (hypothetically “AA) would be nice, but it couldn’t possibly have short-circuiting behavior analogous to && and I Similarly, it’s not clear how short-circuiting would apply to hypothetical assignment operators &&= and =. (It’s also not clear how often &&= and 1= would actually be needed.)
Why dont C comments nest?
Why don’t C comments nest? How am I supposed to comment out code containing comments? Are comments legal inside quoted strings?
C comments don’t nest mostly because PL/I’s comments, which C’s are borrowed from, don’t either. Therefore, it is usually better to —comment out” large sections of code, which might contain comments, with #ifdef or #if 0 ).
The character sequences /* and */ are not special within double-quoted strings, and do not therefore introduce comments, because a program (particularly one which is generating C code as output) might want to print them. (It is hard to imagine why anyone would want or need to place a comment inside a quoted string. It is easy to imagine a program needing to print “/*”.)
Are the outer parentheses in return statements really optional?
Yes
Long ago, in the early days of C, they were required, and just enough people learned C then, and wrote code which is still in circulation, that the notion that they might still be required is widespread.
(As it happens, parentheses are optional with the sizeof operator, too, under certain circumstances.)
Is there a way to have non-constant case labels (i.e. ranges or arbitrary expressions)?
No. The switch statement was originally designed to be quite simple for the compiler to translate, therefore case labels are limited to single, constant, integral expressions. You can attach several case labels to the same statement, which will let you cover a small range if you don’t mind listing all cases explicitly.
Which is more efficient, a switch statement or an if else chain?
The differences, if any, are likely to be slight. The switch statement was designed to be efficiently implementable, though the compiler may choose to use the equivalent of an if/else chain (as opposed to a compact jump table) if the case labels are sparsely distributed.
Do use switch when you can: it’s certainly cleaner, and perhaps more efficient (and certainly should never be any less efficient).
How can I swap two values without using a temporary?
The standard hoary old assembly language programmer’s trick is:
a A= b; b A= a; a A= b;
But this sort of code has little place in modern, HLL programming. Temporary variables are essentially free, and the idiomatic code using three assignments, namely
int t = a; a = b; b = t;
is not only clearer to the human reader, it is more likely to be recognized by the compiler and turned into the most-efficient code (e.g. perhaps even using an EXCH instruction). The latter code is obviously also amenable to use with pointers and floating-point values, unlike the XOR trick.
People claim that optimizing compilers are good and that we no longer have to write things in assembler for speed
People claim that optimizing compilers are good and that we no longer have to write things in assembler for speed, but my compiler can’t even replace i/=2 with a shift.
Was i signed or unsigned? If it was signed, a shift is not equivalent (hint: think about the result if i is negative and odd), so the compiler was correct not to use it.
I have been replacing multiplications and divisions with shift operators, because shifting is more efficient.
This is an excellent example of a potentially risky and usually unnecessary optimization. Any compiler worthy of the same can replace a constant, power-of-two multiplication with a left shift, or a similar division of an unsigned quantity with a right shift. Furthermore, a compiler will make these optimizations only when they’re correct; many programmers overlook the fact that shifting a negative value to the right is not equivalent to division. (Therefore, when you need to make sure that these optimizations are performed, you may have to declare relevant variables as unsigned.)
Are pointers really faster than arrays?
Are pointers really faster than arrays? How much do function calls slow things down? Is ++i faster than i = i + 1?
Precise answers to these and many similar questions depend of course on the processor and compiler in use. If you simply must know, you’ll have to time test programs carefully. (Often the differences are so slight that hundreds of thousands of iterations are required even to see them. For conventional machines, it is usually faster to march through large arrays with pointers rather than array subscripts, but for some processors the reverse is true. (Better compilers should generate good code regardless of which notation you use, though it’s arguably easier for a compiler to convert array indices to pointers than vice versa .)
Function calls, though obviously incrementally slower than in-line code, contribute so much to modularity and code clarity that there is rarely good reason to avoid them. (Actually, by reducing bulk, functions can improve performance.) Also, some compilers are able to expand small, critical-path functions in-line, either as an optimization or at the programmer’s request.
Before rearranging expressions such as i = i + 1, remember that you are dealing with a compiler, not a keystroke-programmable calculator. Any decent compiler will generate identical code for ++i, i += 1, and i = i + 1. The reasons for using ++i or i += 1 over i = i + 1 have to do with style, not efficiency.
What is the best way of making my program efficient?
By picking good algorithms, implementing them carefully, and making sure that your program isn’t doing any extra work. For example, the most microoptimized character-copying loop in the world will be beat by code which avoids having to copy characters at all.
When worrying about efficiency, it’s important to keep several things in perspective. First of all, although efficiency is an enormously popular topic, it is not always as important as people tend to think it is. Most of the code in most programs is not time-critical. When code is not time-critical, it is usually more important that it be written clearly and portably than that it be written maximally efficiently. (Remember that computers are very, very fast, and that seemingly —inefficient” code may be quite efficiently compilable, and run without apparent delay.)
It is notoriously difficult to predict what the —hot spots” in a program will be. When efficiency is a concern, it is important to use profiling software to determine which parts of the program deserve attention. Often, actual computation time is swamped by peripheral tasks such as I/O and memory allocation, which can be sped up by using buffering and caching techniques.
There are lot of opportunities from many reputed companies in the world. You still have opportunity to learn and move ahead in your career in C Programming. We have listed some of C Programming Interview Questions 2018 that helps you in cracking your interview & acquire dream career as C Programming.