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

Discussion

What will be the output of the program ?

#include <stdio.h>

int main()
{
    char *str;
    str = "%d\n";
    str++;
    str++;
    printf(str-2, 300);
    return 0;
}

 

  • A.No output
  • B.30
  • C.3
  • D.300

Answer: D

Correct answer: 300

str initially points at the format "%d\n". Two increments advance it two characters, and str-2 then brings it back to where it started.

So printf receives the original format string and prints 300 followed by a newline. The exercise is about pointer arithmetic being exactly reversible: advancing and retreating by the same amount returns the identical address, and the intervening increments leave no trace.

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

Post your comment

Please log in or register to post a comment.