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;
}
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.
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.
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.
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)
Rajdeep said:
6 years ago
Comma operator always returns the right most value so the output of this question should be Bix.
Nitin said:
7 years ago
No, we can't pass two strings in printf.
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.
Shubham said:
8 years ago
I got error on linux 64 bit compiler, it say's "too many arguments".
Can anyone help me?
Can anyone help me?
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.
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".
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.
India is the correct option, there are no comma concepts.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers