C Language Cheat sheet Free Pdf

Chapter – 1 : C Basics

Introduction to C Language

C Language is a general-purpose programming language created by Dennis Ritchie at Bell Labs in 1972.

Character Set and Tokens in C Language

C supports letters, digits, and special symbols. Tokens include keywords, identifiers, constants, strings, and operators.

int main() {
    int a = 5; // 'int', 'main', '(', ')', '{', 'int', 'a', '=', '5', ';', '}' are tokens
    return 0;
}

First C Program in C Language

The basic structure of a C program includes a main function, which is the entry point.

#include <stdio.h>
int main() {
    printf("Hello, World!\n");
    return 0;
}

Variables in C Language

Variables store data values.

int age = 25;

Data Types in C Language

Common data types include int, float, char, and double.

int num = 10;
float salary = 12345.50;
char grade = 'A';

Constants in C Language

Constants are fixed values that do not change.

#define PI 3.14
const int DAYS_IN_WEEK = 7;

Basic Input/Output in C Language

Using scanf for input and printf for output.

#include <stdio.h>
int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("You entered: %d\n", num);
    return 0;
}

Operators and Expression in C Language

Operators include arithmetic, relational, logical, and bitwise operators.

int a = 10, b = 20;
int sum = a + b; // Arithmetic operator
int isEqual = (a == b); // Relational operator
int logicalAnd = (a && b); // Logical operator
C Language Complete Course

Chapter – 2 : Conditional Control Statements

Introduction

Control flow statements allow you to control the flow of execution in a program.

if Statement

Executes a block of code if a specified condition is true.

if (a > b) {
    printf("a is greater than b");
}

if-else Statement

Executes one block of code if the condition is true, otherwise executes another block.

if (a > b) {
    printf("a is greater than b");
} else {
    printf("a is not greater than b");
}

Nesting if-else Statement

An if-else statement inside another if-else statement.

if (a > b) {
    if (a > c) {
        printf("a is the greatest");
    } else {
        printf("c is the greatest");
    }
} else {
    printf("b is greater than a");
}

else-if Statements

Tests multiple conditions.

if (a > b) {
    printf("a is greater than b");
} else if (a > c) {
    printf("a is greater than c");
} else {
    printf("b or c is the greatest");
}

Switch Case Statement

Selects one of many code blocks to be executed.

switch (a) {
    case 1:
        printf("a is 1");
        break;
    case 2:
        printf("a is 2");
        break;
    default:
        printf("a is not 1 or 2");
}

Chapter – 3 : Loop Control Structure

Introduction

Loops execute a block of code repeatedly.

While Loop

Executes as long as a condition is true.

int i = 0;
while (i < 10) {
    printf("%d\n", i);
    i++;
}

For Loop

Executes a block of code a specified number of times.

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

Do-while Loop

Executes a block of code at least once, then repeats as long as a condition is true.

int i = 0;
do {
    printf("%d\n", i);
    i++;
} while (i < 10);

Jump Statement

Includes break, continue, and goto.

for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break; // Exit loop when i is 5
    }
    printf("%d\n", i);
}

Chapter – 4 : Functions

Introduction to Functions

Functions are blocks of code designed to perform a specific task.

Types Of Functions

Functions can be standard library functions or user-defined functions.

Defining and Declaring a Function

Function declaration specifies the function’s name, return type, and parameters.

void greet() {
    printf("Hello, World!\n");
}

Function Call and Return Statement

Calling a function executes the function code.

int add(int a, int b) {
    return a + b;
}
int main() {
    int result = add(5, 3);
    printf("Result: %d\n", result);
    return 0;
}

Variations in Functions

Function variations include different return types and parameter types.

float multiply(float a, float b) {
    return a * b;
}

Passing Arguments to Functions

Arguments can be passed by value or by reference.

void increment(int *n) {
    (*n)++;
}
int main() {
    int a = 5;
    increment(&a);
    printf("%d\n", a); // Output: 6
    return 0;
}

Handling Non Integer Function

Functions can handle different data types.

char getFirstChar(char *str) {
    return str[0];
}

Recursion

A function calling itself.

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

Chapter – 5 : Scope Rules and Storage Class

Scope Rules of variables

Scope determines the visibility of variables.

int globalVar = 10;
void func() {
    int localVar = 20; // Local scope
}

Storage Class

Specifies the lifetime and visibility of variables.

static int count = 0; // Static variable

Chapter – 6 : Arrays

Introduction to Arrays

Arrays store multiple values of the same type.

Defining and Declaring Array

Arrays are declared with a type and size.

int numbers[5];

Memory Map of an Array

Arrays are stored in contiguous memory locations.

Accessing Elements

Array elements are accessed using indices.

numbers[0] = 1;

Traversing or Outputting in Array

Loop through arrays to access elements.

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

Initializing Array

Arrays can be initialized at declaration.

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

Passing Arrays to Functions

Arrays can be passed to functions by reference.

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d\n", arr[i]);
    }
}

Searching

Finding an element in an array.

int search(int arr[], int size, int key) {
    for (int i = 0; i < size; i++) {
        if (arr[i] == key) return i;
    }
    return -1;
}

Sorting

Arranging array elements in a specific order.

void bubbleSort(int arr[], int size) {
    for (int i = 0; i < size-1; i++) {
        for (int j = 0; j < size-i-1; j++) {
            if (arr[j] > arr[j+1]) {
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
}

Chapter – 7 : String

Introduction to String

Strings are arrays of characters.

Declaring and Initializing a String

Strings are declared and initialized like arrays.

char name[] = "John";

Standard Library String Function

Includes functions like strlen, strcpy, strcmp.

#include <string.h>
int len = strlen(name); // Length of the string

Passing String to Function & Array of String

Strings can be passed to functions and stored in arrays.

void printString(char str[]) {
    printf("%s", str);
}

Chapter – 8 : Structure and Unions

Introduction to Structure and Unions

Structures store different data types together, unions use shared memory.

Declaring and Accessing a Structure

Structures are declared with

struct Person {
    char name[50];
    int age;
};
struct Person person1;

Initializing Assignment of Structure

Assign values to structure members.

struct Person person1 = {"John", 30};

Arrays of Structures

Store multiple structures in an array.

struct Person people[2] = {{"John", 30}, {"Jane", 25}};

Nested Structures

Structures within structures.

struct Address {
    char city[50];
    char state[50];
};
struct Person {
    char name[50];
    struct Address address;
};

Pointers and Structures

Use pointers to structures.

struct Person *ptr = &person1;

Passing Structure to Functions

Pass structures to functions by value or reference.

void printPerson(struct Person p) {
    printf("%s, %d", p.name, p.age);
}

“typedef” for Structures and Unions

Create an alias for structures.

typedef struct {
    char name[50];
    int age;
} Person;
Person person1;

Chapter – 9 : Pointers

Introduction to Pointers

Pointers store memory addresses.

Pointer and Indirection Operators

Use * for indirection and & to get the address.

int a = 5;
int *ptr = &a;

Pointer Variable Declaration and Access

Declare and use pointers.

int *p;
p = &a;

Initialization and Dereferencing of Pointer Variable

Initialize and dereference pointers.

int value = *p;

Pointer to Pointer

Pointers can point to other pointers.

int **pp = &p;

Chapter – 10 : File Handling

Introduction to File Handling

Files store data permanently.

Operations on File

Common operations include open, read, write, and close.

FILE *file = fopen("example.txt", "r");
fclose(file);

Text VS Binary Files

Text files store data in human-readable form, binary files store data in binary form.

Command Line Arguments

Pass arguments to main function from the command line.

int main(int argc, char *argv[]) {
    printf("First argument: %s\n", argv[0]);
    return 0;
}

Here is the Pdf Download Link of C Language CheatSheet.

FREE DOWNLOAD

Send download link to:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top