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 1 of 4.
Sairam said:
6 years ago
int main()
{
printf("%x\n", 9["IndiaSAIRAM"]);
return 0;
}
What will be the output and please explain how?
{
printf("%x\n", 9["IndiaSAIRAM"]);
return 0;
}
What will be the output and please explain how?
(5)
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.
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)
Roshini said:
1 decade ago
Thanks monica.
Mangesh said:
1 decade ago
@Monika : Thamk you so much for explaining in such a nice way.....
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
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].
K.MOUNIKA said:
1 decade ago
All this kind of expression which include + are calculated from right to left of (). thanks @Swathi.
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.
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?
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers