Search code examples
cprogramming-languages

I am getting a strange output in C?


Here is a c program.I am getting a strange output.

When num1=10 and num2=20->

#include<stdio.h>
void main()
{
int num1=10,num2=20;
clrscr();
if(num1,num2)
{
    printf("TRUE");
}
else
{
    printf("FALSE");
}
getch();
}

Output: TRUE

when num1=0 and num2=220 Output: TRUE

But when num1=0 and num2=0: Output: FALSE Why does this happen? also,what does this given below code mean:

if(num1,num2)

Thanks in advance!


Solution

  • You're using the comma operator. That operator first evaluates its first operand, then drops the result on the floor and proceeds to evaluate and return its second operand.

    That's why your program only prints FALSE if num2 evaluates to false in a boolean context (like e.g. 0, 0.0 or NULL).