Aditi Garg Sharma
Aditi Garg Sharma

Reputation: 39

Syntax error reported in basic C code - what is wrong?

The code is given below.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ( int argc, char *argv[] )
{
   //FILE *fps;
   char secret[512] =" ";
   FILE *fps = fopen("/etc/comp2700/share/secret", "r");

   if(fps == NULL)
   {
       printf("Secret file not found\n");
       return 1;
   }

   fgets(secret, 512, fps);
   printf("Secret: %s\n", secret);
   fclose(fps);

   return 0;   
}

When I am trying to run this program it is repeatedly throwing the following error:

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

Upvotes: 1

Views: 1087

Answers (3)

chqrlie
chqrlie

Reputation: 144540

You cannot run your C program from the command line as ./attack1.c. Normally the shell would refuse to execute the C source file because it should not have execute permission, but for some reason, on your system, it must have the x bits and is read by the default shell as a script.

Of course this fails because attack1.c contains C code, not a command file. Note that the #include lines are interpreted as comments by the shell and the error only occurs at line 4.

To run a C program, you must first compile it to produce an executable:

gcc -Wall -o attack1 attack1.c

And then run the executable if there were no compilation errors:

./attack1

You can combine these commands as

gcc -Wall -o attack1 attack1.c && ./attack1

Upvotes: 2

Akhilesh Pandey
Akhilesh Pandey

Reputation: 896

First, you need to compile the attack.c code using the following command:

gcc attack.c

This will create one executable file a.out which you can run using the following command:

./a.out

Hope this helps you.

Upvotes: -1

Niall Cosgrove
Niall Cosgrove

Reputation: 1303

You need to compile your source file with gcc as follows

gcc -o attack attack1.c

then run it with

./attack

You should read up on the difference between compiled versus interpreted languages.

There is a short video here explaining the difference.

Upvotes: 2

Related Questions