0
int main (void);

int x = 0;
while (x < 100) 
x = x++;
{
printf ("here we go, %i /n", x);
}

The errors I get are:

loopwhile.c:8:1: error: expected identifier or '('
while (x < 100)  //if boolean is true all code between braces will execute repetedly u...
^
loopwhile.c:10:1: error: expected identifier or '('
{
^

I don't get this- it's looking for '(', and it's there, so....?

2 Answers 2

1

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.

8
  • So this is what I did: int main (void) int x = 0; while ( x < 100) //if boolean is true all code between braces will execute repetedly until false { printf ("here we go, %i /n", x); x++; }
    – Rick
    Commented Feb 9, 2017 at 0:38
  • the errors are: $ make loopwhile clang -fsanitize=integer -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow loopwhile.c -lcrypt -lcs50 -lm -o loopwhile loopwhile.c:5:16: error: expected ';' after top level declarator int main (void) ^ ; loopwhile.c:9:1: error: expected identifier or '(' while ( x < 100) ^ 2 errors generated.
    – Rick
    Commented Feb 9, 2017 at 0:42
  • Don't know how to post the above and get the formatting right without starting a new thread...
    – Rick
    Commented Feb 9, 2017 at 0:43
  • Quoting my answer: 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.
    – Blauelf
    Commented Feb 9, 2017 at 10:58
  • @Blauelf-- I understand that, but I'm getting an errors that says otherwise: loopwhile.c:5:16: error: expected ';' after top level declarator int main (void) ^ ;
    – Rick
    Commented Feb 9, 2017 at 15:47
0

Your incrementor needs to be within the curly braces. Also, you don't need to assign the incrementor to x, just typing x++; is sufficient to increment x by 1.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .