Writing your first C program is an exciting introduction to programming in one of the most widely-used languages. C provides a solid foundation for understanding programming concepts and working with systems programming. Here are three simple examples to get you started with C programming.
The "Hello, World!" program is a classic beginner program that displays a message on the screen. Here’s how it’s done in C:
Explanation: This program includes the standard I/O library, <stdio.h>
, which contains the printf
function. The main
function is where the program begins, and printf
is used to print "Hello, World!" to the screen.
This program performs a simple addition of two numbers and displays the result. It introduces basic variable usage and arithmetic operations in C.
Explanation: This program defines two integer variables, num1
and num2
, assigns them values, and calculates their sum. The printf
function then displays the result.
This example introduces scanf
, a function that allows the program to accept user input. The program asks the user for their age and then prints it back to them.
Explanation: This program uses scanf
to read an integer from the user and store it in the variable age
. It then uses printf
to display the entered age back to the user.