Home life · Updated
How to Print Hello World in C
Quick answer To print “Hello, World!” in C, write a simple program using the printf() function, compile it with a C compiler, and run the resulting executable. This is the traditional first step to learning C syntax and the build process.
What You’ll Need
- A computer (Windows, macOS, or Linux)
- A text editor (like Notepad, VS Code, or any plain text editor)
- A C compiler – GCC is recommended (install via MinGW on Windows, Xcode Command Line Tools on macOS, or your package manager on Linux)
- A terminal or command prompt
Step-by-Step
- Create a new file – Open your text editor and create a new file. Save it with the name
hello.c. The.cextension tells the compiler it’s a C source file. - Write the basic program – Type the following code exactly as shown:
#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }This includes the standard input/output library, defines the main function, prints the message, and returns 0 to indicate success.
- Save the file – Make sure you save the file with the exact name
hello.c. If your editor adds a different extension (like.txt), change it to.c. - Open a terminal – Navigate to the folder where you saved
hello.c. On Windows, you can hold Shift and right-click in the folder, then select “Open PowerShell window here” or “Open command window here”. On macOS/Linux, use thecdcommand to change directory. - Compile the program – In the terminal, type
gcc hello.c -o helloand press Enter. This tells GCC to compilehello.cand create an executable file namedhello(on Windows it will behello.exe). If there are no errors, you’ll see no output. - Run the program – Type
./helloon macOS/Linux orhello.exeon Windows and press Enter. You should see the output:
Hello, World!
That’s it! You’ve successfully printed “Hello, World!” in C.
Common Mistakes
- Missing semicolons – Every statement in C must end with a semicolon. Forgetting the semicolon after
printf(...)orreturn 0will cause a compile error. - Wrong file extension – Saving the file as
hello.txtorhello.cppwill confuse the compiler. Use.c. - Typing the code incorrectly – C is case-sensitive.
Mainis not the same asmain. Also, the include line must be#include <stdio.h>– note the angle brackets and the.h. - Forgetting to include stdio.h – Without this, the compiler doesn’t know what
printfis, and you’ll get an implicit declaration warning or error. - Using the wrong compile command – If you type
gcc hellowithout the output flag, the compiler may produce a defaulta.outfile, which is fine but can be confusing. Stick with the-oflag.
Tips
- Use a modern editor – Editors like VS Code or Notepad++ highlight syntax and help you spot typos. Avoid word processors like Microsoft Word that add hidden formatting.
- Understand the code –
#include <stdio.h>brings in the standard I/O functions.int main()is the entry point.printfprints to the screen.\nis a newline character.return 0tells the OS the program ended successfully. - Experiment – Change the text inside the quotes to something else, add a second
printfline, or remove the\nto see how output changes. - Check your compiler installation – Run
gcc --versionin the terminal to confirm GCC is installed. If you get “command not found”, you need to install it first. - Use online compilers for quick tests – If you don’t want to install anything, websites like OnlineGDB or Replit allow you to run C code in your browser.
Troubleshooting
“gcc is not recognized” or “command not found” – This means GCC is not installed or not in your PATH. On Windows, install MinGW-w64 and add its bin folder to your system PATH. On macOS, run xcode-select --install to get the command line tools. On Linux, use your package manager (e.g., sudo apt install gcc on Debian/Ubuntu).
Compile errors like “expected ‘;’” – Check the line mentioned in the error message. You likely missed a semicolon or have a typo. Read the error carefully; it often points to the exact spot.
The program compiles but nothing prints – If you run the executable and see no output, make sure you used printf("Hello, World!\n"); with the correct quotes and backslash. Also, check that you’re running the correct executable file – if you compiled to hello, don’t run a.out.
“undefined reference to ‘main’” – This means the compiler can’t find the main function. Make sure you typed int main() exactly, and that the file you’re compiling is the one with the code.
Output shows on the same line – If you want a newline, include \n inside the string. Without it, the next prompt will appear right after your text.
Frequently Asked Questions
Why do we use #include <stdio.h>?
This line tells the compiler to include the standard input/output header file, which declares functions like printf and scanf. Without it, the compiler doesn’t know what printf is.
What does int main() mean?
It’s the function where your program starts executing. int means the function returns an integer value. The return 0; at the end returns 0 to the operating system, indicating successful completion.
Can I use void main() instead?
Some old compilers allow it, but it’s not standard C. The C standard says main should return int. Using void main() can cause warnings or errors on modern compilers.
What is the difference between printf and puts?
printf allows formatted output (like variables), while puts simply prints a string and adds a newline. For “Hello, World!”, you could use puts("Hello, World!"); and it would work too.
Why do I get an error about “implicit declaration of function printf”?
This happens when you forget #include <stdio.h>. The compiler doesn’t know the prototype of printf. Add the include line at the top.
How do I compile on Windows with Visual Studio?
If you’re using Visual Studio, you can create a new C project and write the code in a .c file. Press Ctrl+F5 to build and run. The menu paths may differ slightly by version, but the process is similar.
Related
- How to Wash Hoka Sneakers: Step-by-Step Guide
- How to Wash Hats Without Ruining Them
- How to Whiten Socks: Easy Steps for Bright Whites
Sources: GCC Documentation. Menus and buttons can differ slightly by version. Spotted something out of date? Tell us.