Unexpected error while running hello world code on vs studio

AI Thread Summary
To run a C program in Visual Studio, the source code must be compiled into an executable file, as C is a compiled language. Users often encounter errors when attempting to run the .c file directly instead of the compiled executable. The correct command to compile is "gcc test.c," which generates an executable named "a.out" by default, or "gcc -o test test.c" to create a custom executable name. After compilation, the executable can be run using "./a.out" or "./test," respectively. Understanding these steps is crucial for successfully executing C programs in a Windows environment using Cygwin.
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.
Capture..JPG

But could not figure out the error.
 

Attachments

  • Capture..JPG
    Capture..JPG
    17.4 KB · Views: 584
Physics news on Phys.org
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 scottdave
Back
Top