C Cheatsheet CodeWithHarry
C Cheatsheet CodeWithHarry
Basics
Basic syntax and functions from the C++ programming language.
Boilerplate
#include <iostream>
int main() {
return 0;
cout <<
cin >>
Data types
The data type is the type of data
Character type
char variable_name;
Integer type
1/15
Home - CodeWithHarry
int variable_name;
Float type
float variable_name;
Double type
double variable_name;
Void type
void
Boolean type
bool
Escape Sequences
It is a sequence of characters starting with a backslash, and it doesn't represent itself when used
inside string literal.
Alarm or Beep
\a
Backspace
It adds a backspace
2/15
Home - CodeWithHarry
\b
Form feed
\f
Newline
Newline Character
\n
Carriage return
\r
Tab
\t
Backslash
It adds a backslash
\\
Single quote
\'
Question mark
3/15
Home - CodeWithHarry
\?
Octal No.
\nnn
Hexadecimal No.
\xhh
Null
\0
Comments
A comment is a code that is not executed by the compiler, and the programmer uses it to keep
track of the code.
Multi-line comment
/* It's a
multi-line
comment
*/
Strings
4/15
Home - CodeWithHarry
Declaring String
#include <string>
// String variable
append function
length function
cout << "The length of the string is: " << variable1.length();
variable1[1] = 'i';
Maths
C++ provides some built-in math functions that help the programmer to perform mathematical
operations efficiently.
max function
5/15
Home - CodeWithHarry
min function
sqrt function
#include <cmath>
ceil function
ceil(x)
floor function
floor(x)
pow function
pow(x, y)
If Statement
6/15
Home - CodeWithHarry
if (condition) {
If-else Statement
if (condition) {
} else {
if else-if Statement
if (condition) {
// Statements;
else if (condition){
// Statements;
else{
// Statements
Ternary Operator
switch (expression)
case constant-expression:
statement1;
statement2;
7/15
Home - CodeWithHarry
break;
case constant-expression:
statement;
break;
...
default:
statement;
Iterative Statements
Iterative statements facilitate programmers to execute any block of code lines repeatedly and
can be controlled as per conditions added by the programmer.
while Loop
do-while loop
It is an exit controlled loop. It is very similar to the while loop with one difference, i.e., the body
of the do-while loop is executed at least once even if the condition is False
do
/* code */
for loop
It is used to iterate the statements or a part of the program several times. It is frequently used to
traverse the data structures like the array and linked list.
/* code */
}
8/15
Home - CodeWithHarry
Break Statement
break;
Continue Statement
continue keyword skips the rest of the current iteration of the loop and returns to the starting
point of the loop
continue;
References
Reference is an alias for an already existing variable. Once it is initialized to a variable, it cannot
be changed to refer to another variable. So, it's a const pointer.
Creating References
Pointers
Pointer is a variable that holds the memory address of another variable
Declaration
datatype *var_name;
var_name = &variable2;
Function Definition
9/15
Home - CodeWithHarry
//code to be executed
Function Call
function_name(arguments);
Recursion
Recursion is when a function calls a copy of itself to work on a minor problem. And the function
that calls itself is known as the Recursive function.
void recurse()
... .. ...
recurse();
... .. ...
Object-Oriented Programming
It is a programming approach that primarily focuses on using objects and classes. The objects
can be any real-world entities.
class
class Class_name {
// fields
// functions
// blocks
};
object
Class_name ObjectName;
Constructors
10/15
Home - CodeWithHarry
className() { // Constructor
};
int main() {
className obj_name;
return 0;
Encapsulation
Data encapsulation is a mechanism of bundling the data, and the functions that use them and
data abstraction is a mechanism of exposing only the interfaces and hiding the implementation
details from the user.
#include<iostream>
class ExampleEncap{
private:
* setter functions.
*/
int num;
char ch;
public:
* through them
*/
return num;
return ch;
11/15
Home - CodeWithHarry
*/
this->num = num;
this->ch = ch;
};
int main(){
ExampleEncap obj;
obj.setNum(100);
obj.setCh('A');
cout<<obj.getNum()<<endl;
cout<<obj.getCh()<<endl;
return 0;
File Handling
File handling refers to reading or writing data from files. C provides some functions that allow us
to manipulate data in the files.
#include <iostream>
#include <fstream>
int main() {
ofstream MyFile("filename.txt");
MyFile.close();
12/15
Home - CodeWithHarry
getline()
Opening a File
OPEN MODES
in
out
binary
app
Opens the file and appends all the outputs at the end
13/15
Home - CodeWithHarry
ate
Opens the file and moves the control to the end of the file
trunc
nocreate
noreplace
Closing a file
myfile.close()
Exception Handling
An exception is an unusual condition that results in an interruption in the flow of the program.
A basic try-catch block in python. When the try block throws an error, the control goes to the
except block
14/15
Home - CodeWithHarry
try {
// code to try
catch () {
15/15