C Cheatsheet C Cheatsheet: Table of Content Table of Content
C Cheatsheet C Cheatsheet: Table of Content Table of Content
C Cheatsheet C Cheatsheet: Table of Content Table of Content
Download PDF
Table of content
C Cheatsheet
Table of content
Boilerplate Code
printf() function
scanf() function
Variables
Basic Data Types
Format Specifiers
Escape Sequences
Operators
Conditional Statements
If Statement
If-Else Statements
If Else-If Statements
Switch Case Statements
Iterative Statements
While loop
Do-While Loop
For Loop
Break Statement
Continue Statement
Functions
Recursion
Pointers
Arrays
Strings
Important String Functions
Structures
Dynamic Memory Allocation
File Handling
Boilerplate Code
#include<stdio.h>
int main(){
return 0;
}
printf() function
printf("Hello World");
scanf() function
scanf("format specifier",variable_name);
Variables
It is data name that is used to store the data value in the memory.
Variable
Valid? Why?
Name
Variable
Valid? Why?
Name
Not
amount$ Dollar Sign is not allowed
Valid
First_name Valid -
float - It stores an floating point value with 6 digit precision. Size - 4 bytes
void main(){
//does not return anything.
}
Format Specifiers
%c Character
%d Integer
%f float
%lf double
Format Specifier Type
%l long
octal
%o
representation
%p pointer
%s string
%% prints % symbol
Escape Sequences
Escape
Type
Sequence
Produces Alarm/Beep
\a
Sound
\b Backspace
\f Form Feed
\n New Line
\t Tab Space
\\ Backslash
\? Question Mark
Operators
Arithmetic Operators
* a*b Multiplication
/ a/b Division
Modulo Division-Gives
% a%b
remainder
Relational Operators
Logical Operators
logical
&& a && b
AND
|| a || b logical OR
variable_name++ Here ++ is a increment operator, it increments the value of variable by 1. variable_name-- Here -- is a decrement operator and it
decrements the value of variable by 1.
More examples:
int a = 1; // a value is 1
a++; // Now Value becomes 2
int b = 5; // b value is 5
b--; // Now, b value is 4
Conditional Statements
If Statement
if(codition)
{
//statements or code
}
//example
int a = 1, b = 5;
if(a < b){
printf("A is smaller");
}
If-Else Statements
if(condition){
//statements
}
else{
//statements
}
//example
int a=1; b=5;
if(a < b){
printf("A is smaller");
}
else{
printf("B is smaller");
}
If Else-If Statements
if(condtion){
//code
}
else if(another_condtion){
//code
}
else{
//code
}
switch(expression)
{
case constant-expression:
statement1;
statements2;
break;
case constant-expression:
another_statements;
break;
Iterative Statements
While loop
while(condition){
//code
}
//example
int a = 10;
while(a <=10){
printf("%d",a);
}//will print 1 to 10
Do-While Loop
do{
// code
}
while(codition);
For Loop
Break Statement
Continue Statement
Continue keyword skips the rest of iterative code of loop and return to starting point of loop.
continue;
Functions
Functions are used to divide the code and to avoid repetitive task. It provides reusability and readability to code. Function Declaration
return_type function_name(data_type-parameters){
//code
}
// Example of function to add two numbers
Recursion
Recursion is the process of repeating items in a self-similar way.If a program allows you to call a function inside the same function, then it is called a recursive call of
the function.
void myFunction(){
myFunction(); //Function calling itself
}
int main(){
int n = 5;
printf("Factorial of %d is %l.",n,factorial(n));
return 0;
}
//OUTPUT : Factorial of 5 is 120.
Pointers
datatype *var_name;
Arrays
Declaration
data_type array_name[array_size];
Strings
char string_name[size];
gets("string");
puts("string");
strlen(string_name);
strcat(string1, string2);
strcmp(string1, string2);
strcpy() function It is used to copy the content of 1st string to another string.
strcpy(string1, string2);
Structures
A structure creates a data type that can be used to group items of possibly different types into a single type. Declaration
struct student
{
char name[50];
int class;
float percentage;
char college[50];
}; //Notice the semicolon
If you are aware of the size of an array, then it is easy and you can define it as an array. For example, to store a name of any person, it can go up to a maximum of 100
characters. But now let us consider a situation where you have no idea about the length of the text you need to store, for example, you want to store a detailed
description about a topic. Here we need to define a pointer to character without defining how much memory is required and later. So we use Dynamic Memory
Allocation.
malloc() function Stands for 'Memory allocation' and reserves a block of memory with the given amount of bytes.
var = (casting_type*)malloc(size);
//Example
var = (int*)malloc(n * sizeof(int))
calloc() function Stands for “contiguous allocation” method in C is used to dynamically allocate the specified number of blocks of memory of the specified type.
realloc() function If the allocated memory is insufficient, then we can change the size of previously allocated memory using this function for efficiency purposes.
var = realloc(var2,n);
File Handling
FILE *file
Opening a File
file = fopen(file_name.txt,w)
Closing a File
fclose(file);