Note: Iโm not an expert. Iโm writing this blog just to document my learning journey. ๐
Letโs build up your understanding of the C programming language from the ground up. C is a powerful and foundational language, often considered the "mother of all modern languages" because many other languages (like C++, Java, and even Python) are influenced by it.
๐ฐ 1. What Is C?
C is a general-purpose, procedural, and low-level programming language developed in the early 1970s by Dennis Ritchie at Bell Labs. It was originally created to implement the UNIX operating system.
Why Learn C?
- It's fast and gives you low-level memory access.
- It teaches you how computers actually work.
- It's used in operating systems, embedded systems, compilers, drivers, and more.
๐งฑ 2. Basics of a C Program
Hereโs a simple C program:
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
Breakdown:
-
#include <stdio.h>
โ Includes the Standard Input Output library. -
int main()
โ Entry point of the program. -
printf()
โ Function to print text. -
return 0;
โ Indicates successful program termination.
๐ง 3. Key Concepts
๐ Variables & Data Types
Variables store data. C is statically typed, so you must declare types.
int age = 25;
float weight = 70.5;
char grade = 'A';
๐ Operators
- Arithmetic:
+
,-
,*
,/
,%
- Comparison:
==
,!=
,>
,<
,>=
,<=
- Logical:
&&
,||
,!
๐ Control Flow
if
, else
, else if
if (age > 18) {
printf("Adult\n");
} else {
printf("Minor\n");
}
for
, while
, do-while
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
}
๐ 4. Functions
Functions allow code reuse.
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(3, 4);
printf("Sum is %d\n", result);
}
๐งฑ 5. Arrays and Strings
Arrays
int numbers[5] = {1, 2, 3, 4, 5};
Strings (character arrays)
char name[] = "Alice";
printf("%s", name);
๐ฆ 6. Pointers
Pointers store memory addresses.
int x = 10;
int *p = &x;
printf("%d", *p); // Outputs 10
Understanding pointers is key in C, especially for arrays, functions, and memory management.
๐๏ธ 7. Structures
Used to group variables of different types.
struct Person {
char name[50];
int age;
};
struct Person p1 = {"Alice", 30};
๐ง 8. Memory Management
Manual memory handling using:
-
malloc()
โ allocate memory -
free()
โ deallocate memory
int *ptr = malloc(sizeof(int) * 5);
free(ptr);
๐งฐ 9. File Handling
C supports file operations:
FILE *f = fopen("data.txt", "r");
fscanf(f, "%s", buffer);
fclose(f);
๐งช 10. Compilation & Execution
C is compiled using tools like GCC.
gcc program.c -o program
./program
๐ In Summary
Concept | Description |
---|---|
Low-level | Close to hardware |
Compiled | Translated before execution |
Fast | Excellent for performance-critical tasks |
Manual memory | No garbage collection |
Portable | Works on various systems |
Top comments (0)