Write and Run your First C Program
Hello World Program
- Open Visual Studio Code and create a new file by going to File > New File, or use the keyboard shortcut Ctrl+N (Windows) or Cmd+N (Mac) to create a new file.
- Save the file with a .c file extension, for example "hello.c". Choose a location on your computer where you want to save the file.
- Type or copy and paste the following C code into the file:
// Include Header files
#include "stdio.h" //printf, scanf etc
// Start of C application
int main()
{
// Print to Console/Terminal
printf("Hello, World\n");
// Return Success
return 0;
}
Output:
Output:
Hello, World
Running Hello World Program
- Open the terminal in Visual Studio Code by going to View > Terminal.
Compile the program
- Type the following command to compile the program using the GCC compiler:
$ gcc hello-world.c -o hello-world
hello-world
executable file gets generated
This will create an executable file named "hello".
Run the program
To run the program, type the following command and press Enter:
$ ./hello-world
Hello, World
The output of the program will be displayed in the terminal.