Error compiling and running C program on Mac terminal

896 views Asked by At

I'm having a bit of trouble compiling and running my .c file in terminal. First, when compiling, I see:

HW3.c: In function ‘main’:
HW3.c:87:5: error: ‘for’ loop initial declarations are only allowed in C99 mode
 for(int j = 0; j < 10; j++) {
 ^
HW3.c:87:5: note: use option -std=c99 or -std=gnu99 to compile your code
HW3.c:100:5: error: ‘for’ loop initial declarations are only allowed in C99 mode
 for(int j = 0; j < 10; j++) {
 ^

All of my variables are declared and assigned at the beginning of the program, including j, so I'm not sure why I'm seeing an error about 'for' loop initial declarations.

Secondly, when attempting to run my program, I type:

./a.out HW3.c

and see error

./a.out: Command not found.

What could possibly be the issue here? Is it not running because of the error in compiling? I'm sure I have the command right, right..? Let me know if you need to see the whole program to help, it's not too long, I could copy it over. Thanks!

2

There are 2 answers

3
Mac O'Brien On

You need to add a more recent C standard revision to your compiler options. Try adding the flag --std=c99 and it should work.

As for your second problem, a.out is the executable produced by the compiler. If there are errors in the program, it won't produce an executable, so you have to fix the errors.

You can also specify the name of the executable with the -o flag:

gcc -std=c99 HW3.c -o HW3

This will produce an executable named HW3.

1
Greg Hewgill On

If j is already declared at the beginning of the program, then remove the int part of for (int j:

 for(j = 0; j < 10; j++) {

You can declare j inside the for loop, as you seem to have attempted to do, but you would need to tell your compiler to support a newer revision of the C standard.