C Programming Interview Questions for Placements

11 May 2023

Q1. What is C language?

Answer: The C programming language is a standardized programming language developed in the early 1970s by Ken Thompson and Dennis Ritchie for use on the UNIX operating system. It has since spread to many other operating systems, and is one of the most widely used programming languages. C is prized for its efficiency, and is the most popular programming language for writing system software, though it is also used for writing applications.


Q2. What does static variable mean?

Answer: There are 3 main uses for the static.

1.    If you declare within a function: It retains the value between function calls

2.    If it is declared for a function name: By default function is extern..so it will be visible from other files if the function declaration is as static..it is invisible for the outer files

3.    Static for global variables: By default we can use the global variables from outside files If it is static global..that variable is limited to with in the file.


Q3. What are the different storage classes in C?
 
Answer: C has three types of storage: automatic, static and allocated. Variable having block scope and without static specifier have automatic storage duration.

Variables with block scope, and with static specifier have static scope. Global variables (i.e, file scope) with or without the the static specifier also have static scope. Memory obtained from calls to malloc(), alloc() or realloc() belongs to allocated storage class.


Q4. What is hashing?

Answer: To hash means to grind up, and that?s essentially what hashing is all about. The heart of a hashing algorithm is a hash function that takes your nice, neat data and grinds it into some random-looking integer.

The idea behind hashing is that some data either has no inherent ordering (such as images) or is expensive to compare (such as images). If the data has no inherent ordering, you can?t perform comparison searches.


Q5. Can static variables be declared in a header file?

Answer: You can?t declare a static variable without defining it as well (this is because the storage class modifiers static and extern are mutually exclusive). A static variable can be defined in a header file, but this would cause each source file that included the header file to have its own private copy of the variable, which is probably not what was intended.


Q6. Can a variable be both constant and volatile?

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

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.


Q7. Can include files be nested?

Answer: Yes. Include files can be nested any number of times. As long as you use precautionary measures, you can avoid including the same file twice. In the past, nesting header files was seen as bad programming practice, because it complicates the dependency tracking function of the MAKE program and thus slows down compilation. Many of today?s popular compilers make up for this difficulty by implementing a concept called precompiled headers, in which all headers and associated dependencies are stored in a precompiled state.


Q8. What is a null pointer?

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


Q9. What is the difference between printf() and sprintf() ?

Answer: sprintf() writes data to the character array whereas printf(...) writes data to the standard output device.


Q10. How to reduce a final size of executable?
 
Answer: Size of the final executable can be reduced using dynamic linking for libraries.


Q11. Can you tell me how to check whether a linked list is circular?
Answer: Create two pointers, and set both to the start of the list. Update each as follows: while (pointer1) {
pointer1 = pointer1->next;
pointer2 = pointer2->next;
if (pointer2) pointer2=pointer2->next; if (pointer1 == pointer2) {
print ("circular");
}
}

If a list is circular, at some point pointer2 will wrap around and be either at the item just before pointer1, or the item before that. Either way, its either 1 or 2 jumps until they meet.


Q12. Advantages of a macro over a function?

Answer: Macro gets to see the Compilation environment, so it can expand    TIME    FILE     #defines. It is expanded by the preprocessor.

For example, you can?t do this without macros
#define PRINT(EXPR) printf( #EXPR “=%d\n”, EXPR) PRINT( 5+6*7 ) // expands into printf(”5+6*7=%d”, 5+6*7 ); You can define your mini language with macros:
#define strequal(A,B) (!strcmp(A,B))


Q13. What is the difference between strings and character arrays?

Answer: A major difference is: string will have static storage duration, whereas as a character array will not, unless it is explicity specified by using the static keyword.

Actually, a string is a character array with following properties:
*    the multibyte character sequence, to which we generally call string, is used to initialize an array of static storage duration. The size of this array is just sufficient to contain these characters plus the terminating NUL character.
*    it not specified what happens if this array, i.e., string, is modified.
*    Two strings of same value[1] may share same memory area.


Q14. Which bit wise operator is suitable for turning off a particular bit in a number?

Answer: The bitwise AND operator, again. In the following code snippet, the bit number 24 is reset to zero.

some_int = some_int & ~KBit24;


Q15. Which bit wise operator is suitable for putting on a particular bit in a number?

Answer: The bitwise OR operator. In the following code snippet, the bit number 24 is turned ON: some_int = some_int | KBit24;


Q16. Does there exist any other function which can be used to convert an integer or a float to a string?

Answer: Some implementations provide a nonstandard function called itoa(), which converts an integer to string.
 
#include
char *itoa(int value, char *string, int radix); DESCRIPTION
The itoa() function constructs a string representation of an integer. PARAMETERS
value: Is the integer to be converted to string representation. string: Points to the buffer that is to hold resulting string.
The resulting string may be as long as seventeen bytes. radix: Is the base of the number; must be in the range 2 - 36. A portable solution exists. One can use sprintf():
char s[SOME_CONST]; int i = 10;
float f = 10.20;
sprintf ( s, “%d %f\n”, i, f );


Q17. What is the benefit of using an enum rather than a #define constant?

Answer: The use of an enumeration constant (enum) has many advantages over using the traditional symbolic constant style of #define. These advantages include a lower maintenance requirement, improved program readability, and better debugging capability.
1)    The first advantage is that enumerated constants are generated automatically by the compiler. Conversely, symbolic constants must be manually assigned values by the programmer.
2)    Another advantage of using the enumeration constant method is that your programs are more readable and thus can be understood better by others who might have to update your program later.

3)    A third advantage to using enumeration constants is that some symbolic debuggers can print the value of an enumeration constant. Conversely, most symbolic debuggers cannot print the value of a symbolic constant. This can be an enormous help in debugging your program, because if your program is stopped at a line that uses an enum, you can simply inspect that constant and instantly know its value. On the other hand, because most debuggers cannot print #define values, you would most likely have to search for that value by manually looking it up in a header file.


Q18. What is the quickest sorting method to use?

Answer: The answer depends on what you mean by quickest. For most sorting problems, it just doesn?t matter how quick the sort is because it is done infrequently or other operations take significantly more time anyway. There are three sorting methods in this author?s toolbox that are all very fast and that are useful in different situations. Those methods are quick sort, merge sort, and radix sort.


Q19. When should the volatile modifier be used?

Answer: The volatile modifier is a directive to the compiler?s optimizer that operations involving this variable should not be optimized in certain ways. There are two special cases in which use of the volatile modifier is desirable. The first case involves memory-mapped hardware (a device such as a graphics adaptor that appears to the computer?s hardware as if it were part of the computer?s memory), and the second involves shared memory (memory used by two or more programs running simultaneously).


Q20. When should the register modifier be used?

Answer: The register modifier hints to the compiler that the variable will be heavily used and should be kept in the CPU?s registers, if possible, so that it can be accessed faster.


Q21. How can you determine the size of an allocated portion of memory?

Answer: You can?t, really. free() can , but there?s no way for your program to know the trick free() uses. Even if you disassemble the library and discover the trick, there?s no guarantee the trick won?t change with the next release of the compiler.


Q22. When does the compiler not implicitly generate the address of the first element of an array?

Answer: Whenever an array name appears in an expression such as

  • array as an operand of the size of 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.


Q23.    Why n++ executes faster than n+1 ?

Answer: The expression n++ requires a single machine instruction such as INR to carry out the increment operation whereas, n+1 requires more instructions to carry out this operation.


Q24. What is the purpose of main( ) function ?

Answer: The function main( ) invokes other functions within it.It is the first function to be called when the program starts execution.

  • It is the starting function
  • It returns an int value to the environment that called the program
  • Recursive call is allowed for main( ) also.
  • It is a user-defined function
  • Program execution ends when the closing brace of the function main( ) is reached.
  • It has two arguments 1)argument count and 2) argument vector (represents strings passed).
  • Any user-defined name can also be used as parameters for main( ) instead of argc and argv

Q25. How can I search for data in a linked list?

Answer: Unfortunately, the only way to search a linked list is with a linear search, because the only way a linked list?s members can be accessed is sequentially.
Sometimes it is quicker to take the data from a linked list and store it in a different data structure so that searches can be more efficient.


Q26. Why should we assign NULL to the elements (pointer) after freeing them?

Answer: This is paranoia based on long experience. After a pointer has been freed, you can no longer use the pointed-to data. The pointer is said to dangle; it doesn?t point at anything useful.

If you NULL out or zero out a pointer immediately after freeing it, your program can no longer get in trouble by using that pointer. True, you might go indirect on the null pointer instead, but that?s something your debugger might be able to help you with immediately.

Also, there still might be copies of the pointer that refer to the memory that has been deallocated; that?s the nature of C. Zeroing out pointers after freeing them won?t solve all problems.


Q27. What is a null pointer assignment error? What are bus errors, memory faults, and core dumps?

Answer: These are all serious errors, symptoms of a wild pointer or subscript. Null pointer assignment is a message you might get when an MS-DOS program finishes executing. Some such programs can arrange for a small amount of memory to be available “where the NULL pointer points to (so to speak). If the program tries to write to that area, it will overwrite the data put there by the compiler.

When the program is done, code generated by the compiler examines that area. If that data has been changed, the compiler-generated code complains with null pointer assignment. This message carries only enough information to get you worried. There?s no way to tell, just from a null pointer assignment message, what part of your program is responsible for the error.
Some debuggers, and some compilers, can give you more help in finding the problem.

Bus error: core dumped and Memory fault: core dumped are messages you might see from a program running under UNIX. They?re more programmer friendly. Both mean that a pointer or an array subscript was wildly out of bounds. You can get these messages on a read or on a write. They aren?t restricted to null pointer problems. The core dumped part of the message is telling you about a file, called core, that has just been written in your current directory. This is a dump of everything on the stack and in the heap at the time the program was running.
With the help of a debugger, you can use the core dump to find where the bad pointer was used. That might not tell you why the pointer was bad, but it?s a step in the right direction. If you don?t have write permission in the current directory, you won?t get a core file, or the core dumped message


Q28. What is dangling pointer in c?
Answer: If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.


Q29. What are merits and demerits of array in c?
Answer: 
Merits:
(a)    We can easily access each element of array.
(b)    Not necessity to declare too many variables.
(c)    Array elements are stored in continuous memory location.

Demerits:
(a)    Wastage of memory space. We cannot change size of array at the run time.
(b)    It can store only similar type of data


Q30. Where are the auto variables stored?
 
Answer: Auto variables are stored in main memory and their default value is a garbage value.


Q31. Why Preincrement operator is faster than Postincrement?

Answer: Evaluation of any expression is from left to right. Preincrement is faster because it doesn't need to save the current value for next instruction whereas Postincrement needs to saves current value to be incremented after execution of current instruction.


Q32. Difference between arrays and linked list?

Answer: Major differences between arrays and linked lists are: (i) In array consecutive elements are stored in consecutive memory locations whereas in linked list it not so. (ii) In array address of next element is consecutive and whereas in linked list it is specified in the address part of each node.(iii) Linked List makes better use of memory than arrays.(iv) Insertion or deletion of an element in array is difficult than insertion or deletion in linked list


Q33. What is the use of typedef?
Answer: 
(i)    It increases the portability.
(ii)    It simplify the complex declaration and improve readability of the program.


Q34. What are library Functions?

Answer: Library Functions are predefined functions and stored in .lib files.


Q35. What is a structure?

Answer: Structure is a collection of heterogeneous (i.e. related data items which can be of different types) held together to a single unit. The data items enclosed within a structure are called its members which may be of data type int, float, char, array etc.


Q36. What is a pointer?

Answer: Pointer is a variable that contains address of another variable in the memory. Pointers are quite useful in creation of linked data structures (such as linked lst, trees graphs), managing object allocated memory dynamically, optimize the program to execute faster and use less memory.


Q37. What are the techniques you use for debugging?
 
Answer: (i)Using compiler?s features (ii)Read The Fine Module (iii)printf( ) debugging (iv)Code grinding (v)Assertion


Q38. What are macros? What are its advantages and disadvantages?

Answer: Macro is a Pre-processor.Major advantage of using the macro is to increase the speed of the execution of the program.
Major disadvantage of the macros are:
(i)    No type checking is performed in macro. This may cause error.
(ii)    A macro call may cause unexpected results.


Q39. What is difference between Structure and Unions?

Answer: 
(i)    In structure every member has its own memory whereas in union its members share the same member space.
(ii)    In structure, it is possible to initialize all the members at the same time which is not possible in case of union.
(iii)    A structure requires more space than union(for the same type of members).
(iv)    In union different interpretations of the same memory space are possible which is not so in case of structures.


Q40. What are the advantages of using Unions?

Answer: 
(i)    Efficient use of memory as it it does not demand memory space for its all members rather it require memory space for its largest member only.
(ii)    Same memory space can be interpreted differently for different members of the union.


Q41. What is the difference between ordinary variable and pointer in C?

Answer: An ordinary variable is like a container it can hold any value and we can change the value of ordinary variable at a time throughout the program .A pointer is a variable that stores the address of another Variable.


Q42. How are pointer variables initialized?

Answer: Pointer variable are initialized by one of the following two ways
-    Static memory allocation
-    Dynamic memory allocation


Q43. What is modular programming?

Answer: If a program is large, it is subdivided into a number of smaller programs that are called modules or subprograms. If a complex problem is solved using more modules, this approach is known as modular programming


Q44.    Where does global, static, local, register variables and C Program instructions get stored?

Answer: Global , static, local : In main memory Register variable: In registers
C program : In main memory.


Q45. Where are the auto variables stored?

Answer: Auto variables are stored in main memory and their default value is a garbage value.


Q46. What is an lvalue?

Answer: An lvalue is an expression to which a value can be assigned. The lvalue expression is located on the left side of an assignment statement, whereas an rvalue is located on the right side of an assignment statement. Each assignment statement must have an lvalue and an rvalue. The lvalue expression must reference a storable variable in memory. It cannot be a constant


Q47. What is an argument? Differentiate between formal arguments and actual arguments?

Answer: An argument is an entity used to pass the data from calling function to the called function. Formal arguments are the arguments available in the function definition. They are preceded by their own data types.
Actual arguments are available in the function call.


Q48. When is a switch statement better than multiple if statements?

Answer: A switch statement is generally best to use when you have more than two conditional expressions based on a single variable of numeric type.


Q49. Differentiate between a linker and linkage?

Answer: A linker converts an object code into an executable code by linking together the necessary build in functions. The form and place of declaration where the variable is declared in a program determine the
linkage of variable.


Q50. Define Operator, Operand, and Expression in 'C'?

Answer: Operators are symbols which take one or more operands or expressions and perform arithmetic or logical computations.
Operands are variables or expressions which are used in operators to evaluate the expression. Combination of operands and operators form an expression.



FAQ

Any Questions?
Look Here.

Related Articles

C++ Interview Questions for Placements

Data structures and Algorithm Concepts

Java Interview Questions for placement

Object Oriented Programming Concepts

Operating Systems Concepts

Python Interview Questions

SQL Interview Questions for Placements

Top 23 DSA Interview Questions for Placement