You are here:

C/question of c programming

Advertisement


Question
What does the following fragment of C-program print?

char c[]="GATE2011";
char *p =c;
printf("%s", p+p[3]-p[1]);

Answer
Hello Rajesh

In this program, p[3] is 'E', and p[1] is 'A'. The ASCII character code for 'E' is 69, and for 'A' it is 65.  69 - 65 is 4, so the progam is adding 4 to the pointer p. The p points to the start of the character string "GATE2011", so p points to 'G'. Adding 4 to p results in a pointer to '2', so the program prints the string starting at the character '2', so it prints 2011.

I hope that is clear.
Best regards
Zlatko

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.