We Will Discuss About First C Program
C is a popular programming language that is widely used for system programming, application development, and more. If you’re new to programming and want to learn C, the first step is to write your first C program. In this article, we will guide you through the process of writing your first C program.
Here’s a classic first C program that prints “Hello, world!”:
#include <stdio.h>
int main() {
printf("Hello, world!n");
return 0;
}
Explanation:
#include <stdio.h>
: This line includes the standard input/output header file, which provides functions likeprintf()
for printing output.int main()
: This is the main function of the program, where execution begins. Theint
return type indicates that the function returns an integer value.printf("Hello, world!n");
: This line calls theprintf()
function to print the message “Hello, world!” to the console. Then
character adds a newline.return 0;
: This line returns the value 0 to the operating system, indicating successful program execution.
How to Compile and Run:
- Save the code: Save the code as a
.c
file (e.g.,hello.c
). - Compile: Use a C compiler like GCC (GNU Compiler Collection) to compile the code:
Bash
gcc hello.c -o hello
This will create an executable file named
hello
. - Run: Execute the compiled program:
Bash
./hello
This will output:
Hello, world!
Key Points:
- The
main
function is the entry point of a C program. printf()
is used to print output to the console.- The
n
character adds a newline to the output. - The
return 0;
statement indicates successful program execution.
To write, compile, and run your first C program, follow these steps:
Step 1: Open a text editor
Open a text editor of your choice, such as Notepad, Sublime Text, or Visual Studio Code. It will be where you write your C code.
Step 2: Write the C program
Now, copy and paste the following code into the text editor:
- #include <stdio.h>
- int main() {
- printf(“Hello, C Language”);
- return 0;
- }
Step 3: Save the file
After that, save the file with a .c extension such as first_program.c. This extension indicates that it is a C source code file.
Step 4: Compile the program
Now, compile the program in the command prompt.
Step 5: Run the program
After successful compilation, you can run the program by executing the generated executable file. Enter the following command into the terminal or command prompt:
- ./first_program
The program will execute, and you will see the output on the console:
Output:
Hello, C Language