marilynlov7 All American 650 Posts user info edit post |
So, I have this C program, that computes the factorial of 5. I would like it to compile without error when using gcc -Wall. I have got it down to 2 cryptic ones, "main is usually a function," and "syntax error before { token in fac." Can anyone shed light on where to look for these errors and warnings either in the code, or on Google (tried Google), or GNU or something, without telling me exactly what is wrong? The code is thus:
#include <stdio.h>
int n=5;
int fac(int)
int main(void) { printf("%d\n", fac(n)); return (0); }
fac(int k) { if(k==0) return (1); else return (k*fac(k-1)); }
Thanks9/12/2005 3:31:25 PM |
MiniMe_877 All American 4414 Posts user info edit post |
in your function prototype of fac(), you are missing a semicolon at the end of the line, like this
int fac(int); and in the function definition of fac() you are missing the return type, like so
int fac(int k) 9/12/2005 3:36:15 PM |
30thAnnZ Suspended 31803 Posts user info edit post |
i haven't written anything in C in about 10 years now and i saw that right off 9/12/2005 3:39:36 PM |
marilynlov7 All American 650 Posts user info edit post |
Yeah, its so obvious now. Thanks. 9/12/2005 3:39:55 PM |
MiniMe_877 All American 4414 Posts user info edit post |
GCC's error messages are hard to figure out sometimes
"main is usually a function" WTF is that supposed to mean? That error says nothing about the fact that the error is that you left off a semicolon from the function prototype
As a rule of thumb, check the previous line of code from where you get your errors/warnings from. Many times, the lines that actually contain the error are right before the lines that the compiler sees as errors. 9/12/2005 3:50:45 PM |
esgargs Suspended 97470 Posts user info edit post |
I dislike C/C++ for the kind of programming I do.
Java for life. 9/12/2005 4:08:29 PM |
MiniMe_877 All American 4414 Posts user info edit post |
gargs, thats completely irrelevant. You try to write code to interface directly with hardware, and you'll realize that you have to write code in C++ and interface to it through JNI in Java.
For the work you do, i'm sure Java is a suitable language. 9/12/2005 4:22:55 PM |
Spida911 All American 769 Posts user info edit post |
Man i remember when i didnt have to worry about using more than 64K of RAM for a program. *sign...those were the days 9/13/2005 3:33:43 PM |
Pantherchild Veteran 157 Posts user info edit post |
~Try removing 'void' from int main(void){...}
~Also--WTF on declaring n as a global?~ 9/13/2005 4:47:02 PM |
MiniMe_877 All American 4414 Posts user info edit post |
dude, you didnt read any of the posts, marilynlov7 got the problems fixed
and int main(void) is perfectly acceptable
and so is declaring n as a global variable, although its bad practice IMO to have any global variables unless absolutely necessary9/13/2005 4:53:48 PM |
Specter All American 6575 Posts user info edit post |
Java sucks 9/13/2005 5:05:43 PM |