What is a variable?
When a user enters a new value that would be involved in
the calculation, to manage that value, you can (temporarily) store it in the computer memory. NAME Data Type
Data
Since the values entered in a reserved memory area change
regularly, they are called variables.
Creating a variable (Declaring a variable)
Type of the value (Data Type)
Variable_Name;
Fundamental data types
When programming, we store the variables in our
computer's memory, but the computer has to know what kind of data we want to store in them, since it is not going to occupy the same amount of memory to store a simple number than to store a single letter or a large number, and they are not going to be interpreted the same way.
Name char short int (short)
Description Character or small integer. Short Integer.
Size* 1byte 2bytes
Range* signed: -128 to 127 unsigned: 0 to 255 signed: -32768 to 32767 unsigned: 0 to 65535 signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295
int
Integer.
4bytes
long int (long)
Long integer.
4bytes
signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295
true or false +/- 3.4e +/- 38 (~7 digits) +/- 1.7e +/- 308 (~15 digits) +/- 1.7e +/- 308 (~15 digits) 1 wide character
bool
Boolean value. It can take one of two values: true or false. Floating point number. Double precision floating point number. Long double precision floating point number. Wide character.
1byte
float double long double wchar_t
4bytes 8bytes 8bytes 2 or 4 bytes
Declaration of variables
In order to use a variable in C++, we must first declare
it specifying which data type we want it to be. The syntax to declare a new variable is to write the specifier of the desired data type (like int, bool, float...) followed by a valid variable identifier. For example: int a; float mynumber;
Example 01
// operating with variables #include <iostream> using namespace std; int main () { // declaring variables: int a, b; int result; unsigned short int NumberOfSisters; signed int MyAccountBalance; short Year; }
Scope of variables
Initializing the variable
When you have just declared a variable, it may not hold a
significant value. To know the value it has, you should put an initial value into that memory space. Putting an initial value is referred to as initializing the variable.
Initializing a created variable:
Type of the value(Data Type) Variable_Name; Variable_Name = Value to be assign ; type identifier (initial_value) ;
Example 01
// initialization of variables #include <iostream> using namespace std; int main () { int a=5; // initial value = 5 int b(2); // initial value = 2 int c; c=4; int result; // initial value undetermined a = a + 3; result = a - b; cout << result; return 0; }
Introduction to strings
Variables that can store non-numerical values that are
longer than one single character are known as strings.
// my first string #include <iostream> #include <string> using namespace std; int main () { string mystring = "This is a string"; cout << mystring; return 0; }
// my first string #include <iostream> #include <string> using namespace std; int main () { string mystring; mystring = "This is the initial string content"; cout << mystring << endl; mystring = "This is a different string content"; cout << mystring << endl; return 0; }
Literals in C++
A literal is the source code representation of a fixed value literals are
represented directly in your code without requiring computation. A literal is any number, text or other information that directly represents a value. Integer Literals decimal format hexadecimal format octal format
The floating point Literals Character Literals Unicode escape sequence Boolean Literals
Integer Literals
75 // decimal
0113 // octal 0x4b // hexadecimal 75 // int 75u // unsigned int 75l // long 75ul // unsigned long
Floating Point Numbers
3.14159 // 3.14159
6.02e23 // 6.02 x 10^23 1.6e-19 // 1.6 x 10^-19 3.0 // 3.0 3.14159L // long double 6.02e23f // float
Character and string literals
'z'
'p' "Hello world" "How do you do?"
Unicode escape sequence
\u0041' '\u0030' '\u0022' Capital letter A Digit 0 Double quote "
'\u003b'
'\u0020' '\u0009'
Punctuation ;
Space Horizontal Tab
Boolean Literals
bool chosen = true;
Defined constants (#define)
#define PI 3.14159 #define NEWLINE '\n'
Example
// defined constants: calculate circumference #include <iostream> using namespace std; #define PI 3.14159 #define NEWLINE '\n' int main () { double r=5.0; // radius double circle; circle = 2 * PI * r; cout << circle; cout << NEWLINE; return 0; }
Declared constants (const)
With the const prefix you can declare constants with a
specific type in the same way as you would do with a variable:
const int pathwidth = 100; const char tabulator = '\t';