+ 2

What will be the output of the following program and please explain it ?

#include<stdio.h> void main() { char s[]="hello"; printf("%c\n",*(s+1)); printf("%c\n",*s+1); printf("%s\n",*(s+1)); }

5th Dec 2018, 1:46 AM
Aman Kumar
Aman Kumar - avatar
5 Respostas
+ 3
Have you tried compiling it? Take a look, I've commented it for you: https://code.sololearn.com/cmw8om2I5PK1/?ref=app There is a whole lesson on pointers in Sololearn's C course, take a look: https://www.sololearn.com/learn/C/2962/
5th Dec 2018, 5:01 AM
dρlυѕρlυѕ
dρlυѕρlυѕ - avatar
+ 3
I have updated the code. You use %s to print string. Pointer points to one element only (the element of a string is char) , therefore use %c.
5th Dec 2018, 5:36 AM
dρlυѕρlυѕ
dρlυѕρlυѕ - avatar
+ 2
In your code you have written %c at last statement that's why it shows answer but if You replace it by %s then it shows no output Tell me why is is so?
5th Dec 2018, 5:32 AM
Aman Kumar
Aman Kumar - avatar
+ 1
*(s+1) is more like s[1] that is e. 2nd *s+1 is more like s[0]+1 and s[0] is h and when you add 1 to h it will give you the next alphabet after h that is i. 3rd one is you are trying to print a string %s and in *(s+1) i.e s[1]= e that is just a character not string so it will not print any thing i hope you get it
15th Dec 2018, 4:22 PM
Muneer
Muneer - avatar
0
Here in this program even " are also character values. Then why don't s(0) value as " .
27th Jan 2019, 5:27 AM
KURUVA VIVEK KUMAR