-4

I know that the for loop syntax is like :

for(initialise; condition ; increament/decreament)

but what will be the output for the following:

for( ; ; )
{
printf("hello world");
}
user2809576
  • 121
  • 1
  • 2
  • 11

3 Answers3

2

This has to do with standards for C in general.

6.8.5.3 The for statement

The statement

for ( clause-1 ; expression-2 ; expression-3 ) statement

behaves as follows: The expression expression-2 is the controlling expression that is evaluated before each execution of the loop body. The expression expression-3 is evaluated as a void expression after each execution of the loop body. If clause-1 is a declaration, the scope of any identifiers it declares is the remainder of the declaration and the entire loop, including the other two expressions; it is reached in the order of execution before the first evaluation of the controlling expression. If clause-1 is an expression, it is evaluated as a void expression before the first evaluation of the controlling expression.

Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.

References

  1. WG14/N1256 Committee DraftISO/IEC 9899:TC3, Accessed 2014-04-29, <http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf>
  2. Why can the condition of a for-loop be left empty?, Accessed 2014-04-29, <https://stackoverflow.com/questions/13366290/why-can-the-condition-of-a-for-loop-be-left-empty>
Community
  • 1
  • 1
Cloud
  • 18,753
  • 15
  • 79
  • 153
0

No conditions are specified in the for loop, therefore the loop will run forever.

https://www.princeton.edu/~achaney/tmve/wiki100k/docs/Infinite_loop.html

ElGavilan
  • 6,610
  • 16
  • 27
  • 36
0

That will loop forever, printing Hello World every time through the loop. Another way to achieve this same result is by doing:

while(1){
    printf("Hello World");
}

Also use indentation. it helps with readability (not a problem in your example, but with bigger problems it would be an issue.

Jet
  • 43
  • 5