#Concept #Study #CPP
variables are
## Declaring variables
- always consist of type + name
- cannot start with a number, but with an underscore or letter
- cannot have any spaces
- the name can contain letters,underscores or numbers
- Some names are reserved keywords and cannot be used.
- return
- int
- cout
- [...]
- We cannot declare a name in the same scope
- variables are case sensitive
- you should always be consistent in your naming convention
- never use variables before initializing them
- Declrea variables close to when you need them
```
int age;
double rate;
```
## Initializing variables
describes the process of assigning a value to a variable
```c++
int age; // uninitialized
int age = 21; // C-like initialization
int age (21); // Constructor initialization
int age {21}; // C++11 list initialization syntax
```
## Constants
Similar to variables in C++.
Unlike variables the value of constants cannot be change, if you try to change it you will receive a compiler error.
### literal constants
used to express specific values
```c++
x = 12;
y = 12U; // unsigned constant
```
### declared constant
Most common way to use constants, cannot be changed after defined and always need to be declared.
```c++
const double pi {3.1415926};
```
### defined constant
no longer used in modern c++, works like a find replace, e.g. if you define
```c++
#define pi 3.1415926
```
The compiler will run through the code and replace `pi` everywhere it finds it with the value `3.1415926`
## Global variables
sometimes variables are defined outside of any function, they can be used outside of the current function and automatically initialize to 0.
Global variables can be accessed and changed by any part of the program, can make debugging more complicated.
```c++
int age {17}; //global variable
int main() {
int age {18}; //local variable
}
```
The compiler is always looking locally first before using global variables.
## 🔗Resources