You are on page 1of 17

C interview questions 1. A frequent reader of this site sent this in. No answers, but a nice set of questions.

Consider getting Kernighan and Ritchie title if you find many things puzzling here. 1. What does static variable mean? 2. What is a pointer? 3. What is a structure? 4. What are the differences between structures and arrays? 5. In header files whether functions are declared or defined? 6. What are the differences between malloc() and calloc()? 7. What are macros? What are the advantages and disadvantages? 8. Difference between pass by reference and pass by value? 9. What is static identifier? 10. Where are the auto variables stored? 11. Where does global, static, local, register variables, free memory and C Program instructions get stored? 12. Difference between arrays and linked list? 13. What are enumerations? 14. Describe about storage allocation and scope of global, extern, static, local and register variables? 15. What are register variables? What are the advantage of using register variables? 16. What is the use of typedef? 17. Can we specify variable field width in a scanf() format string? If possible how? 18. Out of fgets() and gets() which function is safe to use and why? 19. Difference between strdup and strcpy? 20. What is recursion? 21. Differentiate between a for loop and a while loop? What are it uses? 22. What are the different storage classes in C? 23. Write down the equivalent pointer expression for referring the same element a[i][j][k][l]? 24. What is difference between Structure and Unions? 25. What the advantages of using Unions? 26. What are the advantages of using pointers in a program? 27. What is the difference between Strings and Arrays? 28. In a header file whether functions are declared or defined? 29. What is a far pointer? where we use it? 30. How will you declare an array of three function pointers where each function receives two ints and returns a float? 31. What is a NULL Pointer? Whether it is same as an uninitialized pointer? 32. What is a NULL Macro? What is the difference between a NULL Pointer and a NULL Macro?

58. Which bit wise operator is suitable for checking whether a particular bit is on or off? 56. How are Structure passing and returning implemented by the complier? 43. What are bit fields? What is the use of bit fields in a Structure declaration? 51. What is the similarity between a Structure.33. Does there exist any way to make the command line arguments available to other functions without passing them as arguments to the function? 50. What is the maximum combined length of command line arguments including the space between adjacent arguments? 48. Which bit wise operator is suitable for putting on a particular bit in a number? 55. Explain one method to process an entire string as one unit? 39. If we want that any wildcard characters in the command line arguments should be appropriately expanded. Can a Structure contain a Pointer to itself? 41. To which numbering system can the binary number 1101100100111100 be easily converted to? 52. What do the ‘c’ and ‘v’ in argc and argv stand for? 46. Write a program to find the Factorial of a number. are we required to make any special provision? If yes. Write a program to concatenate two strings. Are the variables argc and argv are local to main? 47. 59. What is the difference between an enumeration and a set of pre-processor # defines? 45. Write a program to compare two strings without using the strcmp() function. How can we check whether the contents of two structure variables are same or not? 42. Union and enumeration? 40. 60. Which bit wise operator is suitable for checking whether a particular bit is on or off? 53. Write programs for String Reversal. . which? 49. What is near. Which one is equivalent to multiplying by 2?  Left shifting a number by 1  Left shifting an unsigned int or char by 1? 57. How can we read/write Structures from/to data files? 44. 61. Which bit wise operator is suitable for turning off a particular bit in a number? 54. far and huge pointers? How many bytes are occupied by them? 35. How would you obtain segment and offset addresses from a far address of a memory location? 36. The same for Palindrome check. What does the error ‘Null Pointer Assignment’ mean and what causes this error? 34. Are the expressions arr and *arr same for an array of integers? 37. Does mentioning the array name gives the base address in all the contexts? 38. Write a program to interchange 2 variables without using the third one.

itoa() and gcvt() do? 81. What are the advantages of using typedef in a program? 67. How would you use the functions fseek(). How would you obtain the current time and difference between two times? 89. Write a program which employs Recursion? 64. sqrt()? 86. How can you increase the size of a dynamically allocated array? 69. How would you use the functions randomize() and random()? 90. How do you declare the following:  An array of three pointers to chars  An array of three char pointers  A pointer to array of three chars  A pointer to function which receives an int pointer and returns a float pointer  A pointer to a function which receives nothing and returns nothing 80. When reallocating memory if any other pointers point into the same piece of memory do you have to readjust these other pointers or do they get readjusted automatically? 71. How would you use bsearch() function to search a name stored in array of pointers to string? 85. 65. How would you implement a substr() function that extracts a sub string from a given string? . What is object file? How can you access object file? 75. How would you dynamically allocate a one-dimensional and twodimensional array of integers? 68. Write a program which uses command line arguments. How would you use qsort() function to sort an array of structures? 83. What do the functions atoi(). How would you use the functions sin(). Can there be at least some solution to determine the number of arguments passed to a variable argument list function? 79. How would you use the functions memcpy(). How can you increase the size of a statically allocated array? 70. How can a called function determine the number of arguments that have been passed to it? 78. Does there exist any other function which can be used to convert an integer or a float to a string? 82. memset(). Can you write a function similar to printf()? 77. strcpy(). memmove()? 87. Write a program to generate the Fibonacci Series? 63.62. Which header file should you include if you are to develop a function which can accept variable number of arguments? 76. Can you dynamically allocate arrays in expanded memory? 74. pow(). freed(). Write a program which uses functions like strcmp(). How much maximum can you allocate in a single call to malloc()? 73. 66. How would you use qsort() function to sort the name stored in an array of pointers to string? 84. etc. fwrite() and ftell()? 88. Which function should be used to free the memory allocated by calloc()? 72.

x.y). } C interview questions part1 1.y=35. while(*p2++ = *p1++). ajz at his interviews asks what will be printed out when the following code is executed: main() { printf("%x".-1<<4). What will print out? main() { char *p1=“name”. 20). How do you detect a loop in linked list? 97. What will be printed as the result of the operation below: main() { int x=20. x=y++ + x++. Sunil asks: What is the difference between main() in C and main() in C+ +? 98.I feel the correct answer should be linklist is used in cases where you don’t know the memory required to store a data structure and need to allocate is dynamically on demand. Gautam Pagedar adds this question: What is a linklist and why do we use it when we have arrays? . What is the difference between the functions rand().p2). 2. p2=(char*)malloc(20). random(). 96. 0. srand() and randomize()? 92. What will be printed as the result of the operation below: main() .91. How do you print a string on the printer? 94. What is the difference between the functions memmove() and memcpy()? 93. printf(“%d%dn”. char *p2. Can you use the function fprintf() to display the output on the screen? 95. y= ++y + ++x. printf(“%sn”. } Answer : 5794 3. memset (p2. } Answer:empty string.

y).1 4.x>>2).x< <2.b) a=a+b.ptr). What will be printed as the result of the operation below: main() { char s1[]=“Cisco”.x. printf(“%d %dn”.%d. a=temp. temp=a. } Answer: 10. int b) { int temp.y).x.b=a-b. *ptr++. swap2(x. What will be printed as the result of the operation below: main() { .a=a-b. y=10.{ int x=5.y). } Answer:Cisco Systems isco systems 6. 5 10. } Answer: 5.ptr).y).20.s1).%dn”. } int swap2(int a. printf(“%sn”. printf(“%s”. char s2[]= “systems”. What will be printed as the result of the operation below: #define swap(a. b=a. What will be printed as the result of the operation below: main() { char *ptr = ” Cisco Systems”. 5 5. return 0. printf(“%sn”. swap (x. void main() { int x=5. } Answer: Cisco 7.x. printf(“%d %dn”. printf(“%d. ptr++.

The following variable is available in file1. 11. printf("First output:%dn". printf(“%s”. 10.x). strcat(p1.”Cisco”). modifyvalue(). printf("Third output:%dn". who can access it?: 9. strcpy(p1. strcpy(p2. x++. printf("Second output:%dn". p1=(char *)malloc(25).x).c can access the variable. WHat will be the result of the following code? #define TRUE 0 // some code while(TRUE) { // some code } Answer: This will not go into the loop as TRUE is defined as 0. x++. Answer: all the functions in the file1. 13 . x++. } int changevalue(int x) { return(x+=1). int modifyvalue() { return(x+=10). } Answer: 12 . char *p2. } void main() { int x=10. static int average. modifyvalue().p1).x).char *p1. changevalue(x). What will be printed as the result of the operation below: .“systems”). changevalue(x). 13 12.c. What will be printed as the result of the operation below: int x. p2=(char *)malloc(25). } Answer: Ciscosystems 8.p2).

main() { int x=10. Order of constructor and destructor call in case of multiple inheritance? 12.2). . printf(“%d %dn”.fork(). Which way of writing infinite loops is more efficient than others? there are 3ways. When you inherit a class using private keyword which members of base class are visible to the derived class? 14. Scope of static variables? 6. What will be printed as the result of the operation below: main() { int a=0. But cat(cat(1. Is java a pure object oriented language? Why? 11. } Answer: Two lines with “Cisco Systems” will be printed. #define cat(x. Operations involving unsigned and signed – unsigned will be converted to signed 19. Can u have inline virtual functions in a class? 13. } – will print 8 times. y = ++y. 22. Difference between object oriented and object based languages? 7. What happens when recursion functions are declared inline? 5. if(a==0) printf(“Cisco Systemsn”). 16 13. x = x++. main() {fork(). -> ef 15.y).objects contain howmany multiply inherited ancestor? 8. What are the 4 different types of inheritance relationship? 9. ++*ip increments what? it increments what ip points to 18. printf(“Cisco Systemsn”). Can you have constant volatile variable? Yes. How would you find out the no of instance of a class? 10. Array of pts to functions – void (*fptr[10])() 23.y) x##y concatenates x to y.printf("hello world"). you can have a volatile pointer? 17.3) does not expand but gives preprocessor warning. Can structures be passed to the functions by value? 2. a+++b -> (a++)+b 20. Why? 16. malloc(sizeof(0)) will return – valid pointer 21. Advantages and disadvantages of using macro and inline functions? 4. Why cannot arrays be passed by values to functions? 3.x. y=15.fork(). Embedded systems interview questions 1. } Answer: 11. Multiple inheritance . What is the output of printf("\nab\bcd\ref").

What is the difference between hard real-time and soft real-time OS? 31. What are the different storage classes in C? 37.A compound statement is (a) A set of simple statments (b) Demarcated on either side by curly brackets (c) Can be used in place of simple statement (d) A C function is not a compound statement. What are the different BSD and SVR4 communication mechanisms 1. pointers in c? 29. What is false about the following -. What are the features different in pSOS and vxWorks? 35. How is generic list manipulation function written which accepts elements of any kind? 30. 3.r. What is interrupt latency? How can you recuce it? 32. What is forward reference w. The C language terminator is (a) semicolon (b) colon (c) period (d) exclamation mark 2. How is function itoa() written? 26. How can you define a structure with bit field members? 34. What is true about the following C Functions (a) Need not return any value (b) Should always return an integer (c) Should always return a float (d) Should always return more than one value 4. Who to know wether systemuses big endian or little endian format and how to convert among them? 27. What is the differnce between embedded systems and the system in which rtos is running? 33.24. # error – what it does? 25.t. What is interrupt latency? 28. What are the different qualifiers in C? 38. How do you write a function which takes 2 arguments .a byte and a field in the byte and returns the value of the field in that byte? 36. Main must be written as (a) The first function in the program (b) Second function in the program (c) Last function in the program (d) Any where in the program .

Choose from one of the alternatives (a) return(sqr(a)). Which of the following about automatic variables within a function is correct ? (a) Its type must be declared before using the variable (b) Tthey are local (c) They are not initialised to zero (d) They are global 6. (c) return(a*a*a). return(x). 7. return(u).5. Which of the following about the C comments is incorrect ? (a) Ccommentscan go over multiple lines (b) Comments can start any where in the line (c) A line can contain comments with out any language statements (d) Comments can occur within comments 8. if(x=6) y=7. Write one statement equivalent to the following two statements x=sqr(a). (a) 7 (b) 0 (c) 1 (d) 6 9. (d) printf("%d". Read the function conv() given below conv(int t){ int u. (b) printf("sqr(a)"). else y=1. y=0. u=5/9 * (t-32).1 (d) 29 10. } What is returned (a) 15 (b) 0 (c) 16.sqr(a)). What is the value of y in the following code? x=7. Which of the following represents true statement either x is in the range of 10 and 50 or y is zero .

.(a) x >= 10 && x <= 50 || y = = 0 (b) x<50 (c) y!=10 && x>=50 (d) None of these 11... (d) # define TRUE 0 .. printf("%d". i=func(i). q=q+y (d)p=p+x/q=q+y .. while(TRUE){ .. else return 1.. q=q+y (b)p=p+xq=q+y (c)p=p+xq. Which of the following is not an infinite loop ? (a) while(1)\{ .i).. } (c) x=0. } (a) 3 (b) 1 (c) 0 (d) 2 13.. do{ /*x unaltered within the loop*/ ..) { . i=func(i).. q=q+y.. How does the C compiler interpret the following two statements p=p+x...} main() { int =3.} (b) for(..} 12.} while(x = = 0). What does the following function print? func(int i) { if(i%2)return 0. (a) p=p+x.

15 18.16. Read the folllowing code # define MAX 100 # define MIN 100 . if(x>MAX) x=1. To access the 6th element of the array which of the following is incorrect? (a) *(a+5) (b) a[5] (c) pa[5] (d) *(*pa + 5} .15.char c. .string d. if the initial value of x=200. Given the piece of code int a[50].17 use the following alternatives a.float 14.. pa=a.int b... what is the value of l ? (a)20 (b)8 (c)9 (d)21 20.. 10e05 17..For questions 14. A memory of 20 bytes is allocated to a string declared as char *s then the following two statements are executed: s="Entrance" l=strlen(s). "1 e 02" 16.what is the value after executing this code? (a) 200 (b) 1 (c) -1 (d) 50 19. else if(x<MIN) x=-1. int *pa.. x=50. '9' 15.

no) + 1).(*(n1 + 2). else exp 2. (b) if cond 1 if cond 2 if cond 3 exp 1. else exp 4.9 (b) 9..Nicholas"}}.21.no. } struct num nam n1[]={{12. cond 1?cond 2?cond 3?:exp 1:exp 2:exp 3:exp 4.. (b) a=b=c=d=0. (d)int a. else if cond 3 exp 3.. . .5. . printf("%d%d". Consider the following structure: struct num nam{ int no..{15."Martin"}. (c)float a=int b=3.8 (d) 8. Regarding the scope of the varibles..9 (c) 8. float b. What does the above statement print? (a) 8."Peter"}.unpredictable value 22."Fred"}.. is equivalent to which of the following? (a)if cond 1 exp 1. else if cond 2 exp 2. 23.. char name[25]. (c) if cond 1 && cond 2 && cond 3 exp 1 |exp 2|exp 3|exp 4. else exp 3. Identify the in correct expression (a) a=b=3=4..5.n1[2].identify the incorrect statement: (a)automatic variables are automatically initialised to 0 (b)static variables are are automatically initialised to 0 (c)the address of a register variable is not accessiable (d)static variables cannot be initialised with any expression 24.{11. else exp 4. a=b=3.{8.

What is y value of the code if input x=10 y=5.int p. What does the following code do? fn(int n.int r){ static int a=p. if (x==10) else if(x==9) else y=8. The operator for exponencation is (a) ** (b) ^ (c) % (d) not available 26. How many times does the loop occurs? . case 1:a+=a*r. switch(n){ case 4:a+=a*r.a++). (a)9 (b)8 (c)6 (d)7 28. a=0. 25.(d) if cond 3 exp 1. while(a<5) printf("%d\\n". case 3:a+=a*r. Which of the following is invalid (a) a+=b (b) a*=b (c) a>>=b (d) a**=b 27. else exp 4. else if cond 2 exp 2. else if cond 3 exp 3.}} (a)computes simple interest for one year (b)computes amount on compound interest for 1 to 4 years (c)computes simple interest for four year (d)computes compound interst for 1 year 29. case 2:a+=a*r.

(a)infinite (b)5 (c)4 (d)6 30.. Which of the following go out of the loop if expn 2 becoming false (a) while(expn 1)\{..i=10...if(expn 2)continue. How many times does the loop iterated ? for (i=0. What is incorrect among the following A recursive function (a) calls itself (b) is equivalent to a loop (c) has a termination condition (d) does not have a return value at all 32..i) i--. while(i>=0){ printf("%u".} } How many times the loop will get executed (a)10 (b)9 (c)11 (d)infinite 34.if(expn 1)continue.Pick out the add one out (a) malloc() (b) calloc() (c) free() (d) realloc() 35. (a)10 (b) 2 (c) 5 (d) None of these 31..} (b) while(!expn 1)\{if(expn 2)continue.} (c) do{. Consider the following program main() {unsigned int i=10.Consider the following program ....}while(expn 2). (d) while(!expn 2)\{if(expn 1)continue.i+=2) printf("Hi\\n").\} 33..

7. } The value of b[-1] is (a) 1 (b) 3 (c) -6 (d) none 36. The format specified for hexa decimal is .0}. b=&a[2].if(c!=3) 3. } the output of the program is (a) 8 (b) 6 (c) 7 (d) None 37. what is the value of sum (a) 0 (b) 6 (c) 3 (d) None of these 38. break. int y=3. # define prod(a. default:sum=0. switch(n){ case 2:sum=sum+2. printf("%d".prod(x+2.if(c==1) (a) 1 only (b) 1&3 (c) 3 only (d) All of the above 39.if(a<b)then 4.3.b)=a*b main(){ int x=2.sum=1.y-10)).if(c=1) 2.} If n=2.6.main(){ int a[5]={1.Consider the following program segment int n. case 3:sum*=2. Identify the incorrect one 1. int *b.

} The final value of x is (a) 6 (b) 8 (c) 1 (d) 3 43. sum=sum+n. return(sum). while(i>0){ x=func(i). Int *a[5] refers to (a) array of pointers (b) pointer to an array (c) pointerto a pointer (d) none of these 44. } int func(int n){ static sum=0. Find the output of the following program main(){ int x=5.++*p).(a) %d (b) %o (c) %x (d) %u 40. p=&x printf("%d". . } (a) 5 (b) 6 (c) 0 (d) none of these 41. (b) typedef struct { int n3.x. char n2.Which of the following statements is incorrect (a) typedef struct new{ int n1.Consider the following C code main(){ int i=3. i--. *p.}ICE. } DATA. char *n4.

(d) #typedef union { int n7. float n6. .(c) typedef union{ int n5. float n8.} TUDAT.} UDT.