C programming is one of the most widely used and foundational programming languages. Developed in the 1970s by Dennis Ritchie at Bell Labs, C has influenced many other modern programming languages, including C++, Java, and Python. It's a general-purpose language that provides a good balance between high-level functionality and low-level memory manipulation.
C is commonly used for system-level programming (e.g., operating systems, embedded systems) due to its close proximity to hardware and its efficient performance.
A simple C program typically starts with the inclusion of necessary header files, followed by the definition of a main() function. The main() function is the entry point of any C program, and it’s where the program starts its execution.
Below are two very simple tasks often used in C programming for beginners:
Printing text or outputting information to the console is one of the most fundamental actions in any programming language. In C, you can use the printf() function to display text or values.
Here’s an example of a C program that prints a message:
#include <stdio.h> // Preprocessor directive to include standard I/O library
int main() {
// Print a message to the screen
printf("Hello, World!\n");
return 0; // Return 0 to indicate successful execution
}
#include <stdio.h>: This line includes the standard input/output library which contains the printf() function, used for printing text to the screen.main() function: Every C program starts execution from the main() function.printf() function: This function is used to print the output. The text "Hello, World!" is displayed on the screen. The \n at the end of the string is a newline character, which moves the cursor to the next line after printing.return 0;: This line returns 0 from the main() function, indicating the program finished successfully.Hello, World!
Now let’s write a simple program to add two integers and print the result. In this program, we will:
#include <stdio.h> // Include the standard input/output library
int main() {
int num1, num2, sum; // Declare three integer variables: num1, num2, and sum
// Prompt the user for input
printf("Enter first integer: ");
scanf("%d", &num1); // Read the first integer from the user
printf("Enter second integer: ");
scanf("%d", &num2); // Read the second integer from the user
sum = num1 + num2; // Add the two integers and store the result in 'sum'
// Output the result
printf("The sum of %d and %d is %d\n", num1, num2, sum);
return 0; // Indicate that the program has ended successfully
}
int num1, num2, sum;: Declares three integer variables. num1 and num2 will store the two input integers, and sum will store their sum.scanf("%d", &num1);: This function reads the user's input and stores it in the variable num1. %d is the format specifier for integers.sum = num1 + num2;: This performs the addition of the two integers and stores the result in the sum variable.printf("The sum of %d and %d is %d\n", num1, num2, sum);: This prints the result of the addition using the printf() function. The format specifiers %d are replaced with the values of num1, num2, and sum.Enter first integer: 5
Enter second integer: 10
The sum of 5 and 10 is 15
scanf() function to accept input from the user and stores the values in num1 and num2.sum variable.printf() function, displaying the result to the user.#include <stdio.h> includes the standard input/output library necessary for using printf() and scanf().num1, num2, and sum) to hold the values.scanf() reads input from the user, and printf() displays output to the user.+) to add the two integers.return 0; signals the successful completion of the program.This is the foundation of C programming — working with input, output, and arithmetic operations to create simple programs.
Open this section to load past papers