Introduction to C
C is a powerful, general-purpose programming language that's been influential in modern computing—forming the foundation of many operating systems, embedded systems, and performance-critical applications.
- Procedural and low-level access to memory
- Compiled for fast execution
- Used in system software, game development, IoT
Example 1: Hello World
#include
int main() {
printf("Hello, World!\\n");
return 0;
}
Run Code on OneCompiler
Explanation: This basic program uses printf from stdio.h to display a message. Every C program starts with main().
Example 2: Addition
#include
int main() {
int a = 5, b = 3;
printf("Sum = %d\\n", a + b);
return 0;
}
Run Code on OneCompiler
Explanation: Declares two integers, adds them, and prints the result using %d format specifier.
Example 3: Loop
#include
int main() {
for(int i = 1; i <= 5; i++) {
printf("%d ", i);
}
printf("\\n");
return 0;
}
Run Code on OneCompiler
Explanation: A for loop prints numbers from 1 to 5, showcasing iteration in C.
Example 4: Function
#include
int multiply(int x, int y) {
return x * y;
}
int main() {
int res = multiply(4, 5);
printf("Product = %d\\n", res);
return 0;
}
Run Code on OneCompiler
Explanation: Defines a function multiply, calls it in main(), and prints the returned result.