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

Shah said:   4 years ago
* (s+6) and array start from 0, so the last 6th char is NULL and ASCII value of NULL is zero (0) So it will return 0 (zero).
(12)

Kalyan said:   6 years ago
The answer is 6 in GCC compiler.
(2)

Musthak Rahaman said:   6 years ago
In this for strlen we have to count how many elements present in the given string value...and *(s+strlen(s))becomes *(s+6)it is nothing but s[6] but in declared string variable we have 5 positions hence s[6] must be "\0" so the answer is 0.
(9)

Jibon said:   6 years ago
Thanks @Dipankar.

Mala said:   7 years ago
Thanks to all for explaining this.
(1)

Buela said:   8 years ago
Thanks @Musaraf.

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.

Shaurabh Suman said:   8 years ago
Here,
strlen(s)= 6.
So,
*(s+ strlen(s)) = *(s + 6) = s[6].

But here,
s[6] = '\0'.
So , the ASCII value of '\0' is zero.
Ans - 0.
(3)

Ragini said:   8 years ago
Thank you @Kalyani.

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


Post your comments here:

Your comments will be displayed after verification.