Public Group

# C - For loops not working?

This topic is 3586 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Arrgh! I usually use C++, but right now I'm trying to compile a file in C and I'm getting a bunch of errors. I've narrowed it down to the for loops. I created a separate .c file that contains nothing but the following:
#include "stdio.h"

int main()
{
int retVal = 1;

for(int pow = 0; pow < 8; pow++)
retVal *= retVal;

return retVal;
}


And I get a whole slew of errors from this: error C2143: syntax error : missing ';' before 'type' error C2143: syntax error : missing ';' before 'type' error C2143: syntax error : missing ')' before 'type' error C2143: syntax error : missing ';' before 'type' error C2065: 'pow' : undeclared identifier warning C4552: '<' : operator has no effect; expected operator with side-effect error C2059: syntax error : ')' error C2146: syntax error : missing ';' before identifier 'retVal' I am very confused. I tried declaring "pow" before the loop, instead of doing so inside the initialization section, and that seemed to clear the error up. But I get this same error in other instances, too. For instance, it seems that any variables I declare after a call to printf() or puts() causes the same error.
#include "stdio.h"

int main()
{
puts("Hello!");

int retVal = 1;

return retVal;
}


I am very confused! I think my program is valid C, and I am hoping I just have some compiler options wrong, but I can't be sure unless I can compile it!

##### Share on other sites
I think you need to declare int outside of the for loop in C.

int pow;for(pow = 0; pow < 8; pow++)     retVal *= retVal;

##### Share on other sites
Well, in C you must declare your local variables at the beginning of the function or a scope. You can't declare them after a instruction, or in a for cycle's header.

##### Share on other sites
Well, I'm no C expert either, but I thought that you have to declare your variables first.

Try something like this:

int main()
{
int retVal = 1;
int pow = 0;

for(pow = 0; pow < 8; pow++)
retVal *= retVal;

return retVal;
}

--
Ashaman

##### Share on other sites
Wow, I had no idea. Thanks!

##### Share on other sites
check Pelles C

http://smorgasbordet.com/pellesc/

you can write loops like that without problems...

##### Share on other sites
If you don't use C99 you'll have to define the variable before using it in a for loop. And also, you probably should rename your variable pow to something since it's the name of a function defined in math.h.

##### Share on other sites
Don't you also have to use braces if you are putting your single line for loop on two lines:

Either:

for(int pow = 0; pow < 8; pow++) retVal *= retVal;

or:

for(int pow = 0; pow < 8; pow++) {
retVal *= retVal;
}

Also, I've found that some compilers like you to always use:

return(value);

##### Share on other sites
Quote:
 Don't you also have to use braces if you are putting your single line for loop on two lines:Either:for(int pow = 0; pow < 8; pow++) retVal *= retVal;or:for(int pow = 0; pow < 8; pow++) {retVal *= retVal;}

You don't have to use braces in either case, since "retVal *= reVal;" is a single statement.

Quote:
 Also, I've found that some compilers like you to always use:return(value);

Those compilers are broken.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633297
• Total Posts
3011251
• ### Who's Online (See full list)

There are no registered users currently online

×