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 3 of 4.

Sadanand said:   1 decade ago
Thanks priyanka.

Omkar said:   1 decade ago
Thanks Dipankar for Good explanation

Kannan said:   1 decade ago
@Dipankar explained well.

Harshada said:   1 decade ago
Thank you dipankar.

Nagababu said:   1 decade ago
Thank you dipankar.

Prasad said:   1 decade ago
Thanks Dipankar your answer is so clear, I understood.

Shankra said:   1 decade 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.

Gnanavel said:   1 decade ago
Very short example and I understood. Thanks.

Reema ali said:   1 decade ago
Thanks musaraf.


Post your comments here:

Your comments will be displayed after verification.