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;
}
Error
d
e
abcdefgh
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.

Heena said:   1 decade ago
Can anybody explain it how this o/p comes?

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.

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.
..
..

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?

Post your comments here:

Your comments will be displayed after verification.