• +91 9971497814
  • info@interviewmaterial.com

C Interview Questions Answers

Question 1 : What are the advantages of the functions?

Answer 1 : - Debugging is easier - It is easier to understand the logic involved in the program - Testing is easier - Recursive call is possible - Irrelevant details in the user point of view are hidden in functions - Functions are helpful in generalizing the program

Question 2 : Difference between arrays and pointers?

Answer 2 : - Pointers are used to manipulate data using the address. Pointers use * operator to access the data pointed to by them - Arrays use subscripted variables to access and manipulate data. Array variables can be equivalently written using pointer expression.

Question 3 : Is it better to use malloc() or calloc()?

Answer 3 : Both the malloc() and the calloc() functions are used to allocate dynamic memory. Each operates slightly different from the other. malloc() takes a size and returns a pointer to a chunk of memory at least that big: void *malloc( size_t size ); calloc() takes a number of elements, and the size of each, and returns a pointer to a chunk of memory at least big enough to hold them all: void *calloc( size_t numElements, size_t sizeOfElement ); There’s one major difference and one minor difference between the two functions. The major difference is that malloc() doesn’t initialize the allocated memory. The first time malloc() gives you a particular chunk of memory, the memory might be full of zeros. If memory has been allocated, freed, and reallocated, it probably has whatever junk was left in it. That means, unfortunately, that a program might run in simple cases (when memory is never reallocated) but break when used harder (and when memory is reused). calloc() fills the allocated memory with all zero bits. That means that anything there you’re going to use as a char or an int of any length, signed or unsigned, is guaranteed to be zero. Anything you’re going to use as a pointer is set to all zero bits. That’s usually a null pointer, but it’s not guaranteed.Anything you’re going to use as a float or double is set to all zero bits; that’s a floating-point zero on some types of machines, but not on all. The minor difference between the two is that calloc() returns an array of objects; malloc() returns one object. Some people use calloc() to make clear that they want an array.

Question 4 : "union" Data Type What is the output of the following program? Why?

Answer 4 : #include main() { typedef union { int a; char b[10]; float c; } Union; Union x,y = {100}; x.a = 50; strcpy(x.b,"hello"); x.c = 21.50; printf("Union x : %d %s %f n",x.a,x.b,x.c); printf("Union y : %d %s %f n",y.a,y.b,y.c); }

Question 5 : What are x, y, y, u #define Atype int* typedef int *p; p x, z; Atype y, u;

Answer 5 :  x and z are pointers to int. y is a pointer to int but u is just an integer variable

Question 6 : what is the diff between "new" and "operator new" ?

Answer 6 : "operator new" works like malloc.

Question 7 : How do you print only part of a string?

Answer 7 : /* Use printf() to print the first 11 characters of source_str. */ printf(First 11 characters: ‘%11.11s’n, source_str);

Question 8 : What are the characteristics of arrays in C?

Answer 8 : 1) An array holds elements that have the same data type 2) Array elements are stored in subsequent memory locations 3) Two-dimensional array elements are stored row by row in subsequent memory locations. 4) Array name represents the address of the starting element 5) Array size should be mentioned in the declaration. Array size must be a constant expression and not a variable.

Question 9 : What are the advantages of auto variables?

Answer 9 : 1)The same auto variable name can be used in different blocks 2)There is no side effect by changing the values in the blocks 3)The memory is economically used 4)Auto variables have inherent protection because of local scope

Question 10 : malloc() Function- What is the difference between "calloc(...)" and "malloc(...)"?

Answer 10 : 1. calloc(...) allocates a block of memory for an array of elements of a certain size. By default the block is initialized to 0. The total number of memory allocated will be (number_of_elements * size). malloc(...) takes in only a single argument which is the memory required in bytes. malloc(...) allocated bytes of memory and not blocks of memory like calloc(...). 2. malloc(...) allocates memory blocks and returns a void pointer to the allocated space, or NULL if there is insufficient memory available. calloc(...) allocates an array in memory with elements initialized to 0 and returns a pointer to the allocated space. calloc(...) calls malloc(...) in order to use the C++ _set_new_mode function to set the new handler mode.

C Contributors


Share your email for latest updates


Our partners