Unexpected error while running hello world code on vs studio

2 replies · 2K views · last reply
randomgamernerd
Messages
139
Reaction score
4

Homework Statement

: [/B]Write a C program to print hello world

2. The attempt at a solution:
I am completely new to programming.
I am learning C at present.
In my college, we use linux. Since my pc has windows installed so I am using cygwin terminal to use linux commands from windows.
I am currently using visual studio.
I tried to write the first program hello world.
Screenshot of a code editor beside a terminal window. The code reads #include<stdio.h>, int main(), then printf("Hello world"); and return 0; The terminal shows gcc test.c, then ./test.c with the error: syntax error near unexpected token '(' at line 2, 'int main()'.

But could not figure out the error.
 
Attachments
  • Screenshot of a code editor beside a terminal window. The code reads #include<stdio.h>, int main(), then printf("Hello world"); and return 0; The terminal shows gcc test.c, then ./test.c with the error: syntax error near unexpected token '(' at line 2, 'int main()'.
    Capture..JPG
    17.4 KB · Views: 639
C is a compiled language. You cannot run the source code (file with the .c extension) directly. That file must be compiled into an executable file.
It has been years since I have looked at C though.

I recently took a Python class, and now am working on R for a Micromasters program. One thing that often sheds some light is "Googling the error message".

You should try it. When I did it with your error message, one of the search results that I received, is this StackOverflow post:
https://stackoverflow.com/questions/18450014/syntax-error-near-unexpected-token

In reading this, it reminded me of what is going on. The person asking this question was trying to run the .c file directly, just as you were. It appears that they give the proper steps to take for gcc.
Stackoverflow is a forum, where people ask and answer question specific to programming. Note the date on this post is from 2013, so this type of problem is not a new one.
 
Last edited:
When you compile using only
Code:
gcc test.c
then the executable produced is called "a.out".

The command should thus be
Code:
./a.out
not
Code:
./test.c

It would probably be better to compile using
Code:
gcc -o test test.c
to create an executable called "test" instead.
 
  • Like
Likes   Reactions: scottdave