#include "stdio.h"
#include "conio.h"
int main(void)
{
if(printf("ABC"))
{
}
else
{
printf("XYZ");
}
_getch();
return 0;
}
output : ABC
----------------------------------------------------------------------------------------
#include "stdio.h"
#include "conio.h"
int main(void)
{
if(puts("ABC"))
{
}
else
{
printf("XYZ");
}
_getch();
return 0;
}
output : ABC XYZ
(IDE : MSVC++)
what is the difference between printf and puts in if statement in above 2 programs??
printfreturns the number of character writtenputsreturns a non-negative value in case of successAs a result :
printfreturns a positive value which evaluates totrue, theelsebranch is never executed, thus printingABConlyputsmost likely succeeds and returns 0 which evaluates tofalse, theelsebranch gets executed, thus printing bothABCandXYZAs pointed out by others,
putswill also append a newline whileprintfwon’t.