C Overview
C is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, with a static type system. By design, C provides constructs that map efficiently to typical machine instructions.
Key Features
- Low-Level Access: Provides direct access to memory via pointers.
- Efficiency: Code compiles to highly efficient machine instructions.
- Portability: Can be compiled for many platforms.
- Minimal Runtime: Requires minimal runtime support.
- Structured Programming: Supports functions, selection, iteration.
- Standard Library: Provides common functions like I/O, strings, math.
- Influence: Basis for many modern languages like C++, Java, C#.
Common Use Cases
System Programming
Operating systems, device drivers, firmware.
Embedded Systems
Microcontrollers, IoT devices, automotive systems.
High-Performance Computing
Scientific computing, game engines, real-time systems.
Language Development
Used to implement other programming languages.
Example Code
/* Basic C program demonstrating core concepts */
#include <stdio.h>
#include <stdlib.h>
// Function prototype
int factorial(int n);
// Structure example
struct Point {
int x;
int y;
};
int main() {
// Variables and pointers
int number = 5;
int *ptr = &number;
printf("Value: %d, Address: %p\n", number, (void*)ptr);
// Function call
printf("Factorial of %d is %d\n", number, factorial(number));
// Structure usage
struct Point p1 = {10, 20};
printf("Point coordinates: (%d, %d)\n", p1.x, p1.y);
// Dynamic memory allocation
int *arr = (int*)malloc(5 * sizeof(int));
if (arr == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return 1;
}
for (int i = 0; i < 5; i++) {
arr[i] = i * i;
}
free(arr); // Don't forget to free memory
return 0;
}
// Function definition
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}