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;
}
Discussion:
43 comments Page 1 of 5.
                
                        Mounisha said: 
                         
                        7 years ago
                
                ["IndiabiX"].
0-I
1-n
2-d
3-i
4-a
5-b
6-i
7-X
7["IndiabiX"]= it prints X.
                0-I
1-n
2-d
3-i
4-a
5-b
6-i
7-X
7["IndiabiX"]= it prints X.
                     (7)
                
            
                        Sandesh H said: 
                         
                        7 years ago
                
                int main()
{
printf("%c\n", 7["IndiaBIX"]);//output=X. 7 is index. I=0, n=1,...X=7
printf("%c\n", "IndiaBIX"[7]);//output=X. both printf are same
printf("%d\n", 7["IndiaBIX"]);//output=88. prints ASCII value of X
printf("%c\n", *("IndiaBIX"+7));//output=X
printf("%c\n", *(7+"IndiaBIX"));//output=X
return 0;
}
For more details:
If "a" is an array and "i" is index.
a[i]<=>[i]a<=>*(a+i)<=>*(i+a) // all are same and in problem they have used 2nd case.
                {
printf("%c\n", 7["IndiaBIX"]);//output=X. 7 is index. I=0, n=1,...X=7
printf("%c\n", "IndiaBIX"[7]);//output=X. both printf are same
printf("%d\n", 7["IndiaBIX"]);//output=88. prints ASCII value of X
printf("%c\n", *("IndiaBIX"+7));//output=X
printf("%c\n", *(7+"IndiaBIX"));//output=X
return 0;
}
For more details:
If "a" is an array and "i" is index.
a[i]<=>[i]a<=>*(a+i)<=>*(i+a) // all are same and in problem they have used 2nd case.
                     (3)
                
            
                        Chandana said: 
                         
                        7 years ago
                
                Thanks everyone for explaining.
                
                     (1)
                
            
                        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.
                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)
                
            
                        Saurav said: 
                         
                        9 years ago
                
                This follows the rule: arr[x] = *(arr + x) = *(x + arr) = x[arr] where, arr is an array and x is an integer value.
                
                        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?
                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.
                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.
                        Mohammed masood said: 
                         
                        1 decade ago
                
                If 0["indiabix"] then I.
If 1["indiabix"] then n and similarly others.
                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.
                If %d is used it prints ASCII value of that character.
                        Pankajraj said: 
                         
                        1 decade ago
                
                @Wikiok.
Nice ans/example.
Compiler will check condition and print given character.
                Nice ans/example.
Compiler will check condition and print given character.
Post your comments here:
 
            
        Quick links
                            Quantitative Aptitude
                                    
                                    Verbal (English)
                                    
                                    Reasoning
                                    
                                Programming
                                    
                                    Interview
                                    
                                     Placement Papers