C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 16)
16.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    char str[] = "peace";
    char *s = str;
    printf("%s\n", s++ +3);
    return 0;
}
peace
eace
ace
ce
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
40 comments Page 4 of 4.

Lavanya said:   2 decades ago
I need answer description for this.

Suresh said:   1 decade ago
Here *s is a string pointer...s++ means it increases n times..but the pointer comes to the same position..i.e"peace"..now +3 means it gets incremented 3 times..i.e it starts from after 3 chaectrs in string..i.e "c"..so it prints "ce"..

Ranu said:   1 decade ago
All this kind of expression which include + are calculated from right to left of ().

Rohit said:   1 decade ago
Hi.

Here s++ will be executed after the printf statement execution so there will be no effect of increment operator in this printf statement and it will print 'ce' due to +3.

Kiran said:   1 decade ago
Here s++ +3;

The + is more priority than ++ operator.
Then s is incremented to 3 bytes. Then it will print.

You all guys have one doubt that is what about ++ ?

In printf statement only one expression type executed accorrding to its priority based.

That's why here s+3 only occur.

Piyush said:   1 decade ago
Thanks monika.

Sukrutha said:   1 decade ago
Thanks monika.

Poonam said:   1 decade ago
Thanks monika.

Roshan said:   1 decade ago
Thanks monika...

Akshay said:   1 decade ago
Whats the output if they ask like below.......
printf("%d\n", ++s +3); instead of;;
printf("%d\n", s++ +3);


Post your comments here:

Your comments will be displayed after verification.