Switch to full style
For C/C++ coders discussions and solutions
Post a reply

what is the output of this program and how it be?

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:
#include<stdio.h>
#include<conio.h>
void main()
{
char  c[]="GATE2011";
char *p=c;
clrscr();
printf("%s",p+p[3]-p[1]);
getch();




Re: what is the output of this program and how it be?

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

Re: what is the output of this program and how it be?

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

Re: what is the output of this program and how it be?

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...

Re: what is the output of this program and how it be?

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])

Re: what is the output of this program and how it be?

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"

Re: what is the output of this program and how it be?

Sat Feb 26, 2011 2:31 pm

thanku bro...i think it is ascii value not decimal value...

Re: what is the output of this program and how it be?

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]...??

Re: what is the output of this program and how it be?

Thu Mar 03, 2011 6:48 am

its 2011 as per my knowledge
.....

Re: what is the output of this program and how it be?

Wed Mar 09, 2011 5:39 am

Highly informative post, thanks u for sharing!!..

Post a reply
  Related Posts  to : what is the output of this program and how it be?
 OUTPUT??     -  
 Input-Output Operations     -  
 Use for loop to output HTML table     -  
 echo command to output HTML     -  
 output associate array by print_r     -  
 HOW TO DO C++ PROGRAM?     -  
 Need help in Chatting Program     -  
 java program     -  
 java program     -  
 how can i DeployPHP Program     -