Variable in C++


Variable in C++

A variable lets us store names that our programs can manipulate. Each C++ variable has a particular type that defines the variable's memory size and layout, the range of values that can be saved in that memory, and the set of operations used for the variable.

The variable name can be letters, numbers, and the underscore character. It should start with either a letter or an emphasis. The letters in the upper and lower case are different because C++ is case sensitive −

As mentioned in the last chapter, there are the following primary forms of vary in C++ -

  1. bool
    It stores true / false values
  2. char
    Has one byte of data value
  3. int
    The machine’s natural integer size
  4. Float
    Floating point value of single precision
  5. double
    Floating point value of double precision
  6. void
    Representation of the absence of type
  7. wchar_t
    A wide type of character

C++ also enables the definition of several variable kinds, including listing, display, array, reference, data structure, and classes, which can be covered in the following chapters.

The following section will discuss how various variables can be defined, declared, and used.

Variable Definition in C++

A definition variable instructs the compiler where and how much storage the variable needs to create. A variable definition identifies a type of data and includes the following list of one or more variables of that type –

type variable_list;

Variable Declaration in C++

A variable statement assures the compiler that a variable exists with the supplied type and name. The compiler continues compiling without any details of the variable being needed. A variable statement has its significance at the time of compilation alone; the compiler requires the real variable definition when the program is linked.

When you use many files, a variable declaration is helpful, and you specify your variable in one of the files available when the program is connected. To declare a variable in any place using an external keyword. Although a variable can be declared numerous times in your C++, it can only be defined once in a single file, function, or code block.

Variable Declaring

// Declaring a single variable type variable_name; // Declaring multiple variables: type variable_name1, variable_name2, variable_name3;

*Note :- variable also declare without assigning the value, or you can assign the value later:

Example of Variable Declaring

// Declaring a single variable type variable_name; int x; int y; int z; // Declaring multiple variables: type variable_name1, variable_name2, variable_name3; int x,y,z ; // Declaring and assign the value single variable type variable_name=value; int x=10; // Declaring and assign the value multiple variables type variable_name1=value, variable_name2=value, variable_name3=value; int x=6,y=5,z=10;