Advanced C Interview Questions and Answers for Experienced

    1 Votes

46) Print the output of this function pointer C program?

char fun()
{
 char c = 'A';
 printf("Hello World");
 return c;
}

main()
{
 printf("%d", sizeof(fun()));
}

Output is 1. Even if this is function pointer, the sizeof will get the return type and returns the size of that data type. Here, the return type is char, so the output is 1.

47) Guess output or error:
#include<stdio.h>
main()
{
 int a = 10, b = 20, c = 0;
 printf("%d %d %.0d", a, b, c);
}

Output will be "10 20". The %.0d forces compiler to print non negative values. As c is 0, it will not be printed.

48) Guess the condition so that neither Hello or world is printed ?

if(condition) 
{
 printf("Hello"); 
}
else 
{
 printf("world");
}

Condition will be fclose(stdout). It will close the stdout file handle and no output will be printed.

49) Guess the output or error?

#include<stdio.h>
main()
{
 int x = 'Aa';
 printf("%#x", x);
}

Output of this program is  "0x4161". The #x automatically adds 0x before the number and prints the hexadecimal equivalent. The 'Aa' here is multi-character assignment. It means the i will hold ascii value of these two characters in its two bytes. Thus the output is 0x4161.

50) Guess the output of this variable initialization C program?

#include<stdio.h>
main()
{
 int array[] = {[0] = 1, [1] = 2, [2] = 3 };
 printf("%d %d %d\n", array[0], array[1], array[2]);
}

The output will be 1 2 3. The above initialization technique is unique but allowed in C.

51) Guess the output of this C array program?

#include <stdio.h>
main()
{
 int a[10] = {0,1,2,3,4,5,6,7,8,9};
 printf(“%d”, 1[a]);
}

Output of this program is 1. The a[1] expression is equivalent to *(a + 1) and 1[a] is equivalent to *(1 + a). Both of them are same. So, 1[a] will return the value of a[1].

52) Print the output of this C program using "-->" operator ?

#include <stdio.h>
main()
{
 int i = 10;
while(i --> 0)
{
 printf("%d ", i);
}
}

Output - 9 8 7 6 5 4 3 2 1 0. The “-->” is no new operator. Rather than it is a trick to confuse people. The compiler will translate (i --> 0) to ((i--) > 0) as C does not care about blank spaces. The program will start printing from 9 because of the decrement and upto 0 as this is a post increment.

53) Print the output os unassigned type C program?

#include <stdio.h>
struct A
{
 unsigned int i1 : 3;
 unsigned int i2 : 4;
};

main()
{
 struct A a = { 15, 63};
 printf("%d %d\n", a.i1, a.i2);
}

Output of this program is "7 15". The : 3 or :4 in the structure definition means that i1 will be of unsigned int type, but it will be 3 bit long and i2 will be 4 bit long. So, when we assign the value 15 to i1, it will only store the first 3 bits which will result value 7. Same goes for i2. The value of i2 will be 15.

54) Guess the output of this C program using "#" Operator?

#include <stdio.h>
#define A(a,b) a##b
#define B(a) #a
#define C(a) B(a)

main()
{
 printf("%s\n",C(A(1,2)));
 printf("%s\n",B(A(1,2)));
}

In case of B(A(1,2)), A(1, 2) is stringize using # operator in g macro. So, the output is A(1,2) as string. But in case of first printf, the A(1,2) is passed to macro g but before expanding macro B, the A(1,2) macro is expanded. That is why the output of the first printf is 12.

55) What is the difference between void foo(void) and void foo()?

Ans: In C, void foo() means a function foo taking an unspecified number of arguments of unspecified type and void foo(void) means a function foo taking no arguments. So, in case of void foo(), if we call this function like foo(1,2,3), the compiler will not raise any error. But the compiler will raise an error in case of void foo(void).

When a function is called in C, the caller pushes all of the arguments, in reverse order, into the stack before calling the callee. Using foo() means that the compiler won't care to check the arguments passed to foo. In case of foo(void), before calling the function compiler will specifically check the number of arguments and will raise an error saying the mismatch between number of arguments.

56) Guess the output of Sizeof operator inside expression program?

#include <stdio.h>
main()
{
 int i = 10;
 printf("sizeof(++i) is: %d\n", sizeof(++i));
 printf("i : %d\n",i);
}

Output is as given below

sizeof(++i) is: 4
i : 10

The sizeof operator is evaluated during compile time and also the expression inside sizeof is not evaluated. The sizeof just takes the type of the expression. Here sizeof(++i) is same as sizeof(int).

57) Print the output of this C program using Octel and Decimal?

#include <stdio.h>
main()
{
int a[] = {0001,0010,0100,1000};
int i;

for(i = 0; i < 4; i++)
{
 printf("a[%d] : %d\n", i, a[i]);
}
}

Output

a[0] : 1
a[1] : 8
a[2] : 64
a[3] : 1000

The array elements are 0001,0010,0100,1000. As first three element has preceding 0, the compiler will treat them as octal numbers. So, 0001 in octal is 1 in decimal; 0010 in octal is same as 8 in decimal and 0100 in octal is equivalent to 64 in decimal. But the last element has no preceding 0, so the last element will have value 1000.

58) Guess the output of C program with '!' operator?

#include<stdio.h>
main()
{
 int a;
 a = (int)sizeof(!2.3);
 printf(“%d”, a);
}

Output is 4. The '!' operator takes an argument and return 1 if the value is 0 and 0 otherwise. So, !2.3 is 0. To sizeof 0 is an integer and so the output is 4.

59) Print the output of C program given below using strlen and sizeof operator?

#include<string.h>
#include <stdio.h>
main()
{
 printf("%d %d", sizeof("string"), strlen("string"));
}

Output is "7 6". The sizeof return the number of characters including the null character, but strlen returns number of characters without null character.

60) Write a program to check whether a liked list is circular or not?

The following function checks whether a linked list is circular or not and return 1 if true. Otherwise it returns 0.

int IsCircular(node *head) 
{ 
node *slow, * fast; 
slow = head; 
fast = head->next; 
while(true) 
{ 
if((NULL == fast) || (NULL == fast->next)) 
{ 
 return 0; 
} 
else if((fast == slow) || (fast->next == slow)) 
{ 
 return 1; 
} 
else 
{ 
 slow = slow->next; 
 fast = fast->next->next; 
} 
} 
}

61) Guess the output of this C program having a pointer in the main function?

#include<stdio.h>
void func(int *a)
{
 int x = 1;
 a = &x;
} 

main()
{
 int i = 2;
 int *p = &i;
 func(p);
 printf("%d", *p);
}

Output is 2. The pointer a in function func is a local copy of pointer p. So, even if we assign another location to a, it will not be reflected on the p in main function. The p will point to location of i and will print 2 as result.

Page 4 of 4

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know

Related Articles

C++ Interview Questions and Answers for Freshers, Experienced
Embedded C Interview Questions and Answers
C Interview Questions and Answers