"Learn the fundamentals of C programming."
By Samir Niroula
27 October 2023Master C programming from basics to advanced topics.
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.
Install a C compiler and text editor or IDE:
xcode-select --install
)sudo apt-get install gcc
)#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
int age = 25;
float salary = 50000.50;
char grade = 'A';
int a = 10, b = 20;
int sum = a + b; // sum is 30
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.
Operators are used to perform operations on variables and values. Common operators include:
+
, -
, *
, /
, %
(e.g., int sum = a + b;
)==
, !=
, >
, <
, >=
, <=
(e.g., if (a > b)
)&&
, ||
, !
(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
.
Control structures allow you to control the flow of execution in your program. Common control structures include:
if
statements for conditional executionfor
loops for iterating a specific number of timeswhile
loops for iterating while a condition is trueswitch
statements for selecting among multiple optionsExample:
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.
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.
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.
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 = #
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.
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.
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.
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.