C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 15)
15.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    printf("%c\n", 7["IndiaBIX"]);
    return 0;
}
Error: in printf
Nothing will print
print "X" of IndiaBIX
print "7"
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
43 comments Page 3 of 5.

Shabana said:   1 decade ago
Here in the program.
I - 0.
n - 1.
d - 2.
i - 3.
a - 4.
B - 5.
I - 6.
X - 7.

The compiler will skip all 6 character and it will 7th character X only because of 7["IndiaBIX"].

Manali said:   1 decade ago
char a[]="indiaBIX"
printf("%c \n",7[a]);

You will get same output.

Mj taseen said:   1 decade ago
#include<stdio.h>

int main()
{
printf("%d\n", 7["IndiaBIX"]);
return 0;
}

If we write %d then output is 120 can any one explain me?

Nagendra said:   1 decade ago
@Taseen: you're wrong.

Because 120 is the ASCII for lowercase x.
The ASCII for uppercase x is 88.
In your program it is uppercase so output should be 88 not 120.

Bins Emmanuel said:   1 decade ago
We can write a[i] = i[a].

Here they given second case,

Element[address]; i.e. When we are initializing a variable, it is mapping with address.

Eg: a ="indiabix".

a is stored with the base address of "indiabix" in code section.

So here,

7["indiabix"] = "indiabix"[7].

i.e assume address of indiabix stored in code section is 1000.

Then 1000[7].

1000[7] = *(1000+7) = *(1007) = x;

Note: The basic datatype of string is char, & ending with null char.
(1)

Mohammed masood said:   1 decade ago
If 0["indiabix"] then I.

If 1["indiabix"] then n and similarly others.

Mohammed masood said:   1 decade ago
@Mj Taseen.

If %d is used it prints ASCII value of that character.

Pankajraj said:   10 years ago
@Wikiok.

Nice ans/example.

Compiler will check condition and print given character.

Prakhar said:   10 years ago
7[IndiaBIX] is same as IndiaBIX[7] and printf("%c", IndiaBIX[7]) will print character at 7th index of IndiaBIX.

Krishna said:   10 years ago
If I write,

printf("%c,%c \n", "infocom"[5][3]);

It show error:

Subscripted value is neither array nor pointer.

Why this is some one can explain me?


Post your comments here:

Your comments will be displayed after verification.