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.

Recommended Answers

All 2 Replies

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.
{

}

thanks. that helped.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.