Introduction to Programming in C
C is a powerful and widely-used programming language that forms the foundation for many modern programming languages. It is known for its efficiency, control, and low-level access to memory. Whether you’re a beginner or looking to refresh your knowledge, this guide will walk you through the fundamentals of C programming with clear explanations and practical examples.
By the end of this series, you’ll be able to write your own C programs, understand how memory works, and build a strong foundation for learning other programming languages.
Table of Contents
- What is C?
- Setting Up Your Environment
- Variables and Data Types
- Operators and Expressions
- Conditional Statements
- Loops
- Functions
- Arrays
- Pointers
- Structures
What is C?
C is a general-purpose, procedural programming language developed in the early 1970s. It is widely used for system programming, embedded systems, and developing operating systems.
// Example: Your first C program
#include <stdio.h>
int main() {
printf("Hello, C Programming!\n");
return 0;
}
Setting Up Your Environment
To write and run C programs, you need a C compiler like GCC (GNU Compiler Collection) and a text editor or IDE (Integrated Development Environment).
# Example: Compiling and running a C program
gcc program.c -o program
./program
Variables and Data Types
Variables are used to store data in C. You must declare the type of a variable before using it. Common data types include int
, float
, char
, and double
.
// Example: Declaring variables
#include <stdio.h>
int main() {
int age = 25;
float height = 5.9;
char grade = 'A';
printf("Age: %d, Height: %.2f, Grade: %c\n", age, height, grade);
return 0;
}
Operators and Expressions
C includes arithmetic, relational, and logical operators to perform operations on data.
// Example: Using operators
#include <stdio.h>
int main() {
int a = 10, b = 5;
int sum = a + b; // 15
int isGreater = a > b; // 1 (true)
printf("Sum: %d, Is Greater: %d\n", sum, isGreater);
return 0;
}
Conditional Statements
Conditional statements like if
, else if
, and else
allow you to execute code based on certain conditions.
// Example: Conditional statement
#include <stdio.h>
int main() {
int age = 18;
if (age >= 18) {
printf("You are an adult.\n");
} else {
printf("You are a minor.\n");
}
return 0;
}
Loops
Loops like for
, while
, and do-while
help you repeat a block of code multiple times.
// Example: For loop
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
printf("Iteration: %d\n", i);
}
return 0;
}
Functions
Functions are reusable blocks of code that perform a specific task. You can define functions using the return_type function_name()
syntax.
// Example: Function
#include <stdio.h>
// Function to add two numbers
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 10);
printf("Result: %d\n", result); // 15
return 0;
}
Arrays
Arrays are used to store multiple values of the same type in a single variable. You can access and manipulate array elements using their index.
// Example: Array
#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("Number %d: %d\n", i, numbers[i]);
}
return 0;
}
Pointers
Pointers are variables that store memory addresses. They are a powerful feature of C that allows direct memory manipulation.
// Example: Pointers
#include <stdio.h>
int main() {
int num = 10;
int *ptr = # // Pointer to num
printf("Value: %d, Address: %p\n", *ptr, ptr);
return 0;
}
Structures
Structures allow you to group related data of different types under a single name.
// Example: Structure
#include <stdio.h>
// Define a structure
struct Person {
char name[50];
int age;
float height;
};
int main() {
struct Person person1 = {"Alice", 25, 5.9};
printf("Name: %s, Age: %d, Height: %.2f\n", person1.name, person1.age, person1.height);
return 0;
}