Hossain Hossain - 2 months ago 6
C Question

When i give negative number, why don't work first function?

When i give any negative input number, why don't work the first function? Thank you.

#include<stdio.h>
int main()
{
int y=-4;
if(y>0)
printf("i"); /*This line*/
printf("Love");
printf("program");

return 0;
}

Answer

The 'if' statement operates on a code block, i.e. if the condition is true then the following code block is executed otherwise it passes to the next. A code block can consist of a single line or a collection of lines enclosed in braces, e.g. '{ some lines of code }'

Your example has a single line following the if statement:

if(y>0)
printf("i");

so if y is greater than zero then print "i". You have set y to less than zero so the condition is false and nothing is printed by that line. Execution then continues at the next line:

printf("Love");

Hopefully that explains why it doesn't work as you are expecting. I'd recommend more time spent studying the basics of the C language and program flow.