C Language - Cheat Sheet
C Language - Cheat Sheet
C Language - Cheat Sheet
// Return o
return 0;
}
// Other user-defined function definition section
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 1/27
10/9/24, 11:16 PM C Language - Cheat Sheet
#include <stdio.h>
#include is a preprocessor directive that includes the header file in the C program. The
stdio.h is a header file where all input and output-related functions are defined.
main() Function
The main() function is an entry point of a C program, the program's executions start from
the main() function.
int main() {
return 0;
}
Comments
There are two types of comments in C language. Single-line and multi-line comments.
Comments are ignored by the compilers.
Single-line Comments
Use // to write a single-line comment.
Multi-line Comments
Use /* and */ before and after the text to write multi-line comments in C language.
/*
This is line 1
This is line 2
..
*/
Explore our latest online courses and learn new skills at your own pace. Enroll and
become a certified expert to boost your career.
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 2/27
10/9/24, 11:16 PM C Language - Cheat Sheet
Example
printf("Hello world");
scanf("format_specifier", &variable_name);
Format Specifiers
The following is the list of C format specifiers that are used in printf() and scanf()
functions to print/input specific type of values.
%c Character
%d Signed integer
%f Float values
%g or %G Similar as %e or %E
%i Unsigned integer
%lf Double
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 3/27
10/9/24, 11:16 PM C Language - Cheat Sheet
%o Octal representation
%p Pointer
%s String
%u Unsigned int
%x or %X Hexadecimal representation
Example
Open Compiler
#include <stdio.h>
int main(){
return 0;
}
Output
Age: 18
Percent: 67.750000
Data Types
The data types specify the type and size of the data to be stored in a variable. Data types
are categorized in 3 sections −
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 4/27
10/9/24, 11:16 PM C Language - Cheat Sheet
Array
Pointer
Structures
Unions
Enumerations
The printf() function is used to print the formatted text on the console.
printf("Hello world");
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 5/27
10/9/24, 11:16 PM C Language - Cheat Sheet
Open Compiler
#include <stdio.h>
int main() {
int num;
return 0;
}
Output
Identifiers
C identifiers are user-defined names for variables, constants, functions, etc. The following
are the rules for defining identifiers −
Only alphabets, underscore symbol (_), and digits are allowed in the identifier.
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 6/27
10/9/24, 11:16 PM C Language - Cheat Sheet
Keywords
C keywords are the reversed words in the C compiler, which are used for specific purposes
and must not be used as an identifier.
do if static while
Variables
C variables are the name given to a storage area that our programs can use to access and
manipulate the data.
data_type variable_name;
Escape Sequences
Escape sequences are the special characters followed by the escape (backward slash \).
Escape sequences have special meanings and are used for printing those characters that
cannot be printed normally.
\\ \ character
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 7/27
10/9/24, 11:16 PM C Language - Cheat Sheet
\? ? character
\a Alert or bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
Operators
Operators are the special symbols that are used to perform specific mathematical or
logical operations.
Arithmetic
+, -, *, /, % Performs arithmetic operations.
Operators
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 8/27
10/9/24, 11:16 PM C Language - Cheat Sheet
Operators . operations.
Example of Operators
Conditional Statements
C language provides the following conditional statements −
if Statement
if-else Statement
if-else-if Statement
Switch Statement
Ternary Operator
if Statement
An if statement consists of a Boolean expression followed by one or more statements.
if(boolean_expression) {
/* statement(s) will execute if the boolean expression is true */
}
if-else statement
An if-else statement can be followed by an optional else statement, which executes when
the Boolean expression is false.
if (Boolean expr){
Expression;
. . .
}
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 9/27
10/9/24, 11:16 PM C Language - Cheat Sheet
else{
Expression;
. . .
}
if-else-if Statement
The if-else-if statement is also known as the ladder if-else. It is used to check multiple
conditions when a condition is not true.
if(condition1){
}
else if(condition2){
}
…
else{
}
Nested if Statements
By using the nested if statements, you can use one if or else-if statement inside another if
or else-if statement(s).
if (expr1){
if (expr2){
block to be executed when
expr1 and expr2 are true
}
else{
block to be executed when
expr1 is true but expr2 is false
}
}
Switch Statement
A switch statement allows a variable to be tested for equality against a list of values.
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 10/27
10/9/24, 11:16 PM C Language - Cheat Sheet
switch (Expression){
Ternary Operator
The ternary operator (?:) is also known as the conditional operator. It can be used as a
replacement for an if-else statement.
Loops
C loops are used to execute blocks of one or more statements respectively a specified
number of times, or till a certain condition is reached. The following are the loop
statements in C language −
while Loop
do...while Loop
for Loop
while Loop
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 11/27
10/9/24, 11:16 PM C Language - Cheat Sheet
The while loop is an entry-control loop where the condition is checked before executing the
loop body.
while(test_condition){
// Statement(s);
}
do…while Loop
The do…while loop is an exit control loop where the body of the loop executes before
checking the condition.
do{
// Statement(s);
}while(test_condition);
for Loop
The for loop is also an entry-controlled loop where the elements (initialization, test
condition, and increment) are placed together to form a for loop inside the parenthesis
with the for keyword.
Jump Statements
Jump statements are used to transfer the flow of the program from one place to another.
The following are the jump statements in C language −
goto Statement
break Statement
continue Statement
goto Statement
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 12/27
10/9/24, 11:16 PM C Language - Cheat Sheet
The goto statement transfers the program's control to a specific label. You need to define a
label followed by the colon (:). The goto statement can transfer the program's flow up or
down.
label_name:
//Statement(s)
if(test_condition){
goto label_name;
}
break Statement
The break statement can be used with loops and switch statements. The break statement
terminates the loop execution and transfers the program's control outside of the loop
body.
while(condition1){
. . .
. . .
if(condition2)
break;
. . .
. . .
}
continue Statement
The continue statement is used to skip the execution of the rest of the statement within
the loop in the current iteration and transfer it to the next loop iteration.
while (expr){
. . .
. . .
if (condition)
continue;
. . .
}
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 13/27
10/9/24, 11:16 PM C Language - Cheat Sheet
User-defined Functions
The user-defined function is defined by the user to perform specific tasks to achieve the
code reusability and modularity.
Open Compiler
#include <stdio.h>
// Function declaration
int add(int, int);
// Function definition
int add(int a, int b) { return (a + b); }
int main() {
int num1 = 10, num2 = 10;
int res_add;
return 0;
}
Output
Addition is : 20
Arrays
An array is a collection of data items of similar data type which are stored at a contiguous
memory location. The data item may be primary data types (int, float, char), or user-
defined types such as struct or pointers can be stored in an array.
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 14/27
10/9/24, 11:16 PM C Language - Cheat Sheet
Syntax of Arrays
The following is the syntax of declarations of different types of arrays −
Open Compiler
#include <stdio.h>
int main(){
int numbers[5] = {10, 20, 30, 40, 50};
return 0;
}
Output
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 15/27
10/9/24, 11:16 PM C Language - Cheat Sheet
Open Compiler
#include <stdio.h>
int main () {
return 0;
}
Output
a[0][0] = 0
a[0][1] = 0
a[1][0] = 1
a[1][1] = 2
a[2][0] = 2
a[2][1] = 4
a[3][0] = 3
a[3][1] = 6
a[4][0] = 4
a[4][1] = 8
Strings
C string is a sequence of characters i.e., an array of char data type, terminated by "null
character" represented by '\0'. To read and print the string using scanf() and printf()
functions, you will have to use the "%s" format specifier.
String Declaration
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 16/27
10/9/24, 11:16 PM C Language - Cheat Sheet
char string_name[size];
Reading String
scanf("%s", string_name);
Printing String
printf("%s", string_name);
Example of C strings
#include <stdio.h>
int main() {
char name[20];
return 0;
}
Strings Functions
C standard library string.h provides various functions to work with the strings. Here is the
list of C string functions −
char Appends the string pointed to, by src to the end of the string
1
*strcat pointed to by dest.
char Appends the string pointed to, by src to the end of the string
2
*strncat pointed to, by dest up to n characters long.
char Searches for the first occurrence of the character c (an unsigned
3
*strchr( char) in the string pointed to, by the argument str.
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 17/27
10/9/24, 11:16 PM C Language - Cheat Sheet
int
5 Compares at most the first n bytes of str1 and str2.
strncmp
char
7 Copies the string pointed to, by src to dest.
*strcpy
char Searches an internal array for the error number errnum and
10
*strerror returns a pointer to an error message string.
size_t Computes the length of the string str up to but not including the
11
strlen terminating null character.
char Finds the first character in the string str1 that matches any
12
*strpbrk character specified in str2.
char Searches for the last occurrence of the character c (an unsigned
13
*strrchr char) in the string pointed to by the argument str.
char
16 Breaks string str into a series of tokens separated by delim.
*strtok
size_t Transforms the first n characters of the string src into current
17
strxfrm locale and places them in the string dest.
Structures
C structures are the collection of different data types. Structures are considered user-
defined data types where you can group data items of different data types.
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 18/27
10/9/24, 11:16 PM C Language - Cheat Sheet
struct struct_name {
type1 item1;
type2 item2;
.
.
}structure_variable;
Example of Structure
Open Compiler
#include <stdio.h>
struct book{
char title[10];
char author[20];
double price;
int pages;
};
int main(){
struct book book1 = {"Learn C", "Dennis Ritchie", 675.50, 325};
Output
Title: Learn C
Author: Dennis Ritchie
Price: 675.500000
Pages: 325
Size of book struct: 48
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 19/27
10/9/24, 11:16 PM C Language - Cheat Sheet
Unions
C union is a user-defined data type that allows to store set of data items of different data
types in the same memory location.
Example of Union
Open Compiler
#include <stdio.h>
union Data{
int i;
float f;
};
int main(){
union Data data;
data.i = 10;
data.f = 220.5;
Output
data.i: 1130135552
data.f: 220.500000
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 20/27
10/9/24, 11:16 PM C Language - Cheat Sheet
Enumerations (enums)
C enumeration (enum) is an enumerated data type that consists of a group of integral
constants.
Open Compiler
#include <stdio.h>
int main() {
// Printing values
printf("OKAY = %d\n", OKAY);
printf("CANCEL = %d\n", CANCEL);
printf("ALERT = %d\n", ALERT);
return 0;
}
Output
OKAY = 1
CANCEL = 0
ALERT = 2
Pointers
C pointers are derived data types that are used to store the address of another variable
and can also be used to access and manipulate the variable's data stored at that location.
data_type *pointer_name;
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 21/27
10/9/24, 11:16 PM C Language - Cheat Sheet
pointer_name = &variable_name;
Pointer Example
Open Compiler
#include <stdio.h>
int main() {
int x = 10;
return 0;
}
Output
Value of x = 10
Value of x = 20
Type of Pointers
There are various types of pointers in C language. They are −
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 22/27
10/9/24, 11:16 PM C Language - Cheat Sheet
Null pointer
Wild pointer
Void pointer
Dangling pointer
Complex pointers
Far pointer
File pointers
Double pointers
Near pointer
malloc()
calloc()
realloc()
free()
malloc() Function
The malloc() function allocates the requested memory (number of blocks of the specified
size) and returns a pointer to it.
calloc() Function
The calloc() function allocates the requested memory (number of blocks of the specified
size) and returns the void pointer. The calloc() function sets allocated memory to zero.
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 23/27
10/9/24, 11:16 PM C Language - Cheat Sheet
realloc() Function
The realloc() function attempts to resize the memory block pointed to by a pointer that
was previously allocated with a call to malloc() or calloc() function.
free() Function
The free() function deallocates the memory previously allocated by a call to calloc(),
malloc(), or realloc().
File Handling
File handling refers to perform various operations on files such as creating, writing,
reading, deleting, moving, renaming files, etc. C language provides various functions for
file handling.
File Operations
The following are the operations that can perform a file using C language file handling
functions −
Renaming a file
Deleting a file
Closing a file
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 24/27
10/9/24, 11:16 PM C Language - Cheat Sheet
Function Description
Open Compiler
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file;
char file_name[] = "my_file.txt";
char write_data[100] = "Tutorials Point";
char read_data[100];
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 25/27
10/9/24, 11:16 PM C Language - Cheat Sheet
return 0;
}
Output
File's data:
Tutorials Point
Preprocessor Directives
The preprocessor directives are part of pre-compilation and start with the hash (#)
character. These directives instruct the compiler to expand include and expand the code
before starting the process of compilation.
Directive Description
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 26/27
10/9/24, 11:16 PM C Language - Cheat Sheet
#define MAX_ARRAY_LENGTH 20
Standard Libraries
Here is the list of commonly used libraries (C header files) −
Header
Usage
file
https://www.tutorialspoint.com/cprogramming/c_language_cheatsheet.htm 27/27