Primitives
Variables Identifiers and Types
In C++, variables are used to store data values of different types. Data types specify the kind of values that variables can hold.
Variables
In C++, variables are named storage locations used to hold data values. They are used to store and manipulate data during program execution. Variables in C++ have a specific data type that determines the kind of values they can hold.
To declare a variable in C++, you need to specify its data type and give it a name. Here’s an example:
// Declaration of an integer variable named 'age'
int age;
In this example, we declare a variable named ‘age’ of type ‘int’, which can hold whole number values. After declaring a variable, you can assign a value to it:
// Assigning a value of 25 to the 'age' variable
age = 25;
You can also initialize a variable at the time of declaration:
// Declaration and initialization of an integer variable
// 'score' with a value of 90
int score = 90;
Variables can be modified by assigning new values to them:
// Updating the value of the 'score' variable to 95
score = 95;
Identifiers
A variable in C++ is given a unique name that is known as an identifier.
Rules for naming a variable
The general rules for naming a variable are:
- An identifier can only contain uppercase alphabets (A to Z), lowercase alphabets (a to z), numbers (0 to 9), and underscore ‘_’.
- The first letter of an identifier can be an alphabet or an underscore.
Best practice:
- Use descriptive and meaningful names for the variables to make the code self-explanatory.
- It is not good practice to start an identifier with an underscore.
- numbers and Numbers are two different identifiers.
- Best practice to initialise it once you have declared it, to avoid any old value that had been there.
int limit = 100;
auto x = 7; // x is int.
Primitives (Built-in data types):
primitive types, also known as fundamental types, are the basic building blocks for representing simple data values. These types are directly supported by the language and do not require any additional definitions or dependencies. C++ provides several primitive types:
1. Integer Types:
- int: Used to store whole numbers (e.g., 10, -5).
- short: Used to store smaller whole numbers.
- long: Used to store larger whole numbers.
- long long: Used to store very large whole numbers.
- unsigned int: Used to store positive whole numbers only.
2. Floating-Point Types:
- float: Used to store decimal numbers with single precision.
- double: Used to store decimal numbers with double precision.
Difference between float and double
The precision of a floating-point number is the number of digits that can be stored after a decimal point. A float can store seven digits after a decimal point precisely. Whereas, double can store 15 digits after a decimal point precisely. It is recommended to use double for floating-point values.
3. Character Types:
- char: Used to store single characters (e.g., ‘a’, ‘5’, ‘$’).
4. Boolean Type:
- bool: Used to store either true or false values.
- bool is supported as built in data type from c++ 98 and above.
- _Bool for C language from c99 by "stdbool.h" library
5. Void type:
- void: Represents the absence of a type or value.
Neither C/C++ has a string type, they have provisions for handling strings of characters.
- C using the array of char – char str[] = "Hello";
- C++ using the string class from the STL – std::string str = "Hello";
Pointers
pointer is a reference to an object of a given type, the pointer itself typically holds the address of the object it points to.
- the type of the pointer is used as the type when it’s dereferenced,
- and also determining the size of increments and decrements operations on the pointer.
int I = 10;
int * i = &I;
*i = 50; // Dereference the pointer then the I will be 50
// pointer examples
// pointer to constant int,
// so the int is a constant value,
// but the pointer is not,
// so it can point to any other value.
const int a = 10;
const int * ptr = &a;
*ptr = 5; // is wrong
ptr++; //is allowed
// const ptr to int,
// so the pointer is constant
// so you can't change the pointer after declaration.
int a = 10;
int const * ptr = &a;
//or
int *const ptrt = &a;
*ptr = 5; // is allowed
ptr++; // is wrong
// pointer to function
int function(int);
int (*pFunc)(int) = function;
// the function name is an address for the function.
// to call it
int i = (*pFunc)(5);
Reference type
A reference is very much like a pointer but with different semantics
- the major distinction between pointers and references is that references are immutable which means they can not change once it is defined.
- and references are accessed as aliases
- You can’t reference a reference () it will simply copy the value of the new reference to the old one.
- You can’t have a pointer to a reference.
- You can’t have an array of references.
int I = 10;
int &i = I; // has to be define the reference once it is declared;
i = 50; // then the I will be 50
Why:
That’s because references are not objects and don’t occupy the memory so doesn’t have the address. You can think of them as the aliases to the objects. Declaring an array of nothing has not much sense.
C/C++ is a type-safe
- C/C++ enforces type
- Variables have a type and can’t change.
- Expressions have a type (2+2)
- it is ok to promote (put an int into float).
- You will be warned if you demote (put a float into an int because you are going to lose the floating precision).
- Putting a string into an integer, and multiplying a string and float, is an error.
float f_num = 10.0;
std::cout << f_num << endl;
// it will print 10.0
f_num = 9 / 5;
std::cout << f_num << endl:
// it will print 1
f_num = 9.0 / 5;
std::cout << f_num << endl;
// it will print 1.8
- Save a user input
int i = 0;
cin >> i ; // has to be int.
C++ supports various data types, including integers, floating-point numbers, characters, booleans, strings, and user-defined types. Each data type has its specific syntax and characteristics.
Let's see how we can make a user-defined data type in C++ in the next topic