Learn the fundamentals of C programming language
C is a general-purpose, procedural programming language developed by Dennis Ritchie in 1972. It is a low-level language that provides direct access to memory and hardware, making it efficient for system programming.
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
This is the basic structure of a C program. The #include directive includes the standard input/output library, and main() is the entry point of the program.
Understanding different data types in C
Variables are named memory locations that store data values. Data types define the type and size of data that can be stored in variables.
int age = 25;
float salary = 50000.50;
char grade = 'A';
double pi = 3.14159;
printf("Age: %d\n", age);
printf("Salary: %.2f\n", salary);
printf("Grade: %c\n", grade);
printf("Pi: %.5f\n", pi);
C supports various data types: int for integers, float for decimals, char for characters, and double for high-precision decimals.
Mastering loops and conditional statements
Control structures are programming constructs that determine the flow of program execution based on conditions and loops.
// If-else statement
if (age >= 18) {
printf("Adult\n");
} else {
printf("Minor\n");
}
// For loop
for (int i = 0; i < 10; i++) {
printf("%d ", i);
}
// While loop
int count = 0;
while (count < 5) {
printf("Count: %d\n", count);
count++;
}
Control structures allow you to make decisions and repeat code blocks. Use if-else for conditions, for/while for loops.
Creating reusable code blocks
Functions are self-contained blocks of code that perform specific tasks. They promote code reusability and modularity.
// Function declaration
int add(int a, int b) {
return a + b;
}
// Function with void return type
void greet(char name[]) {
printf("Hello, %s!\n", name);
}
int main() {
int result = add(5, 3);
printf("Sum: %d\n", result);
greet("Student");
return 0;
}
Functions help organize code into reusable blocks. They can return values or perform actions without returning anything (void).
Working with collections of data
Arrays are collections of elements of the same data type stored in contiguous memory locations.
// Array declaration and initialization
int numbers[5] = {1, 2, 3, 4, 5};
// Accessing array elements
printf("First element: %d\n", numbers[0]);
printf("Last element: %d\n", numbers[4]);
// Looping through array
for (int i = 0; i < 5; i++) {
printf("numbers[%d] = %d\n", i, numbers[i]);
}
Arrays store multiple values of the same type. Elements are accessed using index numbers starting from 0.
Understanding memory addresses
Pointers are variables that store memory addresses of other variables. They provide direct access to memory locations.
int number = 42;
int *ptr = &number; // Pointer to number
printf("Value: %d\n", number);
printf("Address: %p\n", (void*)&number);
printf("Pointer value: %d\n", *ptr);
// Modifying value through pointer
*ptr = 100;
printf("New value: %d\n", number);
Pointers store memory addresses. They allow direct manipulation of memory and are fundamental to C programming.
Start a new journey with our Academy