variables in cpp C++ - ProgrammerTech
Language:

Search

variables in cpp C++

  • Share this:
variables in cpp C++

Variables in C++

Variables in c plus plus are something that you associate with something else, for example you find the value of a = 60, and the variable occupies a part of the random memory until it is finished. That is, we set the value of a to be used at a later time. A stays in random memory, waits for it to be requested, and then goes after the program is finished.

Variables are places that are held in memory. The purpose of variables is to store data in them while the program is running. There are many types of variables, and the type we give to the variable is what makes the system (the operating system). Specifies the type of data that can be stored in the memory space reserved for the variable. For example, if we want to store a number in a variable, the variable type must be originally intended to store Integer numbers in order to allow this.

 

Data types in C++

There are many types of data types in this language, but the most famous are five types that we will explain in detail, and these types are:

  1. char literal data.
  2. Integer valid data.
  3. float data.
  4. Double long decimal numeric data.
  5. Boolean statements true or false.

 

Char literal variables in C++

This type of data includes (foreign) characters in all its forms (one letter, one number, or space) and occupies 1 byte of memory. In other words, this type is used to store a (foreign) character or an integer whose value is a character relative to its ASCII Code number.

The data is stored in the form

'A' , 'B' ,  ' 5'

Each of them is a char data type and has 1 byte in memory.

char a,b
a = 'a';
b = ' ';
char var1;
var1 = '.';

The char data type can store any (foreign) character because the foreign character needs 1 byte of memory to be stored, while the Arabic character needs 2 bytes to be properly stored in the memory.

 

Integer integer variables

It includes integer values, i.e. a number that does not contain a decimal point and occupies a space of 4 bytes.

int a = 100;

Are the same

int a;
a = 100;

 

Float numerical variables

Real or fractional numbers, this type of data can store a number with a decimal point, and the number can contain seven digits after the comma, and it occupies a space of 4 bytes. The data is in the form.

0.5 , 4.60 , 5.14

All preceding numbers are float because they have a decimal point and the number is short.

float x;
x = 5.2;

float variable is x and the value of this variable is 5.2.

 

Long decimal numerical variables double

Double variables are real or fractional numbers. This type of data can store a number with a decimal point, and it can be represented as 15 digits after the comma, and it occupies an area of ​​8 bytes. It is more accurate than float in exact arithmetic operations.

double x = 14.5;

 

Variables called true or false bool

It is used to store either true or false and occupies 1 byte of space.

bool x = flse;

 

Converting between variables in C++

One of the most important things in programming is the conversion between variables, especially when reading from the user. When reading from the user, the value is always text, so sometimes we need the value to be a number.

  • Divide an integer by an integer and the result is true.
  • Divide a real number by an integer and the result is integer.
  • Dividing a real number by an integer gives the result a real.
  • There is no integer division by a real number.

 

Example 1

#include <iostream>
#include <string>
using namespace std;
int main()
{
    int i = 10;
    string a = to_string(i);
    cout << a;
    return 0;
}

We called the library string, which is responsible for dealing with the change between text and numbers, and we created a scalar variable i and its value is 10. Then we created a text variable a and its value is to convert the scalar variable i to text via the to_string() function. And we typed a means the number into text.

Example 2

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s = "10";
    int i = stoi(s);
    cout << i << '\n';
    return 0;
}

We called the library string, which is responsible for dealing with the change between text and numbers, and we created a text variable s and its value is 10. Then we created a scalar variable i and its value is to convert the text variable s to an integer through the stoil() function. And we print i means the text is converted to an integer.


Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com