Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beginner: syntax error before int main ()

Tags:

c

I'm trying to run a Hello World program but am getting the error

./ex1.c: line 3: syntax error near unexpected token `(`
./ex1.c: line 3: `int main (int argc, char *argv[])'

or

./ex1.c: 3: ./ex1.c: Syntax error: "(" unexpected

or

./ex1.c:3: unknown file attribute: i
./ex1.c:4: parse error near `}'

The weird thing is I've run this same program before and had no issues.

Not sure if these issues are related but the problem happened after I installed Valgrind to run exercise 4 in Learn C The Hard Way. I received an error that said permission denied which I fixed using chmod +x. Then all my .c files needed permission which they had not before. I then did chmod -R 0777 for the directory with all of my .c practice files. So the permission problem is fixed but then the error above started. They may be completed unrelated but wanted to include just in case.

like image 407
MParker Avatar asked Oct 11 '12 02:10

MParker


1 Answers

You can't run a .c file just by using ./ex1.c; you have to compile it into a runnable program first.

Assuming you have a Linux/OS X machine, use gcc -Wall ex1.c -o ex1 to compile it (or, more simply, make ex1). Then you can ./ex1 to run the program.

like image 169
nneonneo Avatar answered Nov 15 '22 14:11

nneonneo