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)

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)

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)

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

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

Ram bhawan said:   1 decade ago
Length of string = 6.

str+6 = \0(i.e. Ascii value = 0).
i.e. = 0;
Simply.

Subhankar Som said:   1 decade ago
strlen of Hello is 6. The expression *(s+strlen(s)) indicates *(s+6). According to pointers to array concept *(s+6)=s[6].

The last element contain '\0'. So the output gives 0 value as the value of \0 is 0(ASCII).

Taj said:   1 decade ago
IT's ok that it will print "0" when "%d" is used.

Why it is printing blank when "%c" is used?

Vivek said:   1 decade ago
strlen(s) = 6.
*(s + 6) = s[6].
s[6] = null.
Therefore it is 0.

As @Meghana said.

Lak said:   1 decade ago
Length of string = 8;


Post your comments here:

Your comments will be displayed after verification.