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

Ravindra bagale said:   1 decade ago
@Kiran.

Hello, ++ has highest priority than '+'.watch following associativity & precedence table. because of the post increment, it first adds 3 in it & printf gets execute, after that post increment happens.

@Akshaya.

printf("%d\n", ++s +3); answer of this is: e
because os pre increment, it 1st increment to 'e' & adds 3 into it, that's why next 'e'.



Operators in order of precedence Associativity
(), [], ->, . left to right
!, ~, ++, -, - (unary), * (indirection), & (address-of), sizeof, casts right to left
* (multiplication), /, % left to right
+, - (subtraction) left to right
<<, >> left to right
<, <=, >=, > left to right
==, != left to right
& (bitwise and) left to right
^ left to right
| left to right
&& left to right
|| left to right
?: right to left
=, +=, -= etc. right to left
, left to right

Rohan said:   7 years ago
@ALL.

#include<stdio.h>
int main()
{
Step1: char str[] = "peace";
/* It will declare str[] array as character and store peace in it.*/

Step 2: char *s = str;
/* It will declare a pointer variable (s) as character and store the content of str. */

Step3: printf("%s\n", s++ +3);
/* It will print the string (as specified %s) , the next question is what?

(i) s++ --> It will point to "p" because it is post_increment operator and still contain string as "peace".
(ii) s++ +3 = add three positions (i.e. At present , it is pointing to s[0] but after incrementing s[0+3] =s[3] , and it is pointing to "c" position no. 3) and prints further (i.e. "ce").
return 0;
}

MONIKA said:   1 decade ago
Hello lavanya.

I don't know why you are confused about this, means I don't know about your confusion, then also I would like to give some explanation.

Since here +3 is used, you have to increment upto 3 positions but pointer will not shift to that position and you will get character 'c' then string starting from 'c' i.e. "ce" will print.

And if you are thinking about the s++ yes it will work but after getting printed the string because it is a post-increment and since it is still pointing the first character of the string i.e. 'p', hence now it reaches at 'e'.

If you will use the statement after last printf statement in program.

printf ("\n%s", s) ;.

It will print string "eace".

Hope I make you understand. Thanx.

Ashu said:   1 decade ago
In the pointers when we store the address of string. Like here s points to the str. on incrementing s i.e. s + 1, from the second position the string is printed i.e. if we do s + 1 then printf("%s",str) then "eace" is printed.

Does the same it holds for array ? I mean point a pointer 'p' to an array then we do p +1 then from there can the rest elements be printed?

Can any1 explain?

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.

Inside the c said:   1 decade ago
int main()
{
char str[] = "peace";
char *s = str;
printf("%s\n", s++ +3);
return 0;
}

Here s points to str
s--->peace

s++ +3//s is post incremented so don't care about s in this line.
You must consider it as just s+3 which is equal to s[3].

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

int main()
{
char str[] = "peace";
char *s = str;
s++;
printf("%s\n", s+3);
return 0;
}
if you run the above code you wold know the difference...!
in the given question s++ not gets incremented b4 printing..!

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

Suresh Paldia said:   1 decade ago
printf("%s\n", s++ +3);
In this,
first s+3 will get executed and printed. Which means s points to c. Hence, ce gets printed.
Then, s++ executes. That makes s to point to e but will not have any effect on outputed print.

Gowtham said:   1 decade ago
int main()
{
char str[] = "peace";
char *s = str;
printf("%s\n", ++s +2);
return 0;
}

It will print as "ce". Because pre-increment and post-increment concept is also included in this code.


Post your comments here:

Your comments will be displayed after verification.