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;
}
Discussion:
38 comments Page 2 of 4.
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?
Snehal said:
9 years ago
Thank you @Dipankar. Nice explanation.
Anant said:
10 years ago
It will print 0 because it points to null character.
Chandra vijay vishwakarma said:
1 decade ago
See, strlen(s)=6, and every string have a default null character, right?
So whenever we call *(s+strlen (s)) means, now char *s is pointing to null character and ASCII of null character is 0, So it is printing 0.
So whenever we call *(s+strlen (s)) means, now char *s is pointing to null character and ASCII of null character is 0, So it is printing 0.
Lak said:
1 decade ago
Length of string = 8;
Vivek said:
1 decade ago
strlen(s) = 6.
*(s + 6) = s[6].
s[6] = null.
Therefore it is 0.
As @Meghana said.
*(s + 6) = s[6].
s[6] = null.
Therefore it is 0.
As @Meghana said.
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?
Why it is printing blank when "%c" is used?
Ram bhawan said:
1 decade ago
Length of string = 6.
str+6 = \0(i.e. Ascii value = 0).
i.e. = 0;
Simply.
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).
The last element contain '\0'. So the output gives 0 value as the value of \0 is 0(ASCII).
Meghana said:
1 decade ago
strlen of hello! is 6. So expression in printf statement reduces to *(s+6). *(s+6) is same as s[6], which is null character. ASCII value for null character is 0. So it prints 0.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers