• +91 9971497814
  • info@interviewmaterial.com

C Interview Questions Answers

Question 1 : How to Copy constructor ?

Answer 1 : A copy constructor is used to create an instanc... A copy constructor is used to create an instance from another instance, e.g.: MyClass A; MyClass B (A); When B is instantiated, the following constructor is called: B (B const & object); Use the body of the copy constructor to copy member data from the source object. e.g.: class Ints { int x, y; Ints (Ints const & object) { x = object.x; y = object.y; } };

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

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

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

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

Answer 5 : 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 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 : Can you add pointers together? Why would you?

Answer 7 : 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 8 : Can math operations be performed on a void pointer?

Answer 8 : 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 9 : When does the compiler not implicitly generate the address of the first element of an array?

Answer 9 : 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 10 : What is the difference between NULL and NUL?

Answer 10 : 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 ‘’.

C Contributors

krishan

Share your email for latest updates

Name:
Email:

Our partners