if inside printf function
if inside printf function
Program
#include <stdio.h>
int main() {
if(printf("hi\\n"))
{
printf("welcome\\n") ;
}
else
{
printf("Hello\\n") ;
}
}
Here's a step-by-step explanation of what this code does:
- printf("hi\\n"):
Theprintffunction prints the string"hi\\n"to the standard output (usually the terminal or console).
printfreturns the number of characters printed. In this case,"hi\\n"has 3 characters ('h', 'i', and '\\n'), soprintfreturns3. - if(printf("hi\\n")):
Theifstatement checks the return value ofprintf("hi\\n"). Sinceprintfreturns3(which is non-zero), the condition evaluates totrue. - Inside the if block:
Because the condition istrue, the code inside theifblock is executed. Thus,printf("welcome\\n");is called, which prints"welcome\\n"to the standard output. - The else block:
Since theifcondition was true, theelseblock is skipped and theprintf("Hello\\n");inside theelseblock is not executed.
Summary:
- The program first prints
"hi\\n". - Then, it prints
"welcome\\n". - The
"Hello\\n"message is not printed because theelseblock is not reached.
Output of the program:
hi
welcome
hi
welcome
Comments
Post a Comment