Aptitude (12) ASP.NET (2) Automata (4) Browser (1) C (5) C# (1) C++ (10) Code (3) CSS (1) Data Structure (1) DATABASE (3) HTML (1) java (43) JSP (1) math (1) MySql (8) other (6) php (3) Servlet (3)

Friday, 31 May 2013

What does the following fragment of C-program print?

1) What does the following fragment of C-program print?
char c[] = "GATE2011";
char *p =c;
printf("%s", p + p[3] - p[1]) ;
(A) GATE2011
(B) E2011
(C) 2011
(D) 011
Answer: (C)
See comments for explanation.
char c[] = "GATE2011";
 
 // p now has the base address string "GATE2011"
char *p =c;
 
// p[3] is 'E' and p[1] is 'A'. 
// p[3] - p[1] = ASCII value of 'E' - ASCII value of 'A' = 4
// So the expression  p + p[3] - p[1] becomes p + 4 which is
// base address of string "2011"
printf("%s", p + p[3] - p[1]) ;

No comments:

Post a Comment