DEV Community

tamilvanan
tamilvanan

Posted on • Edited on

Learn C - 001

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;
}
Enter fullscreen mode Exit fullscreen mode

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';
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Œ Operators

  • Arithmetic: +, -, *, /, %
  • Comparison: ==, !=, >, <, >=, <=
  • Logical: &&, ||, !

๐Ÿ“Œ Control Flow

if, else, else if

if (age > 18) {
    printf("Adult\n");
} else {
    printf("Minor\n");
}
Enter fullscreen mode Exit fullscreen mode

for, while, do-while

for (int i = 0; i < 5; i++) {
    printf("%d\n", i);
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”„ 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);
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿงฑ 5. Arrays and Strings

Arrays

int numbers[5] = {1, 2, 3, 4, 5};
Enter fullscreen mode Exit fullscreen mode

Strings (character arrays)

char name[] = "Alice";
printf("%s", name);
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ฆ 6. Pointers

Pointers store memory addresses.

int x = 10;
int *p = &x;
printf("%d", *p);  // Outputs 10
Enter fullscreen mode Exit fullscreen mode

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};
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ง 8. Memory Management

Manual memory handling using:

  • malloc() โ€“ allocate memory
  • free() โ€“ deallocate memory
int *ptr = malloc(sizeof(int) * 5);
free(ptr);
Enter fullscreen mode Exit fullscreen mode

๐Ÿงฐ 9. File Handling

C supports file operations:

FILE *f = fopen("data.txt", "r");
fscanf(f, "%s", buffer);
fclose(f);
Enter fullscreen mode Exit fullscreen mode

๐Ÿงช 10. Compilation & Execution

C is compiled using tools like GCC.

gcc program.c -o program
./program
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”š 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)