• +91 9723535972
  • info@interviewmaterial.com

C Interview Questions and Answers

C Interview Questions and Answers

Question - 21 : - What will be printed as the result of the operation below: main() { int x=10, y=15; x = x++; y = ++y; printf(“%d %d\n”,x,y); }

Answer - 21 : - 11, 16

Question - 22 : - What will be printed as the result of the operation below: main() { int a=0; if(a==0) printf(“Tech Preparation\n”); printf(“Tech Preparation\n”); }

Answer - 22 : - Two lines with “Tech Preparation” will be printed.

Question - 23 : - What will the following piece of code do int f(unsigned int x) { int i; for (i=0; x!0; x>>=1){ if (x & 0X1) i++; } return i; }

Answer - 23 : - returns the number of ones in the input parameter X

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

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

Question - 25 : - What does static variable mean?

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

Question - 26 : - Advantages of a macro over a function?

Answer - 26 : - 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)) Macros are a necessary evils of life. The purists don’t like them, but without it no real work gets done.

Question - 27 : - What are the differences between malloc() and calloc()?

Answer - 27 : - There are 2 differences. First, is in the number of arguments. malloc() takes a single argument(memory required in bytes), while calloc() needs 2 arguments(number of variables to allocate memory, size in bytes of a single variable). Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.

Question - 28 : - What are the different storage classes in C?

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

Question - 29 : - What is the difference between strings and character arrays?

Answer - 29 : - 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. For example, in the following declarations: char *s1 = “Calvin and Hobbes”; char *s2 = “Calvin and Hobbes”; the strings pointed by s1 and s2 may reside in the same memory location. But, it is not true for the following: char ca1[] = “Calvin and Hobbes”; char ca2[] = “Calvin and Hobbes”; [1] The value of a string is the sequence of the values of the contained characters, in order.

Question - 30 : - Write down the equivalent pointer expression for referring the same element a[i][j][k][l]?

Answer - 30 : - a[i] == *(a+i) a[i][j] == *(*(a+i)+j) a[i][j][k] == *(*(*(a+i)+j)+k) a[i][j][k][l] == *(*(*(*(a+i)+j)+k)+l)


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners