Unexpected error while running hello world code on vs studio

Click For Summary
SUMMARY

The discussion centers on resolving an error encountered while attempting to run a "Hello World" C program using Visual Studio on Windows with Cygwin. The user mistakenly tried to execute the source file directly instead of compiling it first. The correct approach involves using the GCC compiler to compile the C file into an executable, which can be run with the command './a.out' or by specifying an output name with 'gcc -o test test.c'. This method ensures successful execution of the program.

PREREQUISITES
  • Basic understanding of C programming syntax and structure
  • Familiarity with GCC (GNU Compiler Collection) for compiling C code
  • Knowledge of command-line operations in Cygwin or similar terminal environments
  • Experience with Visual Studio for code editing and project management
NEXT STEPS
  • Learn how to use GCC for compiling C programs effectively
  • Research command-line operations in Cygwin for better terminal usage
  • Explore error handling and debugging techniques in C programming
  • Study best practices for writing and organizing C code in Visual Studio
USEFUL FOR

Beginner programmers, students learning C, and anyone troubleshooting compilation errors in C using Visual Studio and 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: 608
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   Reactions: scottdave

Similar threads

  • · Replies 21 ·
Replies
21
Views
3K
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
3
Views
3K
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K