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[]="C-program";
    int a = 5;
    printf(a >10?"Ps\n":"%s\n", str);
    return 0;
}

 

  • A.C-program
  • B.Ps
  • C.Error
  • D.None of above

Answer: A

Correct answer: C-program

The conditional operator chooses between the two format strings, not between two outputs. Since \(5 > 10\) is false, the format used is "%s\n", and str is the argument it consumes - so the program prints C-program. Had the condition been true, "Ps\n" would have been the format and str an unused extra argument.

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

Post your comment

Please log in or register to post a comment.