0

I saw this small code of a program to print HI without a semicolon in the program. I don understand how it works.

int main()
{
while(printf("HI")-2) 
{}
}

Any help would be appreciated.

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by adarshcu
0

printf returns the number of bytes printed to the terminal. So the expression printf("HI")-2 returns 0. We end up with while(0){} .
For further clarification on printf visit here.
As printf is the expression used as the condition for the 'while' loop, no semicolon is required.It is same as

int val = printf("HI");
while(val - 2)//value of val is 2.
{

}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.