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 1 of 4.
Dipankar said:
1 decade ago
The expression *(s+strlen(s)) will yeild *(s+6), because s[]="hello!"; contains 6 characters, so the length is 6.
and the expression *(s+6)= s[6]. (acc. to pointers to array theory).
now in a strng the last element/bit always contains '\0'.
so, s[0]= 'H'
s[1]= 'e'
s[2]= 'l'
s[3]= 'l'
s[4]= 'o'
s[5]= '!'
s[6]= '\0'
hence the output gives zero value because ascii value of \0 is zero.
and the expression *(s+6)= s[6]. (acc. to pointers to array theory).
now in a strng the last element/bit always contains '\0'.
so, s[0]= 'H'
s[1]= 'e'
s[2]= 'l'
s[3]= 'l'
s[4]= 'o'
s[5]= '!'
s[6]= '\0'
hence the output gives zero value because ascii value of \0 is zero.
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)
Kush said:
1 decade ago
Length of the string will be 6. 's' is a pointer which is pointing to the first character of the string. After getting incremented its value by 6 it will point to null character, having ASCII value 0. So 0 will be printed.
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).
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.
Musaraf said:
1 decade ago
In the statement *(s+strlen(s)) indicates that the lenght is 5 so that it wiil be *(s+5)=*s[5] means its points the last character of the string and that is null so the out put is zero.
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.
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.
So the answer 0.
Priyanka said:
1 decade ago
The lenth of word hello! is 6, but here 's+strlen'. So we can't predict the value of 's' as well as here pointer is used means its address. Therefore output is 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.
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)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers