Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the following program?

#include <stdio.h>

void fun();
int main()
{
fun();
return 0;
}
void fun()
{
auto int i = 1;
register char a = 'D';
static int p = 0;
printf("%d %d %ld", i, a, p);
}

 

  • A.1 D 0
  • B.1 0 0
  • C.0 D 1
  • D.1 68 0

Answer: D

Correct answer: 1 68 0

Each declaration in fun behaves as its storage class says:

  • auto int i = 1; prints as 1.
  • register char a = 'D'; is printed with %d, not %c, so it comes out as the character’s code - 68 in ASCII.
  • static int p = 0; prints as 0.

So the output is 1 68 0. The trap is the format specifier: a char is an integer type, and %d shows its numeric value.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.