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