Jump to content
  • Advertisement
Sign in to follow this  
CDProp

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!