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 3 of 4.
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.
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?
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.
Lak said:
1 decade ago
Length of string = 8;
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.
Anant said:
10 years ago
It will print 0 because it points to null character.
Snehal said:
9 years ago
Thank you @Dipankar. Nice explanation.
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?
KALYANI said:
9 years ago
*(s+6) means 6th element from s of 0. So it will be null.
Ragini said:
8 years ago
Thank you @Kalyani.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers