C++ Programming language Cheatsheet

by anupmaurya

C++ is a widely used middle-level programming language which is used in developing major operating systems( Windows, Linux, Android, Ubuntu, iOS etc), Games, databases and compilers etc.


  • cin >> x– read value into the variable x from input stream
  • cout << x — printf value to the output stream
  • // — single line comments
  • /* */ — Multi line comments

Sample C program

#include <iostream> using namespace std; int main() { cout << "Hello World!!"; return 0; }
Code language: PHP (php)
  • #include <iostream> — iostream is a inbuilt header library which allows you to deal with input and output objects like cout etc.
  • using namespace std — Specifies that the object and variable names can be used from standard library.
  • cout — to print the output.
  • main() — main function is the entry point of any C++ program.
  • return 0 — To end the main function

How to compile a program in C++

Open your terminal, Navigate to the directory where you have saved your program. Consider firstprogram.cpp is the name of your program.

sudo g++ -o firstprogram firstprogram.cpp
Code language: CSS (css)

How to run a C++ program


Data types

Basicint, char, float, double, short, short int, long int etc
Derivedarray, pointer etc
User Defined Data Typestructure, enum, Class, Union, Typedef


data-type variable-name = value;
int value = 10; // declaring int variable and assigning value 10 to it char grade = 'A'; // declaring char variable and assigning value A to it
Code language: PHP (php)

Naming convention

  • only letters (both uppercase and lowercase letters), digits and underscore(_).
  • cannot contain white spaces
  • First letter should be either a letter or an underscore(_).
  • Variable type can’t be changed
  • Case sensitive


data-type array-name[size]; // one-dimensional Array data-type array-name[size][size]; // two-dimensional Array
Code language: PHP (php)


int a[5] = {1,2,3,4,5}; int a[2][3] = { {1,2,3}, {4,5,6} };

Literals or Constants

Integer Literal- decimal255
Integer Literal- octal0377
Integer Literal- hexadecimal0xFF
Float point Literal53.0f, 79.02
Character literals‘a’, ‘1’
String literals“OneCompiler”, “Foo”

Escape sequences

Escape sequenceDescription
\nNew line
\rCarriage Return
?Question mark
\tHorizontal tab
\vVertical tab
\fForm feed
Single quotation
Double quotation
\0Null character
\bBack space


Operator typeDescription
Arithmetic Operator+ , – , * , / , %
comparision Operator< , > , <= , >=, != , ==
Bitwise Operator& , ^ ,
Logical Operator&& , `
Assignment Operator= , += , -= , *= , /= , %=, <<=, >>=, &=, ^=, `
Ternary Operator? :
sizeof operatorsizeof()

Conditional Statements

1. If

if(conditional-expression) { //code }
Code language: JavaScript (javascript)

2. If-else

if(conditional-expression) { //code } else { //code }
Code language: JavaScript (javascript)

3. If-else-if ladder

if(conditional-expression-1) { //code } else if(conditional-expression-2) { //code } else if(conditional-expression-3) { //code } .... else { //code }
Code language: PHP (php)

4. Switch

switch(conditional-expression){ case value1: //code break; //optional case value2: //code break; //optional ... default: //code to be executed when all the above cases are not matched; }
Code language: PHP (php)


1. For

for(Initialization; Condition; Increment/decrement){ //code }
Code language: JavaScript (javascript)

2. While

while(condition){ //code }
Code language: JavaScript (javascript)

3. Do-While

do{ //code } while(condition);
Code language: JavaScript (javascript)


Function is a sub-routine which contains set of statements.

// declaring a function return_type function_name(parameters); // defining a function return_type function_name(parameters){ //code } // calling a function function_name (parameters)
Code language: JavaScript (javascript)


Pointer is a variable which holds the memory information(address) of another variable of same data type.

datatype *pointername;


int x = 10, *ptr; /*ptr = x; // Error because ptr is adress and x is value *ptr = &x; // Error because x is adress and ptr is value */ ptr = &x; // valid because &x and ptr are addresses *ptr = x; // valid because both x and *ptr values
Code language: JavaScript (javascript)


Structure is a user-defined data type where it allows you to combine data of different data types.

struct structure_name { member definition; member definition; ... member definition; } [one or more structure variables]; struct structure_name variable name; //declaring structure variables
Code language: JavaScript (javascript)


Enumeration data type is a user-defined data type in C++. enum keyword is used to declare a new enumeration types in C++.

enum name{constant1, constant2, constant3, ....... } var-list;
Code language: PHP (php)


enum month{January, February, March, April, May, June, July, August, September, October, November, December} name;


Typedef is used to explicitly define new data type names by using the keyword typedef. It defines a name for an existing data type but doesn’t create a new data type.

typedef data-type name;


typedef unsigned int distance; // typedef of int
Code language: JavaScript (javascript)

If you have quires and suggestions put them in the comments.Hope this C++ Programming language Cheat-sheet useful to you, Share & Help Other to learn 🙂

You may also like