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

Rupinderjit said:   1 decade ago
Most of you guys are wrong.
Guys!! ASCII(%c) value of NULL gives BLANK SPACE in output.
But integer value(%d) of NULL is 0.

Neha said:   1 decade ago
Thanks priyanka.

Musaraf said:   1 decade ago
In the statement *(s+strlen(s)) indicates that the lenght is 5 so that it wiil be *(s+5)=*s[5] means its points the last character of the string and that is null so the out put is zero.

Sonu said:   1 decade ago
Thanx Chetaan. Your explanation is short but very good.

Priyanka said:   1 decade ago
The lenth of word hello! is 6, but here 's+strlen'. So we can't predict the value of 's' as well as here pointer is used means its address. Therefore output is 0.

Priyanka said:   1 decade ago
When a variable declared as static. It automatically initialized its value to zero. So the length was zero. Finally it prints zero.

Chetan said:   1 decade ago
It gives zero value because ascii value of \0 is zero.

Kush said:   1 decade ago
Length of the string will be 6. 's' is a pointer which is pointing to the first character of the string. After getting incremented its value by 6 it will point to null character, having ASCII value 0. So 0 will be printed.


Post your comments here:

Your comments will be displayed after verification.