• +91 9971497814
  • info@interviewmaterial.com

C Interview Questions Answers

Question 1 :  I had the definition char a[6] in one source file, and in another I declared extern char *a. Why didn't it work?

Answer 1 : In one source file you defined an array of characters and in the other you declared a pointer to characters. The declaration extern char *a does not declare an array and therefore does not match the actual definition. The type pointer-to-type-T is not the same as array-of-type-T. Use extern char a[].

Question 2 : Why can't I do something like this? extern char *getpass(); char str[10]; str = getpass("Enter password: ");

Answer 2 : Arrays are ``second-class citizens'' in C; one upshot of this prejudice is that you cannot assign to them . When you need to copy the contents of one array to another, you must do so explicitly. In the case of char arrays, the strcpy routine is usually appropriate: strcpy(str, getpass("Enter password: "));   (When you want to pass arrays around without copying them, you can use pointers and simple assignment)

Question 3 : what about this? Isn't this an array assignment? char a[] = "Hello, world!\n";

Answer 3 : No, that's an initialization. You are allowed to initialize arrays when you define them.

Question 4 : what is meant by the ``equivalence of pointers and arrays'' in C?

Answer 4 : Much of the confusion surrounding arrays and pointers in C can be traced to a misunderstanding of this statement. Saying that arrays and pointers are ``equivalent'' means neither that they are identical nor even interchangeable. What it means is that array and pointer arithmetic is defined such that a pointer can be conveniently used to access an array or to simulate an array. In other words, as Wayne Throop has put it, it's ``pointer arithmetic and array indexing [that] are equivalent in C, pointers and arrays are different.'') Specifically, the cornerstone of the equivalence is this key definition: A reference to an object of type array-of-T which appears in an expression decays (with three exceptions) into a pointer to its first element; the type of the resultant pointer is pointer-to-T.   That is, whenever an array appears in an expression, the compiler implicitly generates a pointer to the array's first element, just as if the programmer had written &a[0]. (The exceptions are when the array is the operand of a sizeof or & operator, or is a string literal initializer for a character array. As a consequence of this definition, and in spite of the fact that the underlying arrays and pointers are quite different, the compiler doesn't apply the array subscripting operator [] that differently to arrays and pointers, after all. Given an array a and pointer p, an expression of the form a[i] causes the array to decay into a pointer, following the rule above, and then to be subscripted just as would be a pointer variable in the expression p[i] .If you were to assign the array's address to the pointer: p = a; then p[3] and a[3] would access the same element.

Question 5 : How can you check to see whether a symbol is defined?

Answer 5 : You can use the #ifdef and #ifndef preprocessor directives to check whether a symbol has been defined (#ifdef) or whether it has not been defined (#ifndef). Can you define which header file to include at compile time? Yes. This can be done by using the #if, #else, and #endif preprocessor directives. For example, certain compilers use different names for header files. One such case is between Borland C++, which uses the header file alloc.h, and Microsoft C++, which uses the header file malloc.h. Both of these headers serve the same purpose, and each contains roughly the same definitions. If, however, you are writing a program that is to support Borland C++ and Microsoft C++, you must define which header to include at compile time. The following example shows how this can be done: #ifdef _ _BORLANDC_ _ #include #else #include #endif

Question 6 : Can a variable be both const and volatile?

Answer 6 : Yes. The const modifier means that this code cannot change the value of the variable, but that does not mean that the value cannot be changed by means outside this code. For instance, in the example in FAQ 8, the timer structure was accessed through a volatile const pointer. The function itself did not change the value of the timer, so it was declared const. However, the value was changed by hardware on the computer, so it was declared volatile. If a variable is both const and volatile, the two modifiers can appear in either order.

Question 7 : In C, what is the difference between a static variable and global variable?

Answer 7 : A static variable declared outside of any function is accessible only to all the functions defined in the same file (as the static variable). However, a global variable can be accessed by any function (including the ones from different files).

Question 8 : What is storage class and what are storage variable ?

Answer 8 : A storage class is an attribute that changes the behavior of a variable. It controls the lifetime, scope and linkage. There are five types of storage classes 1) auto 2) static 3) extern 4) register 5) typedef

Question 9 : What is the difference between declaring a variable and defining a variable?

Answer 9 : Declaring a variable means describing its type to the compiler but not allocating any space for it. Defining a variable means declaring it and also allocating space to hold the variable. You can also initialize a variable at the time it is defined.

Question 10 : Can you add pointers together? Why would you?

Answer 10 : No, you can’t add pointers together. If you live at 1332 Lakeview Drive, and your neighbor lives at 1364 Lakeview, what’s 1332+1364? It’s a number, but it doesn’t mean anything. If you try to perform this type of calculation with pointers in a C program, your compiler will complain. The only time the addition of pointers might come up is if you try to add a pointer and the difference of two pointers.

C Contributors

krishan

Share your email for latest updates

Name:
Email:

Our partners