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.
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)
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)
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;
}
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;
}
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?
BHAWNA KUKREJA said:
8 years ago
Thanks @Monika.
Yogeshwari said:
9 years ago
Thanks @Monika.
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.
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.
Vicky said:
1 decade ago
Printf ("%c\n", s++ +3);// This will print only c right?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers