C Language CheatSheet with example and definition

A cheat sheet is a handy reference tool that provides quick access to the most commonly used features and syntax of a programming language. In this article, we will present a comprehensive C language cheat sheet with examples. Whether you’re a beginner or an experienced programmer, this cheat sheet will serve as a valuable resource for writing C programs efficiently.

Basic Syntax and Data Types

C is a procedural programming language known for its low-level control over hardware.

Hello World Program:

#include <stdio.h>

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

Variable Declaration and Initialization:

int age = 25;
float salary = 2500.50;
char grade = 'A';

Control Flow Statements

Control flow statements manage the program’s execution based on conditions and loops.

if-else Statement:

if (x > 10) {
    printf("x is greater than 10\n");
} else {
    printf("x is less than or equal to 10\n");
}

for Loop:

for (int i = 0; i < 5; i++) {
    printf("Iteration: %d\n", i);
}

Functions and Pointers

Functions are blocks of reusable code that perform specific tasks. Pointers hold memory addresses.

Function Declaration and Definition:

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

Pointer Declaration and Usage:

int num = 10;
int *ptr = &num;

Pointers and Memory Allocation:

int* ptr = NULL;
ptr = (int*)malloc(sizeof(int));
*ptr = 10;

Arrays and Strings

Arrays store multiple elements of the same data type. And string a type of character array.

Array Declaration and Initialization:

int numbers[5] = {1, 2, 3, 4, 5};

String Manipulation:

char name[] = "John";
printf("Length of name: %d\n", strlen(name));

Input and Output

Input and output operations enable interaction with users and data exchange.

Reading Input:

int number;
printf("Enter a number: ");
scanf("%d", &number);

Writing Output:

printf("The result is: %d\n", result);

File Handling

File Opening and Reading:

FILE* file = fopen("data.txt", "r");
if (file != NULL) {
    char buffer[100];
    fgets(buffer, 100, file);
    printf("Data: %s\n", buffer);
    fclose(file);
}

Memory Management

Memory management involves allocating and deallocating memory.

Dynamic Memory Allocation and Deallocation:

int* numbers = (int*)malloc(5 * sizeof(int));
numbers[0] = 1;
free(numbers);

Error Handling

Checking for Null Pointers:

int* ptr = NULL;
if (ptr == NULL) {
    printf("Pointer is NULL\n");
}

Additional Libraries

Libraries are collections of precompiled code that provide additional functionality.

Including Math Library:

#include <math.h>
double result = sqrt(25);

Preprocessor Directives

Preprocessor directives modify the source code before compilation.

Include Header File:

#include <stdio.h>

Conclusion

This C language cheat sheet provides a quick reference guide for fundamental concepts, syntax, and examples. Use it as a valuable tool to write efficient C programs. However, it’s always recommended to consult official documentation for a more detailed understanding.