C Programming Course

"Learn the fundamentals of C programming."

By Samir Niroula

27 October 2023
C Programming Course

C Programming Course

Master C programming from basics to advanced topics.

1. Introduction to C Programming

C is a powerful language used in system software and applications requiring performance. Developed by Dennis Ritchie in the 1970s, it has influenced many languages like C++, Java, and Python.

2. Setting Up Your Environment

Install a C compiler and text editor or IDE:

  • Windows: MinGW
  • macOS: Xcode Command Line Tools (xcode-select --install)
  • Linux: GCC (sudo apt-get install gcc)

3. Basic Syntax and Structure

#include <stdio.h>
 
int main() {
    printf("Hello, World!\n");
    return 0;
}

4. Variables and Data Types

int age = 25;
float salary = 50000.50;
char grade = 'A';

5. Operators and Expressions

int a = 10, b = 20;
int sum = a + b; // sum is 30

6. Control Structures

int num = 10;
if (num > 0) {
    printf("Positive number\n");
- `int` for integers (e.g., `int age = 25;`)
- `float` for floating-point numbers (e.g., `float salary = 50000.50;`)
- `char` for characters (e.g., `char grade = 'A';`)
 
Example:
 
```c
int age = 25;
float salary = 50000.50;
char grade = 'A';

Each variable must be declared with a specific data type, which determines the kind of data it can hold and the operations that can be performed on it.


5. Operators and Expressions

Operators are used to perform operations on variables and values. Common operators include:

  • Arithmetic operators: +, -, *, /, % (e.g., int sum = a + b;)
  • Relational operators: ==, !=, >, <, >=, <= (e.g., if (a > b))
  • Logical operators: &&, ||, ! (e.g., if (a > 0 && b < 0))

Example:

int a = 10, b = 20;
int sum = a + b; // sum is 30

Expressions combine variables and operators to produce a new value. For example, a + b is an expression that adds the values of a and b.


6. Control Structures

Control structures allow you to control the flow of execution in your program. Common control structures include:

  • if statements for conditional execution
  • for loops for iterating a specific number of times
  • while loops for iterating while a condition is true
  • switch statements for selecting among multiple options

Example:

int num = 10;
if (num > 0) {
    printf("Positive number\n");
} else {
    printf("Non-positive number\n");
}

Control structures are essential for making decisions and repeating actions in your programs.


7. Functions in C

Functions are blocks of code that perform a specific task. They help in organizing code and reusability. A function has a name, a return type, and parameters.

Example:

#include <stdio.h>
 
void greet() {
    printf("Hello, World!\n");
}
 
int main() {
    greet();
    return 0;
}

In this example, greet is a function that prints a message. Functions can take parameters and return values, making them versatile tools for structuring your code.


8. Arrays

Arrays are used to store multiple values of the same type in a single variable. They are useful for handling collections of data.

Example:

int numbers[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
    printf("%d\n", numbers[i]);
}

In this example, numbers is an array of integers with 5 elements. Arrays are indexed starting from 0, so numbers[0] is the first element.


9. Pointers and Memory Management

Pointers are variables that store the memory address of another variable. They are powerful but need careful handling to avoid errors like memory leaks and segmentation faults.

Example:

int num = 10;
int *ptr = &num;
printf("Value: %d, Address: %p\n", num, ptr);

In this example, ptr is a pointer to the variable num. Pointers are essential for dynamic memory allocation and manipulating data structures like linked lists and trees.


10. Structs and Unions

Structs are used to group different data types together, while unions allow storing different data types in the same memory location.

Example:

struct Person {
    char name[50];
    int age;
};
 
struct Person person1;
strcpy(person1.name, "John");
person1.age = 30;

In this example, Person is a struct that groups a character array and an integer. Structs are useful for creating complex data types that represent real-world entities.


11. File I/O

File I/O operations allow you to read from and write to files. This is essential for tasks like data storage, configuration management, and logging.

Example:

#include <stdio.h>
 
int main() {
    FILE *file = fopen("example.txt", "w");
    if (file != NULL) {
        fprintf(file, "Hello, File!\n");
        fclose(file);
    }
    return 0;
}

In this example, fopen opens a file for writing, fprintf writes a string to the file, and fclose closes the file. Proper file handling ensures data integrity and resource management.


12. Conclusion and Next Steps

Congratulations on completing the C programming course! Continue practicing by working on projects and exploring advanced topics like data structures, algorithms, and system programming. The more you practice, the more proficient you will become. Consider contributing to open-source projects or developing your own applications to further enhance your skills.