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.

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.

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)

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.

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?

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.

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

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

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

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

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


Post your comments here:

Your comments will be displayed after verification.