









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










The C Language Cheat Sheet is an easy-to-use reference manual for key grammar and ideas in the C programming language. Beginning with a discussion of the fundamental syntax of a C programme, it gradually moves on to topics such as data types, variables, operators, control flow, arrays, functions, pointers, strings, input/output, structures, memory management, preprocessor directives, file handling, and common library functions. The cheat sheet presents each topic with clear syntax examples, making it simple to comprehend and use in practise. The cheat sheet focuses on the core concepts of C, such as declaring variables, carrying out operations with operators, directing the flow of programmes with conditional statements and loops, working with arrays and strings, utilising functions and recursion, manipulating memory with pointers, managing files, and utilising various other programming languages. You may rapidly refer to and review important C programming principles by utilising this cheat sheet. It is a helpful tool for both new and seasoned programmers who need to locate syntax examples and recollect crucial information when writing in C. This cheat sheet will provide you the crucial knowledge you need to create successful and efficient C programmes, whether you just need a fast refresher or a thorough understanding of the C language.
#include <stdio.h> int main() { // Code goes here return 0;
float pi;
## - char : Used for characters. ### ``` char grade; int *ptr;
x += 5; if (a && b) { ... } if (a > b) { ... } int result = a & b; ****5. Control Flow:****
if (condition) { // code to execute if the condition is true } else { // code to execute if the condition is false } switch (expression) { case constant1: // code to execute if expression equals constant break; case constant2: // code to execute if expression equals constant break; default: // code to execute if expression doesn't match any constant } for (initialization; condition; increment/decrement) { // code to execute repeatedly for (int i = 0; i < 10; i++) { if (i == 5) { continue; // Skip the iteration when i is 5 } } ****6. Arrays:****
int numbers[5]; int numbers[5] = {1, 2, 3, 4, 5}; int firstNumber = numbers[0]; int matrix[3][3]; ****7. Functions:****
int add(int a, int b) { return a + b; } int result = add(5, 3); void greet(char name[]) { - Declare a pointer with a data type followed by an asterisk (*) and the pointer name. ```c int *ptr; int number = 10; int *ptr = &number; int value = *ptr; int numbers[5] = {1, 2, 3, 4, 5}; int *ptr = numbers; int thirdNumber = *(ptr + 2); 9. Strings:
char name[10]; char greeting[] = "Hello"; char firstChar = name[0]; char destination[20]; char source[] = "World"; strcpy(destination, source); 10. Input/Output:
struct Person p; strcpy(p.name, "John"); p.age = 25;
## **12. Memory Management:** - Dynamic memory allocation functions: - malloc(size): Allocates a block of memory of the given size in bytes. ```c int *ptr = (int*)malloc (sizeof(int)); ``` - calloc(num, size): Allocates an array of num elements, each of size bytes, and initializes them to 0. ```c int *ptr = (int*)calloc(5, sizeof(int)); ``` - realloc(ptr, size): Resizes the previously allocated block of memory to the given size. ```c int *newPtr = (int*)realloc(ptr, 10 * sizeof(int)); ``` - free(ptr): Releases the memory block previously allocated. ```c free(ptr); ``` ## **13. Preprocessor Directives:** - #include: Includes header files in the program. ```c #include <stdio.h> #define PI 3. FILE *file = fopen("data.txt", "r"); char line[100]; fgets(line, sizeof(line), file);