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 1 of 2.

Suma said:   6 years ago
Comma operator has left to right associativity. only unary,ternary,assignment operators has right to left associativity.

eg:printf("%d",1,2,3,4);here since we have only one %d , output is 1.Here comma acts a seperator and no associativity plays role.

eg:printf("%d",(1,2,3,4));as comma has left to right associativity 4 is printed as output.

The same applies to strings.

eg:1 printf("India", "BIX\n"); ---> comma acts as seperator, prints India as output.

eg:2: printf(("India", "BIX\n"));---> comma's associativity rule prints BIX as output.
(4)

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.

Sharu said:   8 years ago
Answer of this statement should be "India" because comma operator always returns rightmost operator, in the case of printf statement once comma is read then it will consider preceding things as variable or values for format specifier.

Deepak said:   5 years ago
Comma operator during its scanning goes right to left and while at the rightmost it won't evaluate the rightmost then it will be scanning towards the left and evaluate the left then after goes to the right and evaluate the rightmost.
(1)

Musthak rahaman said:   6 years ago
For this programme, in printf whatever you type between the " " it returns it....because there is no statements and conditions in this simple programme....to consider the comma operator....just follow the basic.

Aishwarya said:   8 years ago
The comma operator has left to right associativity, but it returns the rightmost operand. So in the above question it will start evaluating from left to right and hence it will print "india".

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.

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.

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.

Jhannu said:   8 years ago
Comma operator has right to left associativity but here comma is not being operated because of a missing paranthesis (). printf simply prints the first string.


Post your comments here:

Your comments will be displayed after verification.