C Programming - Strings - Discussion

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

int main()
{
    printf("India", "BIX\n");
    return 0;
}
Error
India BIX
India
BIX
Answer: Option
Explanation:

printf("India", "BIX\n"); It prints "India". Because ,(comma) operator has Left to Right associativity. After printing "India", the statement got terminated.

Discussion:
18 comments Page 2 of 2.

Ashok kumar said:   9 years ago
In printf statement there is no format specifier like %s,%c,%d,etc, that's why it is printing the first letter i.e India.

India is the correct option, there are no comma concepts.

Ayush said:   9 years ago
In the case of printf statement once comma is read then it will consider preceding things as variable or values for format specifier.

Vishwambhar said:   1 decade ago
In example:

prinf("Indian","Bix"); here printf() get input string from double quota.

And display on console. So there is no need of store string into stack. In that example "India" is caught on first double quota and then comma operator terminate the string so only India is printed.

In case of a = 5+(1, 2, 3) bracket has higher precedence.

And 1, 2, 3 is stored on stack, so only 3 is popped from stack and addition is performed. Use stack rules last in first out.

Mamta said:   1 decade ago
Comma operator has associativity left to right.

In case of a=5+ (1, 2, 3) bracket has higher precedence and hence it returns the rightmost operator i.e. 3.

So, I think "India" is correct output.

$tRiKeR said:   1 decade ago
It should be "BIX" as output not "India".

Suryakant sharma said:   1 decade ago
So this answer is not correct because comma oprerator have assosiativity right to left.

Rajnish said:   1 decade ago
Associativity of comma operator is right to left.

So the answer should be BIX.

Arjun said:   1 decade ago
I am confuse about comma operator. One ex is a=5 (1, 2, 3) ;result is 8 we discard 1 and 2 and assume 3, why we could do this type on the program answer is "BIX" what different on both. Please tell me.


Post your comments here:

Your comments will be displayed after verification.