Uploaded by Ahnaf Hasan

C++ programming language

advertisement
Learning C++
Programming Language
Hand-note
Ahnaf Hasan Aunim
BAUET EEE’ 15
Comments
Comments can be used to explain the code. It can also be used to prevent code to
execute instead of deleting it when testing alternative code. Comments can be singlelined and multi-lined.
Single-lined Comments are written with //. Any text in a single line after a // is
commented out and will be ignored by the compiler.
Variables
Variables are containers for storing data values in programming.
They are declared with variable type and variable name using different keywords.
Variable Type
There are different data types in C++. We can store whole numbers, decimal numbers,
lists of data, stacks, boolean values, short and long integers, etc. But we will start with
introducing the main, most important, basic ones.
Variable types used in C++ are:
int
- stores integers (whole numbers), without decimals - 12, 55, 78, 543
double
char
- stores single characters. Char values are surrounded by single quotes - 'a', 'X'
string
bool
- stores floating point numbers, with decimals - 28.5, 13.7, 45.3
- stores text. String values are surrounded by double quotes - "Hello,
- stores values with two states: true or false
World!"
Variable Names
Every variable in our C++ code has its own unique variable name. These unique names
that variables have are called identifiers.
The rules that we follow when we are declaring variables are:
 Names can contain letters, digits and underscores
 Names must begin with a letter or an underscore (_), not with a number
 Names are case sensitive ( myName and myname are different variables)
 Names cannot contain whitespaces or special characters like !, #, %, etc.
 Reserved words (like C++ keywords, such as int) cannot be used as names
Numbers - Integers
The int keyword is used for declaring integers (whole numbers) - 25, 64, 245.
The size of the int data type is 4 bytes. It can store whole numbers from -2147483648 to
2147483647.
int x = 5;
There are lot more data types that are used to store whole numbers, because in
different
scenarios we need to store different magnitudes of numbers. An int can store a number
between -2147483648 and 2147483647 which forces the system to allocate a lot of
memory. We will only mention them just so you know that they exist. You don't have to
learn them for now or get comfortable using them.
The following table shows different data types used for storing whole numbers, their
size and what is the range that can be stored.
Type
Size
Range
int
4 bytes
-2147483648 to 2147483647
unsigned int
4 bytes
0 to 4294967295
short int
2 bytes
-32768 to 32767
unsigned short int
2 bytes
0 to 65,535
long int
8 bytes
-2,147,483,648 to 2,147,483,647
unsigned long int
8 bytes
0 to 4,294,967,295
long long int
8 bytes
-(2^63) to (2^63)-1
unsigned long long int
8 bytes
0 to 18,446,744,073,709,551,615
There are a lot of data types and you don't have to know them. For now you will only
use the int data type. But it's good to know that if you need only positive numbers you
can make the upper bound of the int data type double its size by using unsigned int . By
default the data types are signed, so they have values below zero. If you are solving a
problem that needs low memory usage, you can use the short int. It has a shorter
range of numbers but it's half the size of the int. From the other hand, if you are in a
scenario where you need to store really large numbers you can use the long or long
long int, that has a much larger range of numbers but it's double the size of the int.
For now, don't worry about the other data types. Remember that when you need to
store a whole number (integer) you are using the int keyword.
Download