C Programming
Learn the powerful and influential C programming language, the foundation of modern system programming and many other programming languages.
C Basics
What is C?
C is a general-purpose, procedural programming language developed in the early 1970s by Dennis Ritchie at Bell Labs. It was originally designed for and implemented on the UNIX operating system but has since become one of the most widely used programming languages of all time.
C provides low-level access to memory and language constructs that map efficiently to machine instructions, making it suitable for system programming like operating systems and embedded systems. Despite its age, C remains extremely relevant today and forms the foundation for many modern programming languages including C++, C#, and Objective-C.
Key Features of C
- Efficiency: C is designed to be compiled to efficient machine code with minimal runtime overhead
- Portability: Code written in C can be compiled for different platforms with minimal changes
- Low-level Memory Access: C allows direct manipulation of memory through pointers
- Modularity: C supports structured programming with functions and modules
- Rich Set of Operators: C provides a comprehensive set of operators for arithmetic, logical, and bitwise operations
- Static Type System: C requires variables to be declared with specific types before use
C Syntax
Basic C Program Structure
int main() {
printf("Hello, World!\\n");
return 0;
}
This simple program demonstrates the basic structure of a C application:
#include <stdio.h>- Includes the standard input/output libraryint main()- The main function, the entry point of every C programprintf()- A function from the stdio.h library that outputs textreturn 0;- Returns a value of 0 to the operating system (indicating successful execution)
Data Types in C
C supports several basic data types:
| Type | Description | Example |
|---|---|---|
| int | Integer values | int age = 25; |
| float | Single-precision floating point | float price = 10.5f; |
| double | Double-precision floating point | double pi = 3.14159; |
| char | Single character | char grade = 'A'; |
| void | No value (used for functions that don't return a value) | void printMessage(); |
Type Modifiers
C also provides type modifiers that can be applied to basic types:
short- Reduces the size of an integerlong- Increases the size of an integer or doublesigned- Allows positive and negative values (default for most types)unsigned- Allows only positive values
short int temperature = -5;
Control Structures
printf("You are an adult.\\n");
} else {
printf("You are a minor.\\n");
}
C supports if, else if, and else statements for conditional execution of code.
case 1:
printf("Monday\\n");
break;
case 2:
printf("Tuesday\\n");
break;
default:
printf("Other day\\n");
}
The switch statement allows multi-way branching based on the value of an expression.
printf("%d\\n", i);
}
The for loop is used when you know in advance how many times you want to execute a statement or block of statements.
while (i < 5) {
printf("%d\\n", i);
i++;
}
The while loop is used when you want to repeat a statement or block of statements while a condition is true.
Functions in C
Functions are blocks of code that perform a specific task. They help in organizing code, making it reusable and modular.
int add(int a, int b);
int main() {
int result = add(5, 3);
printf("Sum: %d\\n", result);
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
Key Points About Functions:
- Functions must be declared before they are used
- The return type specifies what kind of value the function returns
- Parameters allow passing data to the function
- The
voidreturn type is used when a function doesn't return any value - Functions can call other functions, including themselves (recursion)
Arrays and Pointers
Arrays
Arrays in C are collections of elements of the same type stored in contiguous memory locations.
int numbers[5] = { 10, 20, 30, 40, 50 };
// Accessing array elements
printf("%d\\n", numbers[0]); // Outputs: 10
// Modifying an array element
numbers[2] = 35;
Arrays are zero-indexed in C, meaning the first element is at index 0.
Pointers
Pointers are variables that store memory addresses. They are one of C's most powerful features, allowing direct memory manipulation.
// Declare a pointer and assign the address of x
int *ptr = &x;
// Access the value using the pointer (dereferencing)
printf("%d\\n", *ptr); // Outputs: 10
// Modify the value through the pointer
*ptr = 20;
printf("%d\\n", x); // Outputs: 20
Relationship Between Arrays and Pointers
In C, arrays and pointers are closely related. The name of an array is essentially a pointer to its first element.
// These two expressions are equivalent
printf("%d\\n", numbers[0]);
printf("%d\\n", *numbers);
// Array indexing can be done with pointer arithmetic
printf("%d\\n", numbers[2]);
printf("%d\\n", *(numbers + 2));
Learning Resources
Continue your C programming journey with these resources:
- "The C Programming Language" by Brian Kernighan and Dennis Ritchie (often called K&R) - The definitive guide written by the language's creators
- Learn-C.org - Free interactive C tutorials
- Cprogramming.com - Tutorials and resources for C programming
- C Reference - Comprehensive reference for C language features