- Introduction
- Comments
- Variables
- Input and Output
- Keywords and Identifiers
- Literals and Operators
- Final Note
- All C/C++ programs must have a
main()
function. - A function named
main
(lower case only). When a cpp program is executed it executes all statements in main in sequential order.
// library imported to acess in built input and output functions such as cout and cin
#include <iostream>
// special function main int stands for integer and signifies the return type of the function
int main(){
// printing out a phrase '\n' is a newline character to move
std::cout << "Hello, World!\n";
// returning 0 which is an integer :p as we specified int as return type
return 0;
}
In c++ there are single line comments //
and multi line comments /*
. Comments are not compiled by the compiler and
usually exists for notes and instructions for ourselever or other developers making our intentions clear.
// This is a single line comment
/*
Line 1
Line 2
And thats it
*/
Note
You can put single line comment into multiline comment. But you cannot nest mutliline comments in another multiline comment.
// VALID
/*
My multi
line
// comment
*/
// INVALID
/* This is another comment /* and another one */ this part is actually not a comment */
All computers have memory, called RAM, that is available for your programs to use. You can think of RAM as a series
of numbered mailboxes that can each be used to hold a piece of data while the program is running. A single piece of
data, stored in memory somewhere, is called a value. In C++ direct access to this memory is discouraged unlike older
langauges like BASIC
. So unlike before like get the value from mailbox 752
we can say get the value from
this object
. A named object is a Variable and the name is an Identifier.
int x; // A variable named x is declared of type int
Note
When the program is run the variable will be instantiated ie a fancy abbreviation of saying the object will be created and assigned a memory address to the memory which was allocated for this object.
Click to read more about data types and slightly more advanced stuff
A data type tells the compiler what type of value (e.g. a number, a letter, text, etc…) the variable will store.
Note
The data type of a variable cannot be changed after it has been declared.
As the name suggests these are the data types that are built into the language. These are the basic building blocks of the language. These are the data types that are used to create more complex data types.
Data Type | Description | Size | Range |
---|---|---|---|
bool |
Boolean value | 1 byte | true or false |
char |
Character | 1 byte | -128 to 127 or 0 to 255 |
int |
Integer | 4 bytes | -2,147,483,648 to 2,147,483,647 |
float |
Floating point number | 4 bytes | 3.4e−038 to 3.4e+038 |
double |
Floating point number | 8 bytes | 1.7e−308 to 1.7e+308 |
void |
No value | - | - |
wchar_t |
Wide character | 2 bytes | 0 to 65,535 |
Note
The size of the data types may vary depending on the compiler and the operating system.
bool isHappy = true;
char letter = 'a';
int age = 21;
float pi = 3.14;
double bigPi = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912;
void nothing = null;
wchar_t wideChar = L'x'; // L is a prefix to tell the compiler that the character is wide
These are the data types that are created using the inbuilt data types. These are more complex data types.
Data Type | Description |
---|---|
array |
An array is a collection of variables of the same type. |
pointer |
A pointer is a variable that stores the memory address of another variable. |
reference |
A reference is an alias for another variable. |
function |
A function is a group of statements that together perform a task. |
int arr[5]; // An array of 5 integers
int *ptr; // A pointer to an integer
int &ref = x; // A reference to an integer
int func(int a, int b); // A function that takes two integers and returns an integer
These are the data types that are created by the user. These are more complex data types.
Data Type | Description |
---|---|
class |
A class is a user-defined data type that can contain data members and member functions. |
struct |
A struct is a user-defined data type that combines objects of different data types. |
union |
Similar to a struct but all members share the same memory location. |
enum |
An enum is a user-defined data type that consists of integral constants. |
typedef |
A typedef is a user-defined data type that is an alias for another data type. |
class Person {
public:
string name;
int age;
void sayHello() {
cout << "Hello, my name is " << name << " and I am " << age << " years old." << endl;
}
};
struct Point {
int x;
int y;
};
union Data {
int i;
float f;
char str[20];
}; // size of union is the size of the largest member here it is 20 bytes
enum Color {
RED,
GREEN,
BLUE
}; // RED = 0, GREEN = 1, BLUE = 2
typedef int feet;
These are the keywords that are used to modify the data types.
Modifier | Description |
---|---|
signed |
The signed modifier is used to specify that a variable can hold both positive and negative values. |
unsigned |
The unsigned modifier is used to specify that a variable can hold only positive values. |
short |
The short modifier is used to specify that a variable is a short integer. |
long |
The long modifier is used to specify that a variable is a long integer. |
signed int i;
unsigned int j;
short int k;
long int l;
Note
No need to fuss or worry about the above data types. Just remember that there are 3 types of data types inbuilt, derived and abstract. And that's it. We will cover each of these data types in detail and more in the upcoming chapters. The above table is just for reference and to give you an idea of what is there in the language.
A variable can be initialized at the time of declaration. This means that a variable can be assigned a value at the time of its declaration. The value used to initialize a variable is called the initializer.
int x; // no initializer (x is uninitialized)
int y = 5; // initializer is 5 (copy initialization)
int z(5); // initializer is 5 (direct initialization)
// List initialization (since C++11)
int a{5}; // initializer is 5 (uniform initialization)
int b = {5}; // initializer is 5 (copy list initialization)
int c{}; // initializer is 0 (value initialization)
Note
Copy initialization is also used whenever values are implicitly copied from one variable to another. For example, when a function returns a value, the returned value is copied to the variable that is used to store the return value. This is called copy initialization.
Note
Direct initialization is also used when values are explicitly cast from one type to another.
Try to avoid direct initialization as much as possible since it cause ambiguity and confusion in code. For example, it makes it difficult to distinguish between a function declaration and a variableint x(); // forward declaration of function x int x(5); // direct initialization of variable x with value 5
Modern way of initializing variables is to use list initialization also known as uniform initialization or brace initialization. This is the recommended way of initializing variables. It has the following advantages:
- It is more readable and understandable.
- It does not allow narrowing conversions.
int x{6.9}; // error: narrowing conversion from double to int
Multiple variables can be initialized in a single statement.
int x { 5 }, y { 6 }, z { 7 };
Note
Modern compilers will typically generate warnings if a variable is initialized but not used (since this is rarely desirable). And iftreat warnings as errors
is enabled, these warnings will be promoted to errors and cause the compilation to fail.int main() { int x { 5 }; // warning: unused variable 'x' return 0; }To avoid this, you can use the
[[maybe_unused]]
attribute to suppress the warning. Since C++17, this attribute is part of the language and can be used to suppress warnings about unused variables.int main() { [[maybe_unused]] int x { 5 }; return 0; }
Unlike other programming languages, C++ does not initialize variables to a default value. This means that if a variable is declared but not initialized, it will contain garbage data. This is called an uninitialized variable.
#include <iostream>
int main() {
int x;
std::cout << x << std::endl; // garbage value
return 0;
}
Undefined behavior is a behavior of a program that is not defined by the language specification. This means that the behavior of the program is not specified by the language specification and can vary from one compiler to another. This can lead to unexpected results and can cause the program to crash.
Symptoms of undefined behavior:
- Produces different results each time the program is run.
- Incorrect results.
- Inconsistent behavior sometimes correct and sometimes incorrect.
- Program crashes either immediately or after some time.
- Different results on different compilers.
- Different results on different platforms (operating systems).
C++ provides a set of built-in functions to perform input and output operations. These functions are defined in the
iostream
header file.
#include <iostream>
std::cout
is an object of std::ostream
class. It is used to display output on the standard output device
which is usually the monitor. It allows us to insert data into the output stream.
A stream is a sequence of characters. It is a sequence of bytes that are sent to or received from a device.
Meaning in the case of std::cout
it is a sequence of bytes that are sent to the monitor a set of them are
displayed at a time. So it is similar in priciple to a movie. A movie is a sequence of frames that are sent to
the monitor and displayed one after the other and they wait for a certain amount of time and then the next frame
is displayed and so on.
cout
-> character output
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
<<
is the insertion operator. It is used to insert data into the output stream.
std::endl
is an object of std::ostream
class. It is used to insert a new line character into the output
stream and flushes the stream.
endl
-> end line
- It helps indicate the end of a line similar to
.
in a sentence. - It positions the cursor at the beginning of the next line.
- It flushes the stream.
#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}
std::endl
is not necessary. It is used to flush the stream. But flushing the stream is not necessary. It is
done periodically by the operating system. So it is not necessary to use std::endl
to flush the stream.
Hence it is not necessary to use std::endl
to insert a new line character.
#include <iostream>
int main() {
std::cout << "Hello World!\n";
return 0;
}
std::cin
is an object of std::istream
class. It is used to read input from the standard input device
which is usually the keyboard. It allows us to extract data from the input stream.
cin
-> character input
#include <iostream>
int main() {
int x { };
std::cin >> x;
return 0;
}
>>
is the extraction operator. It is used to extract data from the input stream.
Note
Anenter
key press is always required after the input is entered.
Keywords are reserved words that have a predefined meaning in the language. They cannot be used as identifiers or variable names. C++ has a reserved set of 92 keywords as of C++20.
An identifier is a name used to identify a variable, function, class, module, or any other user-defined item.
- It cannot be a keyword.
- It can be a combination of letters, digits, and underscores (
_
). - It must start with an alphabet or an underscore (
_
). - It is case sensitive.
- Use meaningful names.
- Use camelCase for function names.
- Use PascalCase for class names.
- Use snake_case for file names.
- Use UPPER_CASE for constants.
- Use LOWER_CASE for variables.
A literal is a notation for representing a fixed value in source code. Literals are represented directly in the source code and are not stored in memory.
#include <iostream>
int main() {
std::cout << 5; // 5 is a literal
return 0;
}
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.
Operator | Description |
---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Modulus |
#include <iostream>
int main() {
std::cout << 5 + 2 << std::endl; // 7
std::cout << 5 - 2 << std::endl; // 3
std::cout << 5 * 2 << std::endl; // 10
std::cout << 5 / 2 << std::endl; // 2
std::cout << 5 % 2 << std::endl; // 1
return 0;
}
Operator | Description |
---|---|
= |
Assigns values from right side operands to left side operand |
+= |
Add AND |
-= |
Subtract AND |
*= |
Multiply AND |
/= |
Divide AND |
%= |
Modulus AND |
#include <iostream>
int main() {
int x { 5 };
x += 2; // x = x + 2
std::cout << x << std::endl; // 7
x -= 2; // x = x - 2
std::cout << x << std::endl; // 5
x *= 2; // x = x * 2
std::cout << x << std::endl; // 10
x /= 2; // x = x / 2
std::cout << x << std::endl; // 5
x %= 2; // x = x % 2
std::cout << x << std::endl; // 1
return 0;
}
Operator | Description |
---|---|
== |
Checks if the values of two operands are equal or not, if yes then condition becomes true. |
!= |
Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. |
> |
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. |
< |
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. |
>= |
Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. |
<= |
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. |
#include <iostream>
int main() {
std::cout << (5 == 2) << std::endl; // 0
std::cout << (5 != 2) << std::endl; // 1
std::cout << (5 > 2) << std::endl; // 1
std::cout << (5 < 2) << std::endl; // 0
std::cout << (5 >= 2) << std::endl; // 1
std::cout << (5 <= 2) << std::endl; // 0
return 0;
}
Operator | Description |
---|---|
&& |
Logical AND |
║ |
Logical OR |
! |
Logical NOT |
#include <iostream>
int main() {
std::cout << (true && true) << std::endl; // 1
std::cout << (true && false) << std::endl; // 0
std::cout << (false && true) << std::endl; // 0
std::cout << (false && false) << std::endl; // 0
std::cout << (true || true) << std::endl; // 1
std::cout << (true || false) << std::endl; // 1
std::cout << (false || true) << std::endl; // 1
std::cout << (false || false) << std::endl; // 0
std::cout << !true << std::endl; // 0
std::cout << !false << std::endl; // 1
return 0;
}
Operator | Description |
---|---|
& |
Bitwise AND |
│ |
Bitwise OR |
^ |
Bitwise XOR |
~ |
Bitwise NOT |
<< |
Bitwise left shift |
>> |
Bitwise right shift |
#include <iostream>
int main() {
std::cout << (5 & 2) << std::endl; // 0
std::cout << (5 | 2) << std::endl; // 7
std::cout << (5 ^ 2) << std::endl; // 7
std::cout << (~5) << std::endl; // -6
std::cout << (5 << 1) << std::endl; // 10
std::cout << (5 >> 1) << std::endl; // 2
return 0;
}
Operator | Description |
---|---|
++ |
Increment |
-- |
Decrement |
#include <iostream>
int main() {
int x { 5 };
std::cout << x++ << std::endl; // 5
std::cout << x << std::endl; // 6
std::cout << ++x << std::endl; // 7
std::cout << x << std::endl; // 7
std::cout << x-- << std::endl; // 7
std::cout << x << std::endl; // 6
std::cout << --x << std::endl; // 5
std::cout << x << std::endl; // 5
return 0;
}
The ternary operator is the only operator that takes three operands.
#include <iostream>
int main() {
int x { 5 };
int y { 2 };
std::cout << (x > y ? x : y) << std::endl; // 5
return 0;
}
Note: For operators such as
=
or<<
we call for their effect to be applied to the left operand.// For example x = y = 9 is equivalent to: x = (y = 9); // 9 is assigned to y and then y is assigned to x std::cout << "Hello" << "World"; evaluates to: (std::cout << "Hello") << "World"; and (std::cout << "Hello") returns a reference to std::cout, so the expression evaluates to std::cout << "World";
Note
New programmers often attempt to write the whole program at once and get overwhelmed by numerous errors. This is why it is important to write small programs and test them as you go along. This will help you to identify and fix errors as you go along. It is also important to write programs that are easy to understand. This will help you to debug your programs easily. It is recommended to print out the output of your programs as you go along. This will help you to identify errors easily.
⏭️ Next | 🔝 Top |
---|