144 views
First C Program
Creating your first C program is a simple and exciting way to get started with C programming. Here’s a step-by-step guide on how to create and run a basic “Hello, World!” program in C:
- Install a C Compiler:
- Before you can write and run C programs, you need a C compiler. Popular C compilers include GCC (GNU Compiler Collection), Clang, and Microsoft Visual C++ (for Windows). Make sure you have a C compiler installed on your system.
- Open a Text Editor:
- You can use any text editor to write your C code. Common text editors include Notepad (Windows), Visual Studio Code, Sublime Text, Atom, and many others.
- Write Your C Code:
- Open your text editor and create a new file. Write the following C code to print “Hello, World!” to the console:
C
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
- This program includes the
stdio.h
header for input and output functions and defines amain
function, which is the entry point of the program. Theprintf
function is used to display “Hello, World!” on the console.
- Save Your C File:
- Save the file with a
.c
extension. For example, you can name ithello.c
.
- Compile Your C Program:
- Open your command prompt or terminal.
- Navigate to the directory where you saved your
hello.c
file using thecd
command (change directory). - Compile your program using the C compiler. For GCC, you can use the following command:
Bash
gcc hello.c -o hello
- This command tells GCC to compile
hello.c
and generate an executable namedhello
(you can choose a different name if you prefer).
- Run Your C Program:
- After a successful compilation, you can run your program using the following command:
Bash
./hello
- You should see the output on your console:
Plaintext
Hello, World!
Congratulations! You’ve just created and run your first C program. This “Hello, World!” program is a simple introduction to C programming and serves as the starting point for more complex projects and learning opportunities in the C language.