Skip to main content
added 587 characters in body
Source Link
Blauelf
  • 21k
  • 2
  • 13
  • 22

Error message is caused by your ; after int main (void), making it a function declaration rather than a function definition. For a function definition, a code block in curly braces follows that line, not a semicolon. Inside those braces is the code that belongs to the function.

Function declaration (not required for main, as you would not call that yourself, but for functions you call above their implementation):

returnType funcName (paramType1, paramType2);

This informs the compiler about the function signature, from that point in code on the function can be used, even if it is implemented later. Parameter names can be supplied to match the function definition, but are ignored.

Function definition:

returnType funcName (paramType1 paramName1, paramType2 paramName2)
{
    /* code in function */
}

That's the actual implementation. Unless you specify void for explicitly not accepting any arguments, parameter names are required.

As other answer's author Ste Bunting pointed out, the increment line should be inside the curly braces, otherwise it becomes the only statement in the loop, with the print function being executed once after the loop has finished.

The x = x++; creates an infinite loop.

Right side x++ is evaluated. While evaluating, x is incremented, but the expression x++ evaluates to pre-increment value of x, unlike ++x, which would evaluate to post-increment value of x. Then you assign that old value to x you just got, reverting the increment. Use just x++; or ++x;, without an assignment.

Error message is caused by your ; after int main (void), making it a function declaration rather than a function definition. For a function definition, a code block in curly braces follows that line, not a semicolon. Inside those braces is the code that belongs to the function.

As other answer's author Ste Bunting pointed out, the increment line should be inside the curly braces, otherwise it becomes the only statement in the loop, with the print function being executed once after the loop has finished.

The x = x++; creates an infinite loop.

Right side x++ is evaluated. While evaluating, x is incremented, but the expression x++ evaluates to pre-increment value of x, unlike ++x, which would evaluate to post-increment value of x. Then you assign that old value to x you just got, reverting the increment. Use just x++; or ++x;, without an assignment.

Error message is caused by your ; after int main (void), making it a function declaration rather than a function definition. For a function definition, a code block in curly braces follows that line, not a semicolon. Inside those braces is the code that belongs to the function.

Function declaration (not required for main, as you would not call that yourself, but for functions you call above their implementation):

returnType funcName (paramType1, paramType2);

This informs the compiler about the function signature, from that point in code on the function can be used, even if it is implemented later. Parameter names can be supplied to match the function definition, but are ignored.

Function definition:

returnType funcName (paramType1 paramName1, paramType2 paramName2)
{
    /* code in function */
}

That's the actual implementation. Unless you specify void for explicitly not accepting any arguments, parameter names are required.

As other answer's author Ste Bunting pointed out, the increment line should be inside the curly braces, otherwise it becomes the only statement in the loop, with the print function being executed once after the loop has finished.

The x = x++; creates an infinite loop.

Right side x++ is evaluated. While evaluating, x is incremented, but the expression x++ evaluates to pre-increment value of x, unlike ++x, which would evaluate to post-increment value of x. Then you assign that old value to x you just got, reverting the increment. Use just x++; or ++x;, without an assignment.

Source Link
Blauelf
  • 21k
  • 2
  • 13
  • 22

Error message is caused by your ; after int main (void), making it a function declaration rather than a function definition. For a function definition, a code block in curly braces follows that line, not a semicolon. Inside those braces is the code that belongs to the function.

As other answer's author Ste Bunting pointed out, the increment line should be inside the curly braces, otherwise it becomes the only statement in the loop, with the print function being executed once after the loop has finished.

The x = x++; creates an infinite loop.

Right side x++ is evaluated. While evaluating, x is incremented, but the expression x++ evaluates to pre-increment value of x, unlike ++x, which would evaluate to post-increment value of x. Then you assign that old value to x you just got, reverting the increment. Use just x++; or ++x;, without an assignment.