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

Discussion

Consider the following ‘C’ fragment of code where a, b, c is given as input then predict the output from the following :

. . .
char x, y, z;
printf (‘‘%d’’, scanf (‘‘%c%c%c’’, &x, &y, &z));

  • A.A syntax error
  • B.A fatal error
  • C.Segmentation violation
  • D.Shows 3 at output

Answer: D

The program shows 3 at output.

The inner call scanf("%c%c%c", &x, &y, &z) is evaluated first. It reads three characters from the input into x, y and z. scanf returns the number of input items successfully assigned, which is 3. That return value is then the argument of printf with the %d format, so printf prints 3.

The code is valid C, so there is no syntax error, and the arguments are proper addresses of char variables, so there is no fatal error or segmentation violation. Whatever characters are typed, three successful %c conversions give 3.

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

Post your comment

Please log in or register to post a comment.