🔧 Variables, Constants, Data Types, and Namespaces in C++
Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to
Welcome to the second blog of the series "Unlocking C++"!
In this blog, I will discuss variables, constants, data types, and namespaces.
It's the basics required for any programming language.
Let's start with variables.
Variable
Variables are named memory locations used for storing the data.
For high level, it's just like we used to do with maths
2x + 4 = 0
=> x = -2
Variables are of many types.
For C++ variable types can be stated as
- Global Variable: Any function or class can access The variable declared globally.
- Local Variable: The local or block-scoped variable can only be accessed within the block. As the block ends, the variable is discarded.
#include <iostream>
int y = 3.14;
int main() {
{ // block
int x = 5;
std::cout << x << std::endl;
std::cout << y << std::endl;
}
std::cout << x << std::endl; // gives error 'x' was not declared in this scope
std::cout << y;
}
Here x is a block-scoped variable so inside the block we can access them but as the block ends it can't be accessed. And y is a global variable so it can be accessed anywhere in the program.
Constants
Constants are also named memory locations used for data storage but their values can't be changed once given.
For example, π.
We know it's a constant and its value will not be changed.
In C we used macros ( #define PI 3.14
) or manifest constants but constants in C++ are better as they come with type safety.
#include <iostream>
int main() {
const double pi = 3.14;
std::cout << pi;
pi = 4; // error: assignment of read-only variable
}
Here as its const, its value can't be changed.
Another important concept for variables and constants.
Declaration is the statement where the variable is declared without any value. Space is given to a variable as it may be given value in future.
int x;
Initialisation is the statement where we give a value to the variable.
x = 5;
In the case of variables declaration and initialisation can happen in the same line or different lines as well but for constant it must be done in the same line.
Data types
Each variable or constant is associated with a data type. It specifies the type of data stored in the variable.
The primary data types in C++ are:
- int: store integer values, 2 bytes size (maybe 4 bytes in some systems)
- long: store integer values but of higher size (4 bytes)
- float: store decimal numbers, less precision, 4 bytes size
- double: store decimal numbers, high precision, 8 bytes size
- char: store a single character, 1 byte size
- bool: either true(1) or false(0), 1 byte size
Here primary data types mean built-in (predefined) data types to be used by the user directly.
There are Derived and User-defined data types as well.
For now, I am stating them but I will make separate blogs for them in the future.
Derived data types
These are the data types derived from primitive data types.
- Function
- Array
- Pointer
- Reference
User-defined data types
These data types are created by users to use in the program.
- struct
- union
- class
- enum
- typedef
Namespaces
Namespace is a declarative region under which variables, functions, and classes exist. It's used to avoid conflict between the same identifiers.
Identifiers means names given to variables, constants, functions, and classes.
In C++ we have a standard namespace (std) but we can create our namespace as well.
#include <iostream>
namespace apple {
int x = 1;
}
namespace banana {
int x = 2;
}
int main() {
std::cout << apple::x << std::endl; // 1
std::cout << banana::x; // 2
return 0;
}
Here the same identifier x is used but in different namespaces.
So this is for this blog. Let's meet in the next blog.
...