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;
}
Discussion:
40 comments Page 2 of 4.
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".
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".
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.
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.
Arpit said:
1 decade ago
Actually s++ = s+1 here use this fundamental s++ +3 means s = s+3.
So the answer is ce.
So the answer is ce.
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?
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?
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.
K.MOUNIKA said:
1 decade ago
All this kind of expression which include + are calculated from right to left of (). thanks @Swathi.
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].
{
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].
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
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
Roshini said:
1 decade ago
Thanks monica.
Mangesh said:
1 decade ago
@Monika : Thamk you so much for explaining in such a nice way.....
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers