C
Programming Language
C Language
Overview
C is a procedural programming language developed by Dennis Ritchie in 1972. Designed as the foundation of systems programming, it is widely used in OS development, embedded systems, and high-performance application development. It is an influential language that became the foundation for many modern programming languages.
Details
C provides the ability to control at a level close to hardware while maintaining the usability of a high-level language. It features direct memory access through pointers, efficient code generation, and high portability.
Starting from its use in Unix OS development, it continues to be utilized in fields where performance is critical, such as Linux kernel, embedded systems, device drivers, and database engines. Standards have been continuously updated with C99, C11, and C18.
Code Examples
Hello World
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Variables and Data Types
#include <stdio.h>
int main() {
// Basic data types
int age = 25;
float height = 175.5f;
double weight = 70.5;
char grade = 'A';
char name[] = "John";
// Array
int numbers[5] = {1, 2, 3, 4, 5};
printf("Name: %s\n", name);
printf("Age: %d years old\n", age);
printf("Height: %.1f cm\n", height);
printf("Weight: %.1f kg\n", weight);
printf("Grade: %c\n", grade);
// Display array
printf("Numbers: ");
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
Functions
#include <stdio.h>
// Function prototypes
int add(int a, int b);
int factorial(int n);
void printArray(int arr[], int size);
int main() {
int x = 5, y = 3;
int sum = add(x, y);
printf("%d + %d = %d\n", x, y, sum);
int n = 5;
printf("%d! = %d\n", n, factorial(n));
int numbers[] = {10, 20, 30, 40, 50};
printArray(numbers, 5);
return 0;
}
// Function to add two numbers
int add(int a, int b) {
return a + b;
}
// Function to calculate factorial (recursive)
int factorial(int n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
// Function to display array
void printArray(int arr[], int size) {
printf("Array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
Pointers and Memory Management
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
// Basic pointers
int value = 42;
int *ptr = &value;
printf("Value: %d\n", value);
printf("Address: %p\n", (void*)&value);
printf("Value through pointer: %d\n", *ptr);
// Dynamic memory allocation
int *dynamicArray = (int*)malloc(5 * sizeof(int));
if (dynamicArray == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Set values in array
for (int i = 0; i < 5; i++) {
dynamicArray[i] = (i + 1) * 10;
}
// Display array
printf("Dynamic array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", dynamicArray[i]);
}
printf("\n");
// Free memory
free(dynamicArray);
// Strings and pointers
char *str = (char*)malloc(50 * sizeof(char));
if (str != NULL) {
strcpy(str, "Hello, C Language!");
printf("String: %s\n", str);
free(str);
}
return 0;
}
Structures
#include <stdio.h>
#include <string.h>
#include <math.h>
// Structure definition
struct Person {
char name[50];
int age;
float height;
};
// Type definition using typedef
typedef struct {
int x;
int y;
} Point;
// Function that takes structure as argument
void printPerson(struct Person p) {
printf("Name: %s, Age: %d, Height: %.1fcm\n",
p.name, p.age, p.height);
}
// Function that takes structure pointer as argument
void updateAge(struct Person *p, int newAge) {
p->age = newAge;
}
// Function to calculate distance
float distance(Point p1, Point p2) {
int dx = p2.x - p1.x;
int dy = p2.y - p1.y;
return sqrt(dx * dx + dy * dy);
}
int main() {
// Structure initialization
struct Person person1 = {"John", 25, 175.5};
struct Person person2;
// Set values individually
strcpy(person2.name, "Jane");
person2.age = 23;
person2.height = 162.0;
printPerson(person1);
printPerson(person2);
// Update age
updateAge(&person1, 26);
printf("After update: ");
printPerson(person1);
// Using Point structure
Point p1 = {0, 0};
Point p2 = {3, 4};
printf("Distance between (%d, %d) and (%d, %d): %.2f\n",
p1.x, p1.y, p2.x, p2.y, distance(p1, p2));
return 0;
}
File Input/Output
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file;
char filename[] = "sample.txt";
char buffer[100];
// Write to file
file = fopen(filename, "w");
if (file == NULL) {
printf("Could not open file: %s\n", filename);
return 1;
}
fprintf(file, "This is a C language sample.\n");
fprintf(file, "Example of file input/output.\n");
fprintf(file, "Number: %d\n", 42);
fclose(file);
printf("Written to file: %s\n", filename);
// Read from file
file = fopen(filename, "r");
if (file == NULL) {
printf("Could not open file: %s\n", filename);
return 1;
}
printf("\nFile contents:\n");
while (fgets(buffer, sizeof(buffer), file) != NULL) {
printf("%s", buffer);
}
fclose(file);
return 0;
}
Preprocessor and Macros
#include <stdio.h>
// Macro definitions
#define PI 3.14159
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define SQUARE(x) ((x) * (x))
// Conditional compilation
#ifdef DEBUG
#define DBG_PRINT(x) printf("DEBUG: %s\n", x)
#else
#define DBG_PRINT(x)
#endif
int main() {
// Using macros
float radius = 5.0;
float area = PI * SQUARE(radius);
printf("Area of circle with radius %.1f: %.2f\n", radius, area);
int a = 10, b = 20;
printf("MAX(%d, %d) = %d\n", a, b, MAX(a, b));
// Debug output (only if DEBUG is defined)
DBG_PRINT("Program is running normally");
// Compiler information
printf("Compilation date/time: %s %s\n", __DATE__, __TIME__);
printf("File name: %s\n", __FILE__);
printf("Line number: %d\n", __LINE__);
return 0;
}
Advantages and Disadvantages
Advantages
- High Performance: Efficient code generation and memory usage
- Hardware Control: Low-level hardware access capability
- Portability: Available on many platforms
- Compact: Simple with minimal necessary features
- Influence: Foundation for many languages
- Rich Documentation: Abundant learning resources accumulated over years
Disadvantages
- Memory Management: Manual memory management required
- Safety: Security risks like buffer overflow
- Learning Curve: Understanding pointers and low-level concepts required
- Development Efficiency: Longer development time compared to high-level languages
- String Processing: Cumbersome string manipulation
Key Links
Ranking Information
- Overall Ranking: 8th
- TIOBE Index: 3rd
- PYPL PopularitY: 4th
- GitHub Usage: 10th
- RedMonk Language Ranking: 10th
- IEEE Spectrum: 9th
- JetBrains Developer Survey: 10th
C is an important language that serves as the foundation for many programming languages and systems as the basis of systems programming.