char c "GATE2011";
char *p =c;
printf "%s", p+p 3 p 1 ;
  =

the output is 2011
can any one explain how?

Please post the entire code that will actually compile. That means adding all the parentheses, commas and semicolons, as well as the function.

What does the following fragment of C-program print?
char c[]="GATE2011";
char *p =c;
printf ("%s", p+p[3]+p[1]);
the ans is 2011
how?

The program you posted is still wrong. The answer would be undefined because p[3] = the letter 'E' and p[1] is the letter 'A'. so p+'E'+'A' = p+69+65, or 127 bytes beyond the end of the buffer. Where did I get those numbers? From any ascii chart, like this one.

the question i have posted is the question of gate 2011(cse).

The code shall print garbage value. Here's why..
p[3] = E ascii code 69, p[1] = A ascii code 65. But p is a itself pointer location.
so p+p[3]+p[1], will point to the memory location 126 specs beyond the base address of the array c[]. Hence the printed value will be garbage. if you are sure about the result then please post the entire code.the provided fragment doesn't make sense.

the answer is 2011

#include<stdio.h>
void main()
{
    char c[]="gate2011";
    char *p=c;
    printf("%s",p+p[3]+p[1]);
    getch();
}

But really i cannot find out that how did it came.printing p[3]or for any i palone does not give any thing except garbage value ,on the other hand printing p gives gate2011.
can anyone explain?plzzz

Edited 5 Years Ago by Narue: Added code tags

I doubt "GATE2001" is something what can be put in a char, for example.

y? sure it char be,provided it is an array of char....(array of char is string...remember??)

You need group of charss to put string to. Char you can put in integer, for example, as it is one smallish integer in C.

the answer is 2011

#include<stdio.h>
void main()
{
    char c[]="gate2011";
    char *p=c;
    printf("%s",p+p[3]-p[1]);
    getch();
}

But really i cannot find out that how did it came.printing p[3]or for any i palone does not give any thing except garbage value ,on the other hand printing p gives gate2011.
can anyone explain?plzzz

its p+p[3]-p[1],not p+p[3]+p[1],hence 2011 is the output...

its p+p[3]-p[1],not p+p[3]+p[1],hence 2011 is the output...

Not possible.

p[3] == '2' == 50 (see any ascii chart for values)

p[1] == 'a' == 97

Therefore: 50 - 97 == -47, which is a negative value, so the result of p+p[3]-p[1] == 47 bytes before p.

the answer is 2011

#include<stdio.h>
void main()
{
    char c[]="gate2011";
    char *p=c;
    printf("%s",p+p[3]+p[1]);
    getch();
}

But really i cannot find out that how did it came.printing p[3]or for any i palone does not give any thing except garbage value ,on the other hand printing p gives gate2011.
can anyone explain?plzzz

change this

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

i don knw how u compiled tat or rather wot u compiled tat on....

Edited 5 Years Ago by Santi1986: n/a

I GOT IT... printf("%s",p+p[3]+p[1])
here p is a pointer pointing to the string so it will contain the base address of "gate 2011"
p[3]=e [ascii code=69] and
p[1]=a [ascii code=65]
and p[3]-p[1]=69-65=4
and p+4 means incrementing p by 4,pointer p will now point to 2
and when we print the pointer with specifier %s it prints the string with base address starting at p+4.
hence output is 2011...

Not possible.

p[3] == '2' == 50 (see any ascii chart for values)

p[1] == 'a' == 97

Therefore: 50 - 97 == -47, which is a negative value, so the result of p+p[3]-p[1] == 47 bytes before p.

p[3]=e[ascii code=69]and p[1]=a[ascii code=65]
therefore 69-65=4 which is a+ve value makes ponter p move ahead 4 bytes

Comments
This article has been dead for over six months. Start a new discussion instead.