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"):
Theprintf
function prints the string"hi\\n"
to the standard output (usually the terminal or console).
printf
returns the number of characters printed. In this case,"hi\\n"
has 3 characters ('h', 'i', and '\\n'), soprintf
returns3
. - if(printf("hi\\n")):
Theif
statement checks the return value ofprintf("hi\\n")
. Sinceprintf
returns3
(which is non-zero), the condition evaluates totrue
. - Inside the if block:
Because the condition istrue
, the code inside theif
block is executed. Thus,printf("welcome\\n");
is called, which prints"welcome\\n"
to the standard output. - The else block:
Since theif
condition was true, theelse
block is skipped and theprintf("Hello\\n");
inside theelse
block is not executed.
Summary:
- The program first prints
"hi\\n"
. - Then, it prints
"welcome\\n"
. - The
"Hello\\n"
message is not printed because theelse
block is not reached.
Output of the program:
hi
welcome
hi
welcome
Comments
Post a Comment