TransWikia.com

Syntax error while running the c program despite having no problem during compiling

Stack Overflow Asked by harsh varma on December 7, 2021

I am trying to run a basic program ‘hello world’using this code

#include<stdio.h>

int main()
{ 
 printf("hello worldn");
 return 0;
}

There is no error while compiling but when i try to run the code the error is:


./hello.c: line 2: syntax error near unexpected token'('
./hello.c: line 2: 'int main()'

How can i solve this because ive tried everything and i dont think there is a syntax error.

One Answer

You don't run the .c file from the shell. After you compile the program, you run the executable that was created.

So if you compiled it like:

gcc -o hello hello.c

Then you run it with

./hello

When you use ./hello.c, the shell tries to run your C source code as a shell script. That won't work because they're completely different languages.

Answered by Barmar on December 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP