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.
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.
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.
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".
Vicky said:
1 decade ago
Printf ("%c\n", s++ +3);// This will print only c right?
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.
{
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.
Manoj Majumdar said:
1 decade 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.
Rest what remains is s+3, which makes the pointer point to c and from there it will print the remaining string.
Yogeshwari said:
10 years ago
Thanks @Monika.
BHAWNA KUKREJA said:
9 years ago
Thanks @Monika.
San said:
8 years ago
++ is the unary operator and + is a binary operator.
unary given higher precedence over binary.
so ++ should consider.
Is it right?
unary given higher precedence over binary.
so ++ should consider.
Is it right?
Venkat said:
8 years ago
@All.
Please consider this.
#include<stdio.h>
int main()
{
char str[]="peace";
char *s = str;
printf("%s\n",++s +3);
return 0;
}
Please consider this.
#include<stdio.h>
int main()
{
char str[]="peace";
char *s = str;
printf("%s\n",++s +3);
return 0;
}
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers