Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple C program output 2

Status
Not open for further replies.

Mogali

Technical User
Aug 21, 2007
1
US
Hi,
I am new to C Program
What is the out put of this program ?
When I executed the output is 2 for c and 6 for d.
Can u tell me reason why it's so.
#include<stdio.h>

void main()
{
int a = 2, b = 6, c, d;

c = a,b;
d = (a,b);

printf("|n The value of c is: %d", c);
printf("|n The value of d is: %d", d);
}
Thanks in advance.
 
c = a,b;

"," is an operator so first evaluate " c = a " then evaluate the 2nd part of the statement "b".

d = (a,b);

first evaluate the stuff in the parentesis "( 2,6)"

From "THE C PROGRAMMING LANGUAGE" K&R:
"A pair of expressions separated by a comma is evaluated left to right, and the type and value of the result are the type and value of the right operand."

so "d" becomes 6
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top