Writing and running your first C program, often referred to as the "Hello, World!" program, is a simple yet essential step in learning C programming. Here's a step-by-step guide on how to do it:
Install a C Compiler: Before you can write and run a C program, you need a C compiler. If you are using a Linux-based system, you probably already have one installed (e.g., GCC). If you're using Windows, you can use a compiler like MinGW or install an Integrated Development Environment (IDE) like Code::Blocks or Dev-C++. Alternatively, you can use an online C compiler for a quick start.
Open a Text Editor: You can use any text editor to write your C program. Common choices include Notepad (Windows), Visual Studio Code, Sublime Text, and Vim (Linux), or any code-specific IDE like Code::Blocks or Dev-C++.
Write the C Program: In your text editor, write the following simple C program:
c#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }
This program includes the standard I/O library
<stdio.h
and defines amain()
function. Inside themain()
function, it uses theprintf
function to display the "Hello, World!" message and then returns 0 to indicate successful execution.Save the Program: Save your C program with an
.c
extension. For example, you can save it ashello.c
.Compile the Program: Open a command prompt or terminal, navigate to the directory where you saved your C program, and compile it using your C compiler. If you're using GCC on the command line, you can compile the program like this:
gcc hello.c -o hello
This command tells GCC to compile
hello.c
and generate an executable namedhello
. The-o
flag specifies the output file name.Run the Program: After successfully compiling the program, you can run it by entering:
bash./hello
If you're on Windows and using MinGW, you can just type
hello
without the./
.Output: You should see the output on the screen:
Hello, World!
Congratulations! You have just written and run your first C program. The "Hello, World!" program is the simplest C program and serves as a great starting point for learning the C programming language. You can build on this foundation to create more complex programs and explore the rich features of the C language.