C Programming - Strings - Discussion

Discussion Forum : Strings - Find Output of Program (Q.No. 12)
12.
What will be the output of the program ?
#include<stdio.h>
#include<string.h>

int main()
{
    static char s[] = "Hello!";
    printf("%d\n", *(s+strlen(s)));
    return 0;
}
8
0
16
Error
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
38 comments Page 2 of 4.

Chandra vijay vishwakarma said:   1 decade ago
See, strlen(s)=6, and every string have a default null character, right?

So whenever we call *(s+strlen (s)) means, now char *s is pointing to null character and ASCII of null character is 0, So it is printing 0.

Anant said:   10 years ago
It will print 0 because it points to null character.

Snehal said:   9 years ago
Thank you @Dipankar. Nice explanation.

Mohana Murali yk said:   9 years ago
S returns address where character 'H' is stored, it is added to length of string, which is unknown? Am I right?

KALYANI said:   9 years ago
*(s+6) means 6th element from s of 0. So it will be null.

Ragini said:   8 years ago
Thank you @Kalyani.

Vilas said:   8 years ago
As pointer s, points to the (s+6) th position. There mean null space (\0). And static char keep default value as 0. If the variable is not initialized.

So the answer 0.

Buela said:   8 years ago
Thanks @Musaraf.

Jibon said:   6 years ago
Thanks @Dipankar.

Dipankar said:   1 decade ago
The expression *(s+strlen(s)) will yeild *(s+6), because s[]="hello!"; contains 6 characters, so the length is 6.

and the expression *(s+6)= s[6]. (acc. to pointers to array theory).

now in a strng the last element/bit always contains '\0'.
so, s[0]= 'H'
s[1]= 'e'
s[2]= 'l'
s[3]= 'l'
s[4]= 'o'
s[5]= '!'
s[6]= '\0'

hence the output gives zero value because ascii value of \0 is zero.


Post your comments here:

Your comments will be displayed after verification.