Naming Variables
C++ encourages user declare meaningful variables. For instance, to express “cost of trip”, the corresponding variable can be named as cost_of_trip
or CostOfTrip
, instead of cot
, x
, …
The following are valid and invalid C++ variable names
1
2
3
4
5
6
7
8
9
10
11
int lion; //valid
int Lion; //valid and more distinct
int LION; //valid and even more distinct
Int rabbit; // invalid, not "Int"
int rabbit_1; //valid
int _rabbit; //valid
int 4you; //invalid, can't start with a number
int come2us; //valid
int rabbit2; //valid
int double; //invalid, "double" is a reserved word in c++
int rabbit-turtle; //invalid, no hyphen allowed
Basic Types
short
A short cut of short int
, which has width of 16 bits. The range of short
is $ -32,768 $ to $ 32,767 $
int
Basic integer type, which has at least 16 bits. But it always has 32 bits on general PC (32/64 bit systems). The range of int
is $ -2,147,483,648 $ to $ 2,147,483,647 $
long
A short cut of long int
. This type has at least 32 bits. So it width is the same as int
on PC.
long long
Target type will have width of at least 64 bits. the range is $ -9,223,372,036,854,775,808 $ to $ 9,223,372,036,854,775,807 $
float
Single precision floating point type. Usually use IEEE-754 32 bit as format. Its range is $ ± 3.4028234 · 10^{38} $
double
Double precision floating point type. Usually use IEEE-754 64 bit as format. Its range is $ ± 1.7976931348623157 · 10^{308} $
char
Type for storing text code, which has width of 1 bit. Usually, one char
stores one ASCII code, every ASCII code correspond with a common character. For example, 65 for ‘A’.
Check Widths of Basic Types
We can use sizeof() function to check the width of every type.
1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
using namespace std;
int main() {
cout << "width of short is " << sizeof(short) << " bytes" << endl;
cout << "width of int is " << sizeof(int) << " bytes" << endl;
cout << "width of long is " << sizeof(long) << " bytes" << endl;
cout << "width of long long is " << sizeof(long long) << " bytes" << endl;
cout << "width of float is " << sizeof(float) << " bytes" << endl;
cout << "width of double is " << sizeof(double) << " bytes" << endl;
cout << "width of char is " << sizeof(char) << " byte" << endl;
return 0;
}
Note that 1 byte equals 8 bit. The result will be:
1
2
3
4
5
6
7
width of short is 2 bytes
width of int is 4 bytes
width of long is 4 bytes
width of long long is 8 bytes
width of float is 4 bytes
width of double is 8 bytes
width of char is 1 byte
By the way, we can also use sizeof() to check a variable’s width, such as:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
using namespace std;
int main() {
int int_num = 33030;
double double_num = 5.55;
char str[20] = "Hello World";
int int_array[5] = {0}; //{0} is used for initialize all integers in array to 0
cout << "width of int_num is " << sizeof(int_num) << " bytes" << endl;
cout << "width of double_num is " << sizeof(double_num) << " bytes" << endl;
cout << "width of str is " << sizeof(str) << " bytes" << endl;
cout << "width of int_array is " << sizeof(int_array) << " bytes" << endl;
return 0;
}
1
2
3
4
width of int_num is 4 bytes
width of double_num is 8 bytes
width of str is 20 bytes
width of int_array is 20 bytes
Signed Types
In order to simplify the calculation, short, int, long, long long
follow 2’s complement format. The leftmost bit of a signed integer (sign bit) is 0 if the number is positive or zero, 1 if it’s negative. For instance, the largest 32-bit int
is
$ 01111111111111111111111111111111 $
which has the value $ 2,147,483,647\ (2^{31} – 1) $ .
Here are some other examples:
$ 01111111111111111111111111111110 => 2,147,483,646\\01111111111111111111111111111101 => 2,147,483,645\\01111111111111111111111111111100 => 2,147,483,644\\…\\00000000000000000000000000000010 => 2\\00000000000000000000000000000001 => 1\\00000000000000000000000000000000 => 0 $
While sign bit is 1 such as
$ 11111111111111111111111111111111 => -1\\11111111111111111111111111111110 => -2\\11111111111111111111111111111101 => -3\\…\\10000000000000000000000000000010 => -2,147,483,646\\ 10000000000000000000000000000001 => -2,147,483,647\\10000000000000000000000000000000 => -2,147,483,648 $
Unsigned Types
An integer with no sign bit is said to be unsigned, that is, it has no negative value. We can declare unsigned types like below:
unsigned short
16 bits, The range of unsigned short
is $ 0 $ to $ 65,535 $
unsigned int
Having at least 16 bits, but it always has 32 bits on general PC (32/64 bit systems). The range of unsigned int
is $ 0 $ to $ 4,294,967,295 $
unsigned long
A short cut of long int
. This type has at least 32 bits. So it width is the same as int
on PC.
unsigned long long
Target type will have width of at least 64 bits. the range is $ 0 $ to $ 18,446,744,073,709,551,615 $
1
2
3
4
5
6
7
8
#include<iostream>
using namespace std;
int main() {
unsigned int a;
unsigned long long b;
//statements...
return 0;
}
Constant
Use put const before any type to declare a constant. Declaring a constant to store a value that used frequently can make a program safer and more efficient. The value of a constant can’t be change after it’s been declared. If doing so, error message comes up.
1
2
3
4
5
6
7
8
#include<iostream>
using namespace std;
int main() {
const unsigned int a = 10;
const double b = 100.5;
a = 11; //try to change the value of a constant
return 0;
}
When compiling, we get this error
1
2
3
test.cpp: In function 'int main()':
test.cpp:6:5: error: assignment of read-only variable 'a'
a = 11; //try to change the value of a constant