Advanced C Interview Questions and Answers for Experienced

    1 Votes

31) Print the output of value assignment to a C constant program?

#include<stdio.h>
void main()
{
int const * p=5;
printf("%d",++(*p));
}

This program will give a compiler error: Cannot modify a constant value. Here p is a pointer to a "constant integer". But in the printf function, we tried to change the value of the "constant integer" which is not allowed in C and thus the compilation error.

32) Print the output of this data comparison C program?
#include<stdio.h>
main()
{
float me = 1.1;
double you = 1.1;
if(me==you)
 printf("I love U");
else
 printf("I hate U");
}
Output is "I hate U"

For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the number of bytes, the precession with of the value represented varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than long double. So comparing them will result false and thus the “I hate U' string is printed. It is better to avoid comparing different type of floating point numbers.

33) Guess the output of the backspace and carriage return C program?

#include<stdio.h>
main()
{
 printf("\nab");
 printf("\bsi");
 printf("\rha");
}

Output is "hai"

The “\b” in C represents backspace and \r carriage return. The “\b” will erase the last character printed in the output. So, the first printf feed a newline character and then prints ab. The second printf first erases b from ab and then prints si. Until now the output is asi. The third printf encounters the “\r” and move the cursor back to the beginning of the line and then prints ha overwriting the as of the asi. So, the final output will be hai.

34) Print the output of this recursive function C Program?

void add(int a, int b)
{
 int c;
 c = a + b;
 add (1,1);
}

The function will lead to stack overflow. Here, there is no terminating condition and that is why it leads to infinite recursion which results in STACK OVERFLOW. When a function is called,

  • It will evaluate actual parameter expressions.
  • It will allocate memory for local variables.
  • It will store caller’s current address of execution.
  • Then it executes rest of the function body and reaches end and returns to the caller’s address.

Here without proper termination condition, the recursion will continue and the memory is allocated for all the recursion instances. After certain time the memory allocated for the programs stack will be full and it will cause stack overflow.

35) Print  the output of this format specifier C Program?

#include<stdio.h>
main()
{
 char s[] = "Hello world";
 int i = 7;
 printf("%10.*s", i, s);
}

Output of this program is "Hello w"

In this program the format specifier is a bit different from normal %s. After the “%” operator the 10 forces to print minimum 10 characters to be printed in output. The “.*” followed by that takes an integer argument and represents the number of characters to be printed from the string followed. So, the argument i with value 7 forces to print first 7 characters of the variable s. The remaining 3 characters are filled by preceding blank spaces.

36) Guess the condition in place of X which lead to print “Hello world” as output?

if(X)
{
 printf(“Hello”);
}
else
{
 printf(“ world”);
}

The program should look like this -

if(!printf(“Hello”)
{
 printf(“Hello”);
}
else
{
 printf(“ world”);
}

The printf(“Hello”) in if condition is executed and will print “Hello” in the output and this printf will return number of characters printed i.e. 5. The preceding “!” will turn it 0 which will cause the if condition to fail and execute the printf statement in else block.

37) What will be the output of the following piece of code?

printf(“%d”, (int)sizeof('A'));

The output will be same as sizeof(int). In case 64 bit machine it will be 4. The sizeof operator will change the 'A' to its ASCII value and to the sizeof operator that ASCII value is nothing but an integer.

38) Print the output or error?

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

Output of this program is 45545. The arguments in any function call are pushed into the stack from left to right order. During evaluation those arguments are popped out from the stack. So, ultimately. the evaluation is from right to left. So, the last argument will result 5, the next one will result 4 and also the variable i at that point will hold 4. The next one is again 5 and after that it is again 5 as it is post decrement. The first one will return 4 and turn the value of i to 5.

39) Print the output of this C program with math functions?

#define square(x) x*x
main()
{
 int i;
 i = 64/square(4);
 printf("%d",i);
}

Program will produce the output - 64. The macro call square(4) will substituted by 4*4 and the expression becomes i = 64/4*4 . Since / and * has equal priority the expression will be evaluated based on the associativity which is left to right. So, 64/4*4 will be equal to (64/4)*4 i.e. 16*4 = 64.

40) Guess the output or error?

#include<stdio.h>
struct A
{
 int i;
 char str[10];
 float f;
};

main()
{
 struct A a = {4};
 printf("%d %s %f\n", a.i, a.str, a.f);
}

The program will print 4 0.0000. In C, when a structure variable in partially initialized, other members of that variables will be initialized to 0 or NULL. Here, a.str will be initialized to NULL and a.f will be initialized to 0.0000.

41) Print the output of this static storage class C program ?

#include<stdio.h>
main()
{
 static int a[5];
 int i = 0;
 a[i] = i++;
 printf("%d %d\n", a[0], a[1]);
}

Output - 0 0. The static storage class will initialize all the elements of the array to 0. The 2nd element i.e. element with index 1 will be populated with 0 as the increment is post increment. So, the output will be 0 0.

42) Guess the output of this sizeof C program?

main()
{
 char c;
 printf(“Size of c is %d”, sizeof c);
}

Output of this program is  "Size of c is 1". Sizeof is an operator not function. So, sizeof <variable_name> is a valid statement. But sizeof int is not. It will give you compilation error. Better to use sizeof(<variable_name/data_type>)  i.e. enclosed in parenthesis.

43) Guess output of this C program on Macro?

#define A(x, y) cou##x##y
main()
{
 int count = 12;
 printf(“%d”, A(n,t));
}

Output is 12. The macro A will take two argument and paste them after word cou. So when we called A(n,t), the macro will be replaced with count and print the value 12. Here ## in macro pastes arguments and after replacement it ends up with variable name not value.

44) How to use scanf to get a complete line(i.e upto '\n') from input?

Use scanf(“%[^\n]”, str). [^\n] is a regular expression that means until '\n' is encountered.

45) Print the output of this size multiplier C program?

main()
{
struct A
{
 char a;
 int i; 
}a;
 printf("%d",sizeof(a)); 
}

Output will be 8. The size should be (4 + 1) or 5 but the compiler adds padding and make its size multiple of 4.

Page 3 of 4

Related Articles

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

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know