C Programming - Strings - Discussion
Discussion Forum : Strings - Find Output of Program (Q.No. 34)
34.
What will be the output of the program ?
#include<stdio.h>
#include<string.h>
int main()
{
printf("%c\n", "abcdefgh"[4]);
return 0;
}
Answer: Option
Explanation:
printf("%c\n", "abcdefgh"[4]); It prints the 5 character of the string "abcdefgh".
Hence the output is 'e'.
Discussion:
4 comments Page 1 of 1.
Dastaan said:
10 years ago
So we don't have to declare a pointer variable before use? and what else after ", " (comma) in statement, will it proceed after the comma?
Mohsin said:
1 decade ago
In this question "abcdefgh" is like a pointer variable.
For example p is pointer variable that contains string "abcdefgh".
char *p="abcdefgh";
Now print.
p[0] = "abcdefgh"[0] = a.
p[1] = "abcdefgh"[1] = b.
p[2] = "abcdefgh"[2] = c.
p[3] = "abcdefgh"[3] = d.
p[4] = "abcdefgh"[4] = e.
..
..
For example p is pointer variable that contains string "abcdefgh".
char *p="abcdefgh";
Now print.
p[0] = "abcdefgh"[0] = a.
p[1] = "abcdefgh"[1] = b.
p[2] = "abcdefgh"[2] = c.
p[3] = "abcdefgh"[3] = d.
p[4] = "abcdefgh"[4] = e.
..
..
Kumar said:
1 decade ago
"abcdefgh" is a string which returns a pointer pointing to starting address of string.
Let pointer be ptr. Then,
ptr[4] = *(ptr+4)= e.
Let pointer be ptr. Then,
ptr[4] = *(ptr+4)= e.
Heena said:
1 decade ago
Can anybody explain it how this o/p comes?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers