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 2 of 4.

Amit shinde said:   3 years ago
printf("%s\n", s++ +3);

In the above statement, ++ after s is post-increment, s represents the address of p and s+3 will give the address of c. Here, s++ +3 is the same as s+3 because ++ is post-increment.
(3)

Beniwal said:   1 decade ago
#include<stdio.h>

int main()
{
char str[] = "peace";
char *s = str;
printf("%s\n", s++ +3);
printf("%s\n", s+3);
return 0;
}

Run this. You will know diff.

Krishan Kumar Pandey said:   1 decade ago
Actually What is happening here, s++ is the post increment. So the initial value of s is replacing s++ means first character and you adding +3 so it will go to 'c' and print remaining characters.

Datta Bachate said:   1 decade ago
*s = base address of str.
Now s is pointing to p.

Since s++ is a post increment, it gets incremented 3 times, not before that. So s is pointing to c.

So it points from "ce".

Rajesh said:   1 decade ago
*s=base adress of str
now s is pointing to p.
since s++ is a post increment , it gets incremented 3 times , not before that . so s is pointing to c .
so it pints from "ce"

Manoj Majumdar said:   9 years ago
=> s++, executes later after printing the string for the first time.

Rest what remains is s+3, which makes the pointer point to c and from there it will print the remaining string.

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.

Venkat said:   7 years ago
@All.

Please consider this.

#include<stdio.h>

int main()
{
char str[]="peace";
char *s = str;
printf("%s\n",++s +3);
return 0;
}

Sairam said:   6 years ago
int main()
{
printf("%x\n", 9["IndiaSAIRAM"]);
return 0;
}

What will be the output and please explain how?
(5)

San said:   7 years ago
++ is the unary operator and + is a binary operator.
unary given higher precedence over binary.
so ++ should consider.

Is it right?


Post your comments here:

Your comments will be displayed after verification.