What's wrong with this C code?

Caporegime
Joined
12 Mar 2004
Posts
29,962
Location
England
Code:
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char * argv[]) {
	if (argv[0] == "" || argv[1] == "" || argv[2] == "") {
		printf("ERROR! Missing command line arguments");
	} else {
		int graduationage = graduateAge(atoi(argv[2]));
		printf("Name: %s\nAge: %s\nAge upon graduation: %d\n", argv[1], argv[2], graduationage);

		int i;
		for (i = 0; i < graduationage; i++) {
			printf("%s\n", argv[1]);
		}
	}
	system("pause");
        return 0;
} 

int graduateAge(int age) {
	return age + 2;
}

Keeps saying "i" is undeclared. :mad:
 
Last edited:
Could you not take the int declaration on line 11 and move it to within the 'for' on line 12?

e.g.

for (int i = 0; i < graduationage; i++)
 
You have to declare it in the way he has already you wouldnt be allowed to declare i on the for line. You should check the number of arguments passed before you start indexing into argv[]. You should cleanall and then rebuild as the above sample compiles fine on my system.
 
C likes it's declarations first within a block.

So move "int i;" either into the for(int index=0; ... or at the start where:
...} else {
int i, graduationAge;
... stuff
}
 
C likes it's declarations first within a block.

So move "int i;" either into the for(int index=0; ... or at the start where:
...} else {
int i, graduationAge;
... stuff
}

Some variations of C don't allow you to declare a variable inside a for statement. i.e. You can't do for(int i=0 ; i<10 ; i++)

Your best bet is to move the int i; or you could put braces around the int i and the whole of the for loop after it.
 
doing this: for(int i = 0; etc..) is C++, not C.

declare int i = 0; either at the top of the file or at the top of main()

This all depends on the version of C, ANSI C99 does allow the C++ for (int i = 0;...) style, earlier versions and K&R C doesn't.

And this I think is the issue the OP is having, namely that whatever compiler used doesn't like the declartion of i after a printf, which woud imply it's a C90 or K&R C standard,

So moving the declation of i to the start of the block or start of main() should fix it...
 
This all depends on the version of C, ANSI C99 does allow the C++ for (int i = 0;...) style, earlier versions and K&R C doesn't.

And this I think is the issue the OP is having, namely that whatever compiler used doesn't like the declartion of i after a printf, which woud imply it's a C90 or K&R C standard,

So moving the declation of i to the start of the block or start of main() should fix it...

This. It did throw me of a few times in the past too.
 
Thanks everyone, moving it to the start of the block fixed it. The program I am using is Microsoft Visual Studio 2008 which is awful but we can't use any other program on the uni pc's like devc++.
 
The problem is visual studio has caused a number of problems with dissappearing files. Does it really matter if it's a little out of date when using a language decades old?
 
Code:
snip

Ahhhh it burns!

Ahem, rule of thumb I always tell people is declare all variables at the start of the function, or (where possible) start of the statement, its just easier than going through code at 5am to find a variable if its at a unified location.

As for C being old, I use C daily for my OS development, its old but its still the OS language of choice ;)
 
Ahhhh it burns!

Ahem, rule of thumb I always tell people is declare all variables at the start of the function, or (where possible) start of the statement, its just easier than going through code at 5am to find a variable if its at a unified location.

As for C being old, I use C daily for my OS development, its old but its still the OS language of choice ;)


It really really shouldn't be, unless you work in one of the handful of areas that need it. If we all acted like that we'd still be writing asmembler
 
Also, why are you checking to see if argv[0], argv[1] and argv[2] are empty?

argv[0] will never be empty - it's the executable you are running - and if either argv[1] or argv[2] are empty argc will be less than three and therefore the wrong number of arguments.
 
It really really shouldn't be, unless you work in one of the handful of areas that need it. If we all acted like that we'd still be writing asmembler

I'm not saying its the best choice by a long stretch, but it is the standard language for OS development, some people use C++, some C#, some BASIC >.> C just happens to be the most popular for a varity of reasons. And we still write in ASM for various parts of the kernel.
 
Also, why are you checking to see if argv[0], argv[1] and argv[2] are empty?

argv[0] will never be empty - it's the executable you are running - and if either argv[1] or argv[2] are empty argc will be less than three and therefore the wrong number of arguments.

I removed it later, it was just for some diagnostic thing that I forgot to clean up.
 
Microsoft Visual Studio 2008 has one really nice feature... bookmarks... you can bookmark and group all your TODO, FIXME, etc. and references in the code that could be useful for implementing other features, etc.


Is there any reason for the int main/return 0 btw? - personally I've always just used void main.
 
Last edited:
Back
Top Bottom