C is one of the most important programming languages. It was created by Dennis Ritchie in 1972. Even though it’s an old language, C is still used today to write programs that run close to the hardware, like operating systems or games. Many modern programming languages have borrowed ideas from C.
Let’s start by looking at a simple C program:
Hello, World! in C
#include <stdio.h>
int main() {
// This program prints "Hello, World!"
printf("Hello, World!\n");
return 0;
}
Explanation:
#include <stdio.h>
: This line includes the standard library so we can use theprintf
function.int main()
: This is where the program starts.printf("Hello, World!\n");
: This command prints the message on the screen.return 0;
: This means the program ran successfully.
This simple program shows how easy it is to get started with C.
Main Features of C
- Simple and Efficient:
C is easy to learn and write. It also runs very fast, which is why it’s used for programs that need to be quick and efficient. - Portable:
C programs can run on many types of computers without needing to change the code. This makes it very flexible. - Memory Control:
C gives programmers control over memory, allowing them to manage how the computer stores and uses data. This makes C great for system-level programming.
Data Types in C
C has different types of data that it can store. These include:
#include <stdio.h>
int main() {
int age = 25; // Integer (whole number)
float height = 5.9; // Decimal number
char grade = 'A'; // Character
// Printing the values
printf("Age: %d\n", age);
printf("Height: %.2f\n", height);
printf("Grade: %c\n", grade);
return 0;
}
Explanation:
int
: Used for storing whole numbers like25
or-10
.float
: Used for decimal numbers like5.9
.char
: Used for a single character like'A'
.
Control Structures in C
C uses things like if
, else
, and loops to control how the program runs.
If-Else Example:
#include <stdio.h>
int main() {
int number = 10;
if (number > 5) {
printf("Number is greater than 5\n");
} else {
printf("Number is 5 or less\n");
}
return 0;
}
For Loop Example:
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("Count: %d\n", i);
}
return 0;
}
These are basic commands that allow the program to make decisions or repeat tasks.
Functions in C
Functions are a way to break a program into smaller pieces. Here’s an example of a function that adds two numbers:
#include <stdio.h>
// Function to add two numbers
int addNumbers(int a, int b);
int main() {
int result = addNumbers(5, 7); // Call the function
printf("Sum: %d\n", result);
return 0;
}
// Function definition
int addNumbers(int a, int b) {
return a + b; // Return the sum of a and b
}
Explanation:
addNumbers(int a, int b)
: This is a function that takes two numbers and adds them together.- Function call: In the
main
function, we calladdNumbers
and pass in two numbers (5
and7
).
Pointers in C
Pointers allow you to work directly with memory. A pointer stores the address of another variable. Here’s an example:
#include <stdio.h>
int main() {
int num = 10;
int *ptr = # // Pointer to num
printf("Value of num: %d\n", num);
printf("Address of num: %p\n", &num);
printf("Pointer value: %p\n", ptr);
printf("Value at the address pointer points to: %d\n", *ptr);
return 0;
}
Explanation:
&num
: This gives the address of the variablenum
.*ptr
: This accesses the value stored at the address the pointer is pointing to.
Where is C Used?
C is still used in many important areas:
- Operating Systems: Unix, Linux, and even Windows have been written in C.
- Embedded Systems: Many small devices, like phones or cars, use C because it’s fast and works directly with hardware.
- Game Development: Many video games and game engines use C because of its speed and control.
- Compilers: C is used to write software that compiles other programming languages.
Conclusion
C is a simple and powerful language that remains a key tool for developers. It is used to write programs that need to work quickly, directly with hardware, or control memory in a detailed way. Even though newer languages have been created, C is still widely used today.
Learning C gives you a solid foundation for programming and understanding how computers work. It also opens the door to learning other languages, as many modern programming languages, like C++ and Python, are based on C.