• +91 9971497814
  • info@interviewmaterial.com

C Interview Questions Answers

Question 1 : What is a null pointer?

Answer 1 : There are times when it’s necessary to have a pointer that doesn’t point to anything. The macro NULL, defined in , has a value that’s guaranteed to be different from any valid pointer. NULL is a literal zero, possibly cast to void* or char*. Some people, notably C++ programmers, prefer to use 0 rather than NULL. The null pointer is used in three ways: 1) To stop indirection in a recursive data structure 2) As an error value 3) As a sentinel value

Question 2 : What is the difference between NULL and NUL?

Answer 2 : NULL is a macro defined in for the null pointer. NUL is the name of the first character in the ASCII character set. It corresponds to a zero value. There’s no standard macro NUL in C, but some people like to define it. The digit 0 corresponds to a value of 80, decimal. Don’t confuse the digit 0 with the value of ‘’ (NUL)! NULL can be defined as ((void*)0), NUL as ‘’.

Question 3 : When does the compiler not implicitly generate the address of the first element of an array?

Answer 3 : Whenever an array name appears in an expression such as - array as an operand of the sizeof operator - array as an operand of & operator - array as a string literal initializer for a character array Then the compiler does not implicitly generate the address of the address of the first element of an array.

Question 4 : Can math operations be performed on a void pointer?

Answer 4 : No. Pointer addition and subtraction are based on advancing the pointer by a number of elements. By definition, if you have a void pointer, you don’t know what it’s pointing to, so you don’t know the size of what it’s pointing to. If you want pointer arithmetic to work on raw addresses, use character pointers.

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

Answer 5 : 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.

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

Answer 6 : 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 7 : What is storage class and what are storage variable ?

Answer 7 : 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 8 : In C, what is the difference between a static variable and global variable?

Answer 8 : 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 9 : Can a variable be both const and volatile?

Answer 9 : 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 10 : How can you check to see whether a symbol is defined?

Answer 10 : 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

C Contributors

krishan

Share your email for latest updates

Name:
Email:

Our partners