C Cheatsheet
C Cheatsheet
In this Cheat Sheet, we will delve into the basics of the C language,
exploring its fundamental concepts that lay the groundwork for
programming. We will cover topics such as variables, data types, and
operators, providing you with a solid understanding of the building
blocks of C programming.
C CheatSheet By Skills Taiyari Page 2
Basic Syntax
Consider the below Hello World program:
int main()
{
printf("Hello World!");
return 0;
}
Output
Hello World!
Here,
#include <stdio.h>: The header file inclusion to use printf()
function.
int main(): The main() function is the entry point of any C
program.
printf(“Hello World”): Function to print hello world.
return 0: Value returned by the main() function.
C CheatSheet By Skills Taiyari Page 3
Variables
A variable is the name given to the memory location that stores some
data.
Syntax of Variable
data_type variable_name;
data_type variable_name = initial_value;
A variable can be of the following types:
1st Local Variable
2nd Global Variable
3rd Static Variable
4th Extern Variable
5th Auto Variable
6th Register Variable
Note: There are a few rules which we have to follow while naming a
variable.
C CheatSheet By Skills Taiyari Page 4
Data Types
The data type is the type of data that a given variable can store.
Different data types have different sizes. There are 3 types of data
types in C:
1st Basic Data Types
2nd Derived Data Types
3rd User Defined Data Types
Basic data types are built-in in the C programming language and are
independent of any other data type. There are x types of basic data
types in C:
1st char: Used to represent characters.
2nd int: Used to represent integral numbers.
3rd float: Used to represent decimal numbers up to 6-7 precision
digits.
4th double: Used to represent decimal numbers up to 15 precision
digits.
5th void: Used to represent the valueless entity.
Example of Basic Data Types
char c = 'a';
int integer = 24;
float f = 24.32;
double d = 24.3435;
void v;
C CheatSheet By Skills Taiyari Page 5
The size of these basic data types can be modified using data type
modifiers which are:
1st short
2nd long
3rd signed
4th unsigned
Example of Data Type Modifiers
unsigned int var1;
long double var2;
long int var3;
Derived data types are derived from the basic data types. There are 2
derived data types in C:
1st Arrays
2nd Pointers
The user-defined data types are the data types that are defined by the
programmers in their code. There are 3 user-defined data types in C:
1st Structure
Identifiers
Identifiers is the name given to the variables, functions, structure, etc.
Identifiers should follow the following set of rules:
1st A variable name must only contain alphabets, digits, and
underscore.
2nd A variable name must start with an alphabet or an underscore
only. It cannot start with a digit.
C CheatSheet By Skills Taiyari Page 6
Example of Identifiers
Keywords
Keywords are the reserved words that have predefined meanings in the
C compiler. They cannot be used as identifiers.
Example of Keywords
auto,
float,
int,
return,
switch
The printf() function is used to print the output on the standard output
device which is generally the display screen.
Syntax of printf()
printf("formatted-string", ...{arguments-list});
where,
formatted-string: String to be printed. It may contain format
specifiers, escape sequences, etc.
arguments-list: It is the list of data to be printed.
The scanf() function is used to take input from the standard input
device such as the keyboard.
Syntax of scanf()
scanf("formatted-string", {address-argument-list});
where,
formatted-string: String that describes the format of the input.
address-of-arguments-list: It is the address list of the
variables where we want to store the input.
int main()
{
int roll_num;
char name[50];
return 0;
}
Output
Name is ? and Roll Number is 0
C CheatSheet By Skills Taiyari Page 8
Input
Enter Roll No: 20
Enter Name: GeeksforGeeks
Output
Name is GeeksforGeeks and Roll Number is 20.
Format Specifiers
Format specifiers are used to describe the format of input and output in
formatted string. It is different for different data types. It always starts
with %
The following is the list of some commonly used format specifiers
in C:
%c For b type.
%lf Double
%p Pointer
%s String
%u Unsigned int
%% Prints % character
Escape Sequence
C CheatSheet By Skills Taiyari Page 9
Escape sequences are the characters that are used to represent those
characters that cannot by represented normally. They start with ( \ )
backslash and can be used inside string literals.
The below table list some commonly used escape sequences:
Operators
C CheatSheet By Skills Taiyari Page 10
Operators are the symbols that are used to perform some kind of
operation. Operators can be classified based on the type of operation
they perform.
There are the following types of operators in C:
Conditional Statements
Conditional statements are used to execute some block of code based
on whether the given condition is true. There are the following
conditional statements in C:
1. if Statement
2. if-else Statements
The if-else statement contains the else block in addition to the if block
which will be executed if the given condition is false.
Syntax if-else
if (expression) {
// if block
}
else {
// else block
}
3. if-else-if Ladder
C CheatSheet By Skills Taiyari Page 12
5. Conditional Operator
C CheatSheet By Skills Taiyari Page 13
int main()
{
// conditional operator will assign 10 if 5 < 25,
// otherwise it will assign 20
int i = 5 < 25 ? 10 : 20;
if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}
Output
i is 10
Loops
C CheatSheet By Skills Taiyari Page 14
Loops are the control statements that are used to repeat some block of
code till the specified condition is false. There are 3 loops in C:
1. for Loop
2. while Loop
The while loop is also an entry-controlled loop but only the condition is
the part of is syntax.
Syntax of while
while (condition) {
// initialization
}
3. do-while Loop
Jump Statements
Jump statements are used to override the normal control flow of the
program. There are 3 jump statements in C:
1. break Statement
It is used to terminate the loop and bring the program control to the
statements after the loop.
Syntax of break
break;
It is also used in the switch statement.
2. continue Statement
The continue statement skips the current iteration and moves to the
next iteration when encountered in the loop.
Syntax of continue
continue;
3. goto Statement
// Driver code
int main()
{
int i = 0;
// do_while loop
i = 1;
do {
printf("%d ", i++);
} while (i <= 10);
printf("\n");
// goto statement
i = 1;
C CheatSheet By Skills Taiyari Page 17
any_label:
printf("%d ", i++);
if (i <= 10) {
goto any_label;
}
return 0;
}
Output
12346 8 9 10
12345 6
12345 6 7 8 9 10
12345 6 7 8 9 10
C CheatSheet By Skills Taiyari Page 18
Arrays
An array is a fixed-size homogeneous collection of items stored at a
contiguous memory location. It can contain elements from type int,
char, float, structure, etc. to even other arrays.
Array provides random access using the element index.
Array size cannot change.
Array can have multiple dimensions in which it can grow.
Syntax of Arrays
Example of Arrays
int main()
{
// array declaration and initialization
int arr[5] = { 10, 20, 30, 40, 50 };
return 0;
}
C CheatSheet By Skills Taiyari Page 19
Output
Elements in Array: 10 20 100 40 50
C CheatSheet By Skills Taiyari Page 20
Strings
Strings are the sequence of characters terminated by a ‘\0’ NULL
character. It is stored as the array of characters in C.
Syntax of Strings
Example of Strings
#include <stdio.h>
#include <string.h>
int main()
{
// declare and initialize string
char str[] = "Geeks";
// print string
printf("%s\n", str);
int length = 0;
length = strlen(str);
return 0;
}
Output
Geeks
Length of string str is 5
C CheatSheet By Skills Taiyari Page 21
C String Functions
Pointers
Pointers are the variables that store the address of another variable.
They can point to any data type in C
Syntax of Pointers
data_type * ptr_name;
Note: The addressof (&) operator is used to get the address of a
variable.
We can dereference (access the value pointed by the pointer) using the
same * operator.
Example of Pointers
// Driver program
int main()
{
int var = 10;
Output
C CheatSheet By Skills Taiyari Page 23
Functions
Functions are the block of statements enclosed within { } braces that
perform some specific task. They provide code reusability and
modularity to the program.
Function Syntax is divided into three parts:
1. Function Prototype
2. Function Definition
3. Function Call
Example of Function
C CheatSheet By Skills Taiyari Page 25
// Driver code
int main()
{
// Calling sum function and
// storing its value in add variable
int add = sum(10, 30);
Output
Sum is: 40
Type of Function
1. malloc()
2. calloc()
The calloc() function allocates the number of blocks of the specified size
in the memory. It returns the void pointer to the memory block. If the
allocation is failed, it returns the null pointer.
Syntax
calloc (size_t num, size_t size);
3. realloc()
The realloc() function is used to change the size of the already allocated
memory. It also returns the void pointer to the allocated memory.
Syntax
realloc (void *ptr, size_t new_size);
4. free()
C CheatSheet By Skills Taiyari Page 27
int main()
{
// using malloc to allocate the int array of size 10
int* ptr = (int*)malloc(sizeof(int) * 10);
return 0;
}
Output
malloc Array Size: 10
calloc Array Size: 10
malloc Array Size after using realloc: 5
C CheatSheet By Skills Taiyari Page 28
Structures
A structure is a user-defined data type that can contain items of
different types as its members. In C, struct keyword is used to declare
structures and we can use ( . ) dot operator to access structure
members.
Structure Template
...{
...structure template...
}var1, var2..., varN;
or
strcut str_name var1, var2,...varN;
Example of Structure
};
// Driver code
int main()
{
// variable declaration after structure template
// initialization with initializer list and designated
// initializer list
struct str1 var1 = { 1, 'A', 1.00, "GeeksforGeeks" },
var2;
struct str2 var3 = { .ff = 5.00, .ii = 5, .cc = 'a' };
return 0;
}
Output
Struct 1:
i = 1, c = A, f = 1.000000, s = GeeksforGeeks
Struct 2:
i = 1, c = A, f = 1.000000, s = GeeksforGeeks
Struct 3
i = 5, c = a, f = 5.000000
C CheatSheet By Skills Taiyari Page 30
File Handling
File handling is the process of performing input and output on a file
instead of the console. We can store, retrieve, and update data in a file.
C supports text and binary files.
C File Operations
Preprocessor Directives
The preprocessor directives are used to provide instructions to the
preprocessor that expands the code before compilation. They start with
the # symbol.
The following table lists all the preprocessor directives in
C/C++:
C Math Functions