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

Discussion

What will be the output of the following program?

#include <stdio.h>

int main()
{
register int i = 2;
static char ch = 'A';
auto float j;
int k;
k = ++ch && i;
k = ++ch;
j = i-- + ++k * 2;
printf("%d %f", k, j);
return 0;
}

 

  • A.B 3
  • B.65 138.000000
  • C.68 138.000000
  • D.A 138

Answer: C

Correct answer: 68 138.000000

Follow the four statements, with ch starting at 'A' = 65.

  1. k = ++ch && i; - ch becomes 66; both operands are non-zero, so k is 1.
  2. k = ++ch; - ch becomes 67 and k takes that value.
  3. j = i-- + ++k * 2; - i-- contributes 2 (leaving i at 1); ++k makes k 68, and \(68 \times 2 = 136\); so \(j = 2 + 136 = 138\).

Printed with %d and %f: 68 138.000000.

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

Post your comment

Please log in or register to post a comment.