What is the output of the following program: (Assume that the appropriate…
2015
What is the output of the following program: (Assume that the appropriate preprocessor directives are included and there is not syntax error)
main( ) { char S[]="ABCDEFGH"; printf("%C", *(&S[3])); printf("%s", S+4); printf("%u", S); /*Base address of S is 1000 */ }- A.
ABCDEFGH1000
- B.
CDEFGH1000
- C.
DDEFGHH1000
- D.
DEFGH1000
Attempted by 153 students.
Show answer & explanation
Correct answer: D
Answer: DEFGH1000
Explanation:
Evaluate *(&S[3]): &S[3] is the address of the fourth character in the array, and * gives the character at that address. S[3] is 'D', so this printf prints D.
Evaluate S+4 with %s: S+4 points to the fifth character (S[4]) which is 'E'. Printing this as a string outputs the substring "EFGH".
Evaluate printf("%u", S): this prints the numeric value of the pointer S. Given the base address 1000, it prints 1000. (Note: the correct, portable way to print a pointer is using %p; using %u for a pointer is implementation-defined, but the question provides the address.)
Combine the outputs in order: 'D' + 'EFGH' + '1000' => DEFGH1000.