C Language CheatSheet

👋 Hello World

#include <stdio.h>

int main() {
    printf("Hello, World!\\n");
    return 0;
}

💡 Tip: Always include stdio.h for input/output.

📦 Variables & Data Types

#include <stdio.h>

int main() {
    int age = 25;
    float pi = 3.14;
    char grade = 'A';

    printf("Age: %d, Pi: %.2f, Grade: %c\\n", age, pi, grade);
    return 0;
}

💡 Tip: Use %d (int), %f (float), %c (char), %s (string).

🔀 Control Flow (if, loops)

#include <stdio.h>

int main() {
    int i;

    for(i = 1; i <= 5; i++) {
        if(i % 2 == 0)
            printf("%d is Even\\n", i);
        else
            printf("%d is Odd\\n", i);
    }
    return 0;
}

💡 Tip: Beginners: practice loops with conditions. Pro: avoid infinite loops by defining exit conditions.

🛠 Functions

#include <stdio.h>

int add(int a, int b) {
    return a + b;
}

int main() {
    printf("Sum: %d\\n", add(5, 3));
    return 0;
}

💡 Tip: Define functions before main() or use a function prototype.

📚 Arrays

#include <stdio.h>

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

💡 Tip: Arrays are fixed size. For dynamic arrays, use pointers & malloc.

🔤 Strings

#include <stdio.h>
#include <string.h>

int main() {
    char name[20] = "Maxon";
    printf("Length: %lu\\n", strlen(name));
    strcat(name, " Codes");
    printf("Full Name: %s\\n", name);
    return 0;
}

💡 Tip: Always include string.h for string functions.

📍 Pointers

#include <stdio.h>

int main() {
    int x = 42;
    int *p = &x;

    printf("Value: %d\\n", *p);
    printf("Address: %p\\n", p);
    return 0;
}

💡 Tip: Pointers store memory addresses. Use * to access values, & for address.

🏗 Structures

#include <stdio.h>

struct Student {
    char name[20];
    int age;
};

int main() {
    struct Student s1 = {"Maxon", 22};
    printf("Name: %s, Age: %d\\n", s1.name, s1.age);
    return 0;
}

💡 Tip: Pro: use typedef for shorthand struct usage.

💾 Dynamic Memory

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *arr = malloc(5 * sizeof(int));
    for(int i = 0; i < 5; i++) arr[i] = i+1;
    for(int i = 0; i < 5; i++) printf("%d ", arr[i]);
    free(arr);
    return 0;
}

💡 Tip: Always free() dynamically allocated memory to avoid memory leaks.

📂 File Handling

#include <stdio.h>

int main() {
    FILE *f = fopen("data.txt", "w");
    fprintf(f, "Hello File!\\n");
    fclose(f);

    f = fopen("data.txt", "r");
    char text[50];
    fgets(text, 50, f);
    printf("%s", text);
    fclose(f);
    return 0;
}

💡 Tip: Always check if fopen() returns NULL before using.

♻ Recursion

#include <stdio.h>

int factorial(int n) {
    if(n == 0) return 1;
    return n * factorial(n-1);
}

int main() {
    printf("Factorial: %d\\n", factorial(5));
    return 0;
}

💡 Tip: Pro: recursion is elegant but can cause stack overflow if too deep.

⚙️ Preprocessor Directives

#include <stdio.h>
#define PI 3.14159

int main() {
    printf("Pi = %.2f\\n", PI);
    return 0;
}

💡 Tip: Use #define for constants. For modern practice, prefer const keyword.

إرسال تعليق