22-01-2013, 03:32 PM
C Interview Questions
![Microsoft Word Document .doc](https://seminarproject.net/images/attachtypes/doc.gif)
1: How can you print a literal % with printf?
A: %%
1.2: Why doesn't \% print a literal % with printf?
A: Backslash sequences are interpreted by the compiler (\n, \", \0, etc.), and \% is not one of the recognized backslash sequences. It's not clear what the compiler would do with a \% sequence -- it might delete it, or replace it with a single %, or perhaps pass it through as \ %. But it's print’s behavior we're trying to change, and printf's special character is %. So it's a %-sequence we Should be looking for to print a literal %, and printf defines the one we want as %%.
1.3: Are the parentheses in a return statement mandatory?
A: No. The formal syntax of a return statement is return expression ;
But it's legal to put parentheses around any expression, of course, whether they're needed or not.
1.4: How can %f work for type double in printf if %lf is
required in scanf?
A: In variable-length argument lists such as printf's, the old "default argument promotions" apply, and type float is implicitly converted to double. So printf always receives doubles, and defines %f to be the sequence that works whether you had passed a float or a double. (Strictly speaking, %lf is *not* a valid printf format specifier, although most versions of printf quietly excepts it.)
Scanf, on the other hand, always accepts pointers, and the type’s pointer-to-float and pointer-to-double are very different (especially when you're using them for storing values). No implicit promotions apply.
1.5: If a machine uses some nonzero internal bit pattern for
null pointers, how should the NULL macro be defined?
A: As 0 (or (char *) 0), as usual. The *compiler* is responsible for translating null pointer constants into internal null pointer representations, not the preprocessor.
1.6: If p is a pointer, is the test if (p) Valid? What if a machine uses some nonzero internal bit Pattern for null pointers?
A: The test is always valid. Since the definition of "true" in C is "not equal to 0," the test is equivalent to
If (p! = 0)
And the compiler then translates the 0 into the appropriate internal representation of a null pointer.
1.7: What is the ANSI Standard definition of a null pointer
constant?
A: "An integral constant expression with the value 0, or such an expression cast to type (void *)".
1.8: What does the auto keyword mean? When is it needed?
A: auto is a storage-class specifier, just like extern and static. But since automatic duration is the default for local variables (and meaningless, in fact illegal, for global variables), the keyword is never needed. (It's a relic from the dawn of C.)
1.9: What does *p++ increment?
A: The pointer p. To increment what p points to, use (*p)++ or ++*p.
1.10: What's the value of the expression 5["abcdef"] ?
A: 'f'.
(The string literal "abcdef" is an array, and the expression is equivalent to "abcdef"[5]. Why is the
inside-out expression equivalent? Because a is equivalent to *(a + b) which is equivalent to *(b + a) which is equivalent to b[a].
[b]1.11: [POOR QUESTION] How can you swap two integer variables
without using a temporary?
A: The reason that this question is poor is that the answer ceased to be interesting when we came down out of the trees and stopped using assembly language.