For C/C++ coders discussions and solutions
Wed Feb 16, 2011 8:01 am
Hi friendzz..Can any body tell me the output of this program and explain how it came....and also how these operations (p+p[3]-p[1] ) performed
- Code:
void main()
{
char c[]="GATE2011";
char *p=c;
clrscr();
printf("%s",p+p[3]-p[1]);
getch();
}
Fri Feb 18, 2011 2:58 pm
Is there Any body to explain this.....k ivl give the output, plz i need explanation how this output came?
output is : 2011
Fri Feb 18, 2011 3:37 pm
%s used to print a string of characters .
i didn't run this app, but it seems this p[3]-p[1] part removed the First part of the string . are you sure that the output is 2010 or G2011
Fri Feb 18, 2011 4:29 pm
sure it's 2011 (check once if possible)...
but i need hw this output came i.e. what operations takes place here on p...whether it performs on p's address or ascii values of "p" or any other...
Sun Feb 20, 2011 10:21 am
This code is equivalent to
- Code:
p + (69 - 65)
where 69 is the decimal equivalent of 'E' (p[3]) and 65 is the decimal equivalent of 'A' (p[1])
Tue Feb 22, 2011 5:13 pm
Statement will be: (as written above)
p+(69-65)
For example, if the code is as follows:
printf("%s",p+1);
output will be "ATE2011"
Similarly for
printf("%s",p+2);
output will be "TE 2011"
ASCII values of A(p[1]) and E(p[3]) are 69 and 65 respectively, so it will be "p+(69-65)" and the output will be "2011"
Sat Feb 26, 2011 2:31 pm
thanku bro...i think it is ascii value not decimal value...
Sun Feb 27, 2011 7:13 am
pyromaniac wrote:Statement will be: (as written above)
p+(69-65)
For example, if the code is as follows:
printf("%s",p+1);
output will be "ATE2011"
Similarly for
printf("%s",p+2);
output will be "TE 2011"
ASCII values of A(p[1]) and E(p[3]) are 69 and 65 respectively, so it will be "p+(69-65)" and the output will be "2011"
thank u dude...but p contains the address of the string....but how it can store ascii value in p[1] and p[3]...??
Thu Mar 03, 2011 6:48 am
its 2011 as per my knowledge
.....
Wed Mar 09, 2011 5:39 am
Highly informative post, thanks u for sharing!!..
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.