This might be one of the most debated C interview questions; the answer to this question varies book by book, and site by site on the internet. Here, I would like to make it clear there are only two storage classes in C, and the rest are storage class specifiers.
As per reference manual of “The C Programming Language” by: Brian W. Kernighan and Dennis M. Ritchie, in the Appendix A of the reference manual, the very first line says: There are two storage classes: automatic and static.
[The C programing Language 2nd Edition,Brian W. Kernighan ,Dennis M. Ritchie ]
[The C programing Language 2nd Edition,Brian W. Kernighan ,Dennis M. Ritchie ]
Q2. What are the different storage class specifiers in C?
There are 4 storage class specifiers in C, out of which auto and static act as storage classes as well. So, the storage class specifiers are:
- Auto
- Register
- Static
- Extern
Q3. What are library functions in C?
Library functions are the predefined functions in C, stored in .lib files.
Q4. Where are the auto variables stored?
Auto variables are stored in the main memory. The default value of auto variables is garbage value.
Q5. What is the difference between i++ and ++i?
One of the most asked C interview questions or viva questions – i++ and ++i. The expression i++ returns the old value and then increases i by 1, whereas the expression ++i first increases the value of i by 1 and then returns the new value.
Q6. What is l-value in C? Mention its types.
Location value, commonly known as the l-value, refers to an expression used on the left side of an assignment operator. For example: in the expression “x = 5″, x is the l-value and 5 is the r-value.
There are two types of l-value in C – modifiable l-value and non-modifiable l-value. modifiable l-value denoted a l-value which can be modified. non-modifiable l-value denote a l-value which cannot be modified. const variables are non-modifiable l-value.
Q7. Can i++ and ++i be used as l-value in C?
In C both i++ and ++i cannot be used as l-value. Whereas in C++, ++i can be used as l-value, but i++ cannot be.
Q8. Which of the following shows the correct hierarchy of arithmetic operations in C?
(1) / + * –
(2) * – / +
(3) + – / *
(4) * / + -
(2) * – / +
(3) + – / *
(4) * / + -
4 is the correct answer.
Q9. Which bit wise operator is suitable for
- checking whether a particular bit is on or off?
Ans. The bitwise AND operator. - turning off a particular bit in a number?
Ans. The bitwise AND operator. - putting on a particular bit in a number?
Ans. The bitwise OR operator.
Q10. Can a C program be written without using the main function?
This is one of the most interesting C interview questions. I guess, up until now, you might not have ever written a C program without using the main function. But, a program can be executed without the main function. See the example below:
Now, lets see what’s happening within the source code. Here, #define acts as the main function to some extent. We are basically using #define as a preprocessor directive to give an impression that the source code executes without the main function.
Q11. What is the output of printf(“%d”); ?
For printf(“%d”, a); the compiler will print the corresponding value of a. But in this case, there is nothing after %d, so the compiler will show garbage value in output screen.
Q12. What is the difference between printf() and sprintf() ?
printf() statement writes data to the standard output device, whereas sprintf() writes data to the character array.
Q13. What is the difference between %d and %*d ?
Here, %d gives the original value of the variable, whereas %*d gives the address of the variable due to the use of pointer.
Q14. Which function – gets() or fgets(), is safe to use, and why?
Neither gets() nor fgets() is completely safe to use. When compared, fgets() is safe to use than gets() because with fgest() a maximum input length can be specified.
Q15. Write a C program to print “Programming is Fun” without using semicolon (;) .
Here’s a typical source code to print “Programming is Fun”.
There’s not much trick in printing the line without using the semicolon. Simply use the printf statement inside the if condition as shown below.
Q16. What is the difference between pass by value and pass by reference?
This is another very important C interview questions. I will try to explain this in detail with source code and output.
Pass by Value:
This is the process of calling a function in which actual value of arguments are passed to call a function. Here, the values of actual arguments are copied to formal arguments, and as a result, the values of arguments in the calling function are unchanged even though they are changed in the called function. So, passing by value to function is restricted to one way transfer of information. The following example illustrates the mechanism of passing by value to function.
In this example, the values of x and y have been passed into the function and they have been swapped in the called function without any change in the calling function.
Pass by Reference:
In pass by reference, a function is called by passing the address of arguments instead of passing the actual value. In order to pass the address of an argument, it must be defined as a pointer. The following example illustrates the use of pass by reference.
In this example, addresses of x and y have been passed into the function, and their values are swapped in the called function. As a result of this, the values are swapped in calling function also.
Q17. Write a C program to swap two variables without using a third variable.
This is one of the very common C interview questions. It can be solved in a total of five steps. For this, I have considered two variables as a and b, such that a = 5 and b = 10.
When two or more than two conditional expressions are based on a single variable of numeric type, a switch statement is generally preferred over multiple if statements.
Q19. Write a C program to print numbers from 1 to n without using loop.
Q20. What is the difference between declaration and definition of a function?
Declaration of a function in the source code simply indicates that the function is present somewhere in the program, but memory is not allocated for it. Declaration of a function helps the program understand the following:
- what are the arguments to that function
- their data types
- the order of arguments in that function
- the return type of the function
Definition of a function, on the other hand, acts like a super set of declaration. It not only takes up the role of declaration, but also allocates memory for that function.
Q21. What are static functions? What is their use in C?
In the C programming language, all functions are global by default. Therefore, to make a function static, the “static” keyword is used before the function. Unlike the global functions, access to static functions in C is restricted to the file where they are declared.
The use of static functions in C are:
- to make a function static
- to restrict access to functions
- to reuse the same function name in other file
Q22. Mention the advantages and disadvantages of arrays in C.
Advantages:
- Each element of array can be easily accessed.
- Array elements are stored in continuous memory location.
- With the use of arrays, too many variables need not be declared.
- Arrays have a wide application in data structure.
Disadvantages:
- Arrays store only similar type of data.
- Arrays occupy and waste memory space.
- The size of arrays cannot be changed at the run time.
Q23. What are array pointers in C?
Array whose content is the address of another variable is known as array pointer. Here’s an example illustrating array pointers in C.
Q24. How will you differentiate char const* p and const char* p ?
In char const* p, the pointer ‘p’ is constant, but not the character referenced by it. Here, you cannot make ‘p’ refer to any other location, but you can change the value of the char pointed by ‘p’.
In const char* p, the character pointed by ‘p’ is constant. Here, unlike the upper case, you cannot change the value of character pointed by ‘p’, but you can make ‘p’ refer to any other location.
Q25. What is the size of void pointer in C?
Whether it’s char pointer, function pointer, null pointer, double pointer, void pointer or any other, the size of any type of pointer in C is of two byte. In C, the size of any pointer type is independent of data type.
Q26. What is the difference between dangling pointer and wild pointer?
Both these pointers don’t point to a particular valid location.
A pointer is called dangling if it was pointing to a valid location earlier, but now the location is invalid. This happens when a pointer is pointing at the memory address of a variable, but afterwards some variable has been deleted from that particular memory location, while the pointer is still pointing at that memory location. Such problems are commonly called dangling pointer problem and the output is garbage value.
Wild pointer too doesn’t point to any particular memory location. A pointer is called wild if it is not initialized at all. The output in this case is any address.
Q27. What is the difference between wild pointer and null pointer?
Wild pointer is such a pointer which doesn’t point to any specific memory location as it is not initialized at all. Whereas, null pointer is the one that points the base address of segment. Literally, null pointers point at nothing.
Q28. What is far pointer in C?
Far pointer is the pointer that can access all the 16 segments, i.e. the whole residence memory of RAM. The size of far pointer is 4 byte. The example below illustrates the size of far pointer in C.
FILE is simply a predefined data type defined in stdio.h file.
Q30. What are the differences between Structure and Unions?
- Every member in structure has its own memory, whereas the members in a union share the same member space.
- Initialization of all the member at the same time is possible in structures but not in unions.
- For the same type of member, a structure requires more space than a union.
- Different interpretations of the same memory space is possible in union but not in structures.
Q31. Write a C program to find the size of structure without using sizeof operator?
Q32. What is the difference between .com program and .exe program?
Both these programs are executable programs, and all drivers are .com programs.
- .com program executes faster than .exe program.
- .com file has higher preference than .exe type
No comments:
Post a Comment