954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

char *

The output is 2011. Can anyone plz tell me how it is so ????

#include<stdio.h>
#include<conio.h>

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

}
ram619
Light Poster
46 posts since Mar 2010
Reputation Points: 6
Solved Threads: 0
 

Try looking at it like this:

printf("%s",p + (p[3] - p[1]));


or like this:

printf("%s",&p[4]);
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

If your still having problems try looking at the problem like this:

printf("%s",p + ('E' - 'A'));
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

The ascii value of E is 69 and A is 65
so more or less the its like

printf("%s",p + 69-65);

when simplified it will look like

printf("%s",p + 4);

this will print out the characters from 2 up to 1 (2011)

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

Thanks a lot to all of you ............

ram619
Light Poster
46 posts since Mar 2010
Reputation Points: 6
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You