top of page
Anchor 1
Frequently Asked C Interview Question & Answers

What does static variable mean?

A static variable is available to a C application, throughout the lifetime of the program. At the time of starting the program execution, static variables allocations take place first. In a scenario where one variable is to be used by all the functions (which is accessed by main () function), then the variable need to be declared as static in a C program.

Close

What is the difference between calloc() and malloc()?

A block of memory may be allocated using the function malloc (). The malloc function reserves a block of memory of specified size and returns a pointer of type void(). This means we can assign the base address of the block to any type of pointer

Syntax –     P = (cast type*)malloc(byte size);

Calloc() is also a memory allocation function which is generally used to allocate memory for array and structure. malloc() is used to allocate a single block of storage space, calloc() allocates multiple blocks of storage, each of the same size and initializes them with zero.

Syntax –     P = (cast type*)calloc(n,array size);

Close

Advantages of a macro over a function?

Actually, macro and function are used for different purposes. A macro replaces its expression code physically in the code at the time of preprocessing. But in case of a function, the control goes to the function while executing the code. So when the code is small then it is better to use a macro. But when code is large then the function should be used.

Close

What is page thrashing?

It happens when a high level of paging activity happen. Thrashing is caused by under allocation of the minimum number of pages required by a process, forcing it to continuously page fault. The system can detect thrashing by evaluating the level of CPU utilization as compared to the level of multiprogramming. This problem can be eliminated by reducing the level of multiprogramming.

Close

How do you override a defined macro?

You can use the #undef preprocessor directive to undefine (override) a previously defined macro. A way of an overriding macro is shown below.
#ifdef MACRO
#undef MACRO
#endif
#define MACRO X

Close

What are the different storage classes in C?

C has three types of storage classes: automatic, static and allocated.

  1. Variable having block scope and without static specifier have automatic storage duration.

  2. Variables with block scope, and with static specifier have static scope.

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

Close

Is using exit() the same as using return?

No, the exit() function is used to exit from your program and return control to the operating system. The return statement is used to return from a function and return control to the calling function. If you issue a return from the main() function, you are essentially returning control to the calling function, which is the operating system. In this case, the return statement and exit()function are similar.

Close

Differentiate call by value and call by reference?

Call by value: A process in which the values of the actual parameters sent by the calling function are copied to the formal parameters of the called function.

Call by reference: A process in which the parameters of a calling function are passed to the parameters of the called function using an address.

Close

What is the difference between constant pointer and constant variable?

CONSTANT POINTERS AND POINTER TO CONSTANT

 

CONSTANT POINTERS

  • These type of pointers are the one which cannot change the address they are pointing to.

  • This means that suppose there is a pointer which points to a variable (or stores the address of that variable).

  • Now if we try to point the pointer to some other variable (or try to make the pointer store address of some other variable), then constant pointers are incapable of this.

  • A constant pointer is declared as: 'int *const ptr' ( the location of 'const' make the pointer 'ptr' as a constant pointer)

 

POINTER TO CONSTANT

  • These type of pointers are the one which cannot change the value they are pointing to.

  • This means they cannot change the value of the variable whose address they are holding.

  • A pointer to a constant is declared as : 'const int *ptr' (the location of 'const' makes the pointer 'ptr' as a pointer to constant.)

 

EXAMPLE ON CONSTANT POINTERS

  1. #include<stdio.h>

  2. #include <conio.h>

  3. int main(){

  4. int a[] = {10,11};

  5. int* const ptr = a;

  6.  

  7. *ptr = 11;

  8.  

  9. printf("\n value at ptr is : [%d]\n",*ptr);

  10. printf("\n Address pointed by ptr : [%p]\n",(unsigned int*)ptr);

  11.  

  12. ptr++;

  13. printf("\n Address pointed by ptr : [%p]\n",(unsigned int*)ptr);

  14.  

  15. getch();

  16. }

 

Output: when we compile the above code, the compiler complains increment of read-only variable ‘ptr’.

 

EXAMPLE ON POINTER TO CONSTANTS

  1. #include<stdio.h>

  2. #include <conio.h>

  3. int main(){

  4. int a = 10;

  5. const int* ptr = &a;

  6.  

  7.  

  8. printf("\n value at ptr is : [%d]\n",*ptr);

  9. printf("\n Address pointed by ptr : [%p]\n",(unsigned int*)ptr);

  10.  

  11. *ptr = 11;

  12.  

  13. getch();

  14. }

Output: when the above code is compiled, the compiler complains assignment of read-only location ‘*ptr’

Close

What is difference between structure and union?

Structure:

  • Struct keyword is used to define a structure.

  • Members do not share memory in a structure.

  • Any member can be retrieved at any time in a structure.

  • Several members of a structure can be initialized at once.

  • Size of the structure is equal to the sum of the size of each member.

  • Altering the value of one member will not affect the value of another.

  • Stores different values for all the members.

Union:

  • Union keyword is used to define a union.

  • Members share the memory space in a union.

  • Only one member can be accessed at a time in a union.

  • Only the first member can be initialized.

  • Size of the union is equal to the size of the largest member.

  • Change in value of one member will affect other member values.

  • Stores same value for all the members.

Close
bottom of page