What Is C Programming?
C is a general-purpose, procedural programming language developed by Dennis Ritchie at Bell Labs in 1972. It was designed to rewrite the UNIX operating system and has since become one of the most influential languages in computing history.
C gives you direct access to memory through pointers, allows low-level bit manipulation, and compiles to highly efficient machine code. This combination of power and efficiency is why C remains the language of choice for operating systems, embedded devices, and performance-critical software more than 50 years after its creation.
Why Learn C?
Learning C is not just about mastering a language — it is about understanding how computers work at a fundamental level. Here is why C matters:
- Foundation for other languages — Python, JavaScript, Java, C++, Go, and Rust all borrow concepts from C. Understanding C makes learning them significantly easier.
- Operating systems — The Linux kernel, Windows kernel, and macOS are all written primarily in C. If you want to work on systems programming, C is non-negotiable.
- Embedded systems — Microcontrollers in your car, microwave, smartphone, and IoT devices run C code. The embedded industry is dominated by C.
- Performance — C compiles to native machine code with minimal runtime overhead. When every microsecond counts, C delivers.
- Interview preparation — Technical interviews at top companies test C concepts like pointers, memory management, and data structures.
- Open source ecosystem — MySQL, PostgreSQL, Redis, nginx, Git, Docker, and thousands of other critical tools are written in C.
C Program Structure
Every C program follows a consistent structure. Here is the simplest possible C program:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}Key elements:
#include <stdio.h>— Preprocessor directive that includes the Standard Input/Output libraryint main()— Entry point of every C program. Execution starts here.printf()— Function to print output to the consolereturn 0— Indicates the program executed successfully
Variables and Data Types
C is a statically typed language — you must declare the type of every variable before using it. Here are the fundamental data types:
#include <stdio.h>
int main() {
// Integer types
int age = 25;
long big_number = 1000000L;
short small = 10;
unsigned int positive = 42;
// Floating-point types
float pi = 3.14f;
double precise_pi = 3.141592653589793;
// Character type
char letter = 'A';
// String (array of characters)
char name[] = "CodePractise";
printf("Age: %d\n", age);
printf("Pi: %f\n", pi);
printf("Letter: %c\n", letter);
printf("Name: %s\n", name);
printf("Size of int: %lu bytes\n", sizeof(int));
printf("Size of double: %lu bytes\n", sizeof(double));
return 0;
}| Type | Size | Range | Format Specifier |
|---|---|---|---|
| char | 1 byte | -128 to 127 | %c |
| int | 4 bytes | -2^31 to 2^31-1 | %d |
| float | 4 bytes | ±3.4e38 | %f |
| double | 8 bytes | ±1.7e308 | %lf |
| long long | 8 bytes | ±9.2e18 | %lld |
Control Flow
C provides standard control flow structures — if/else, switch, and loops:
If-Else and Switch
#include <stdio.h>
int main() {
int score = 85;
// If-else
if (score >= 90) {
printf("Grade: A\n");
} else if (score >= 80) {
printf("Grade: B\n");
} else if (score >= 70) {
printf("Grade: C\n");
} else {
printf("Grade: F\n");
}
// Switch statement
int day = 3;
switch (day) {
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
case 4: printf("Thursday\n"); break;
case 5: printf("Friday\n"); break;
default: printf("Weekend\n"); break;
}
return 0;
}Loops
#include <stdio.h>
int main() {
// For loop — print 1 to 10
for (int i = 1; i <= 10; i++) {
printf("%d ", i);
}
printf("\n");
// While loop — sum of even numbers
int sum = 0, i = 1;
while (i <= 100) {
if (i % 2 == 0) sum += i;
i++;
}
printf("Sum of even numbers 1-100: %d\n", sum);
// Do-while loop
int count = 0;
do {
printf("Count: %d\n", count);
count++;
} while (count < 5);
return 0;
}Functions
Functions in C allow you to organize code into reusable blocks. Every function has a return type, a name, parameters, and a body:
#include <stdio.h>
// Function declarations
int add(int a, int b);
float calculate_area(float radius);
void print_array(int arr[], int size);
int main() {
printf("3 + 5 = %d\n", add(3, 5));
printf("Area of circle (r=5): %.2f\n", calculate_area(5.0));
int numbers[] = {10, 20, 30, 40, 50};
print_array(numbers, 5);
return 0;
}
int add(int a, int b) {
return a + b;
}
float calculate_area(float radius) {
return 3.14159f * radius * radius;
}
void print_array(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}
}Arrays and Strings
Arrays store collections of the same data type. Strings in C are null-terminated character arrays:
#include <stdio.h>
#include <string.h>
int main() {
// Arrays
int scores[] = {95, 87, 72, 91, 68};
int length = sizeof(scores) / sizeof(scores[0]);
printf("Scores: ");
for (int i = 0; i < length; i++) {
printf("%d ", scores[i]);
}
printf("\n");
// Find max and min
int max = scores[0], min = scores[0];
for (int i = 1; i < length; i++) {
if (scores[i] > max) max = scores[i];
if (scores[i] < min) min = scores[i];
}
printf("Max: %d, Min: %d\n", max, min);
// Strings
char greeting[] = "Hello";
char target[] = "World";
char full[50];
strcpy(full, greeting);
strcat(full, " ");
strcat(full, target);
printf("Combined: %s\n", full);
printf("Length: %lu\n", strlen(full));
return 0;
}Pointers — The Heart of C
Pointers are variables that store memory addresses. They are the most powerful (and most confusing) feature of C. Mastering pointers is essential for dynamic memory allocation, data structures, and system programming.
#include <stdio.h>
int main() {
int x = 42;
int *ptr = &x; // ptr stores the address of x
printf("Value of x: %d\n", x);
printf("Address of x: %p\n", (void *)&x);
printf("Value of ptr: %p\n", (void *)ptr);
printf("Dereferenced ptr: %d\n", *ptr);
// Modify value through pointer
*ptr = 100;
printf("Modified x: %d\n", x); // x is now 100
// Pointer arithmetic
int arr[] = {10, 20, 30, 40, 50};
int *p = arr;
for (int i = 0; i < 5; i++) {
printf("arr[%d] = %d (address: %p)\n", i, *(p + i), (void *)(p + i));
}
return 0;
}Dynamic Memory Allocation
C provides functions to allocate memory at runtime. This is essential for data structures like linked lists, trees, and graphs:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("How many numbers? ");
scanf("%d", &n);
// Allocate array dynamically
int *arr = (int *)malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
// Fill and display
for (int i = 0; i < n; i++) {
arr[i] = (i + 1) * 10;
}
for (int i = 0; i < n; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}
// Reallocate to bigger array
arr = (int *)realloc(arr, (n + 5) * sizeof(int));
for (int i = n; i < n + 5; i++) {
arr[i] = (i + 1) * 10;
}
printf("After realloc:\n");
for (int i = 0; i < n + 5; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}
// Free memory — always do this!
free(arr);
arr = NULL;
return 0;
}Structs and Enums
Structs let you create custom data types by grouping related variables:
#include <stdio.h>
#include <string.h>
struct Student {
char name[50];
int age;
float gpa;
};
struct Point {
int x, y;
};
float distance(struct Point a, struct Point b) {
int dx = a.x - b.x;
int dy = a.y - b.y;
return sqrt(dx * dx + dy * dy);
}
int main() {
struct Student s1;
strcpy(s1.name, "Alice");
s1.age = 20;
s1.gpa = 3.85;
printf("Name: %s, Age: %d, GPA: %.2f\n", s1.name, s1.age, s1.gpa);
// Struct initializer
struct Student s2 = {"Bob", 22, 3.92};
printf("Name: %s, Age: %d, GPA: %.2f\n", s2.name, s2.age, s2.gpa);
return 0;
}File I/O
C provides a complete file handling API for reading and writing files:
#include <stdio.h>
int main() {
// Write to file
FILE *fp = fopen("data.txt", "w");
if (fp == NULL) {
printf("Error opening file!\n");
return 1;
}
fprintf(fp, "Name: CodePractise\n");
fprintf(fp, "Language: C\n");
fprintf(fp, "Rating: 5/5\n");
fclose(fp);
// Read from file
fp = fopen("data.txt", "r");
if (fp == NULL) {
printf("Error opening file!\n");
return 1;
}
char line[100];
while (fgets(line, sizeof(line), fp)) {
printf("%s", line);
}
fclose(fp);
return 0;
}Data Structures in C
C does not have built-in data structures, so you implement them from scratch. This is one of the best ways to truly understand how data structures work:
Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void push(struct Node **head, int data) {
struct Node *new = (struct Node *)malloc(sizeof(struct Node));
new->data = data;
new->next = *head;
*head = new;
}
void print_list(struct Node *head) {
struct Node *current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
int main() {
struct Node *head = NULL;
push(&head, 50);
push(&head, 40);
push(&head, 30);
push(&head, 20);
push(&head, 10);
printf("Linked List: ");
print_list(head);
return 0;
}C vs Other Languages
| Feature | C | C++ | Python | Java |
|---|---|---|---|---|
| Type System | Static | Static | Dynamic | Static |
| Memory Management | Manual | Manual + RAII | Garbage Collected | Garbage Collected |
| Speed | Very Fast | Very Fast | Slow | Fast |
| OOP | No | Yes | Yes | Yes |
| Use Case | Systems | Systems + Apps | General | Enterprise |
| Learning Curve | Steep | Steep | Gentle | Moderate |
C Interview Questions
Prepare for technical interviews with these commonly asked C questions:
- What is the difference between
malloc()andcalloc()? —malloc()allocates uninitialized memory;calloc()allocates zero-initialized memory. - What is a dangling pointer? — A pointer that points to memory that has been freed or gone out of scope.
- What is the difference between
structandunion? — Struct members each get their own memory; union members share the same memory location. - What is the sizeof operator? — Returns the size in bytes of a data type or variable. It is evaluated at compile time for fixed-size types.
- What is a function pointer? — A variable that stores the address of a function, allowing you to call functions dynamically.
Cheat Sheet
| Concept | Syntax |
|---|---|
| Include library | #include <stdio.h> |
| Variable declaration | int x = 10; |
| Function definition | int func(int a) { return a * 2; } |
| Pointer | int *p = &x; |
| Dereference | *p |
| Array | int arr[5] = {1,2,3,4,5}; |
| String | char s[] = "hello"; |
| Struct | struct Point { int x, y; }; |
| malloc | int *p = malloc(n * sizeof(int)); |
| free | free(p); |
| File open | FILE *f = fopen("file.txt", "r"); |
| File close | fclose(f); |
Practice Problems
- Reverse a string — Write a function that reverses a character array in place.
- Fibonacci sequence — Generate the first N Fibonacci numbers using iteration.
- Binary search — Implement binary search on a sorted array.
- Matrix multiplication — Multiply two 2D arrays.
- Linked list reversal — Reverse a singly linked list in place.
- Stack implementation — Implement a stack using arrays.
- File word counter — Count words in a text file.
- Palindrome checker — Check if a string reads the same backwards.
Related Tutorials
Continue your learning journey with these tutorials:
- C Variables and Data Types
- Pointers in C
- Dynamic Memory Allocation
- Arrays in C
- Functions in C
- Memory Management in C
- Linked Lists in C
- Sorting Algorithms in C
▶ Want to Run Code?
Practice C concepts with our Python compiler. Write, run, and experiment with code instantly — no sign-up required.
Try Our Online Compiler →FAQ
What is C programming used for?
C is used for system programming, operating systems (Linux, Windows kernels), embedded systems, game engines, database engines (MySQL, PostgreSQL), compilers, and performance-critical applications. It is the foundation for most modern programming languages.
How to run C code online?
You can run C code online using compilers like CodePractise, OnlineGDB, or Programiz. Simply write your C code in the editor, click Run, and see the output instantly. Our Python compiler also lets you practice similar algorithmic problems while learning C concepts.
Is C good for beginners?
C is an excellent first language because it teaches you how computers actually work — memory, pointers, and low-level operations. While it has a steeper learning curve than Python, understanding C makes learning any other language significantly easier.
What companies use C?
Major companies using C include Microsoft (Windows kernel), Google (Chrome, Android kernel), Apple (macOS, iOS kernels), Linux Foundation, Oracle (MySQL), PostgreSQL, Redis, nginx, and virtually every embedded systems company (ARM, Intel, Qualcomm).
What is the difference between C and C++?
C is a procedural language focused on functions and low-level memory access. C++ is an extension of C that adds object-oriented programming, templates, the STL, and more abstractions. C is preferred for system-level code; C++ for applications needing both performance and abstraction.