Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Variable Types

C++ Variable Types

A variable is, in essence, just a name for a storage area that the program can manipulate.

In C++, there are many variable types for storing different kinds of data.

Every variable in C++ has a specified type; the type determines the size and layout of the variable's storage, the range of values that can be stored in memory, and the operations that can be applied to the variable.

A variable name can consist of letters, digits, and underscore characters. It must begin with a letter or an underscore.

Uppercase and lowercase letters are different because C++ is case-sensitive.

Based on the basic types explained in the previous chapter, the following basic variable types will be explained in the next chapter:

Type Description
bool bool type, stores the value true or false, occupying 1 byte.
char char type, used to store ASCII characters, usually occupying 1 byte.
int int type, usually used to store ordinary integers, usually occupying 4 bytes.
float

float type, used to store single-precision floating-point numbers. Single precision has the format: 1 sign bit, 8 exponent bits, 23 fraction bits, usually occupying 4 bytes.

double

double type, used to store double-precision floating-point numbers. Double precision has 1 sign bit, 11 exponent bits, 52 fraction bits, usually occupying 8 bytes.

void Represents the absence of a type.
wchar_t wchar_t type, used to store a wider range of characters, usually occupying 2 or 4 bytes.

C++ also allows defining various other types of variables, such asenum, pointer, array, reference, data structure, classand so on, which will be explained in later chapters.

  1. Integer Types:

  2. int: used to represent integers, usually occupying 4 bytes.
  3. short: used to represent short integers, usually occupying 2 bytes.
  4. long: used to represent long integers, usually occupying 4 bytes.
  5. long long: used to represent longer integers, usually occupying 8 bytes.
  6. - `int`: used to represent integers, usually occupying 4 bytes. - `short`: used to represent short integers, usually occupying 2 bytes. - `long`: used to represent long integers, usually occupying 4 bytes. - `long long`: used to represent longer integers, usually occupying 8 bytes.
    1. Floating-Point Types:

    2. float: used to represent single-precision floating-point numbers, usually occupying 4 bytes.
    3. double: used to represent double-precision floating-point numbers, usually occupying 8 bytes.
    4. long double: used to represent higher-precision floating-point numbers; the number of bytes occupied may vary by implementation.
    5. - `float`: used to represent single-precision floating-point numbers, usually occupying 4 bytes. - `double`: used to represent double-precision floating-point numbers, usually occupying 8 bytes. - `long double`: used to represent higher-precision floating-point numbers; the number of bytes occupied may vary by implementation.
      1. Character Types:

      2. char: used to represent characters, usually occupying 1 byte.
      3. wchar_t: used to represent wide characters, usually occupying 2 or 4 bytes.
      4. char16_t: used to represent 16-bit Unicode characters, occupying 2 bytes.
      5. char32_t: used to represent 32-bit Unicode characters, occupying 4 bytes.
      6. - `char`: used to represent characters, usually occupying 1 byte. - `wchar_t`: used to represent wide characters, usually occupying 2 or 4 bytes. - `char16_t`: used to represent 16-bit Unicode characters, occupying 2 bytes. - `char32_t`: used to represent 32-bit Unicode characters, occupying 4 bytes.
        1. Boolean Type:

        2. bool: used to represent boolean values, can only taketrueorfalse.
        3. - `bool`: used to represent boolean values, can only take `true` or `false`.
          1. Enumeration Types:

          2. enum: used to define a set of named integer constants.
          3. - `enum`: used to define a set of named integer constants.
            1. Pointer Types:

            2. type*: used to represent a pointer to an object of typetype.
            3. - `type*`: used to represent a pointer to an object of type `type`.
              1. Array Types:

              2. type[]ortype[size]: used to represent an array composed of elements of the same type.
              3. - `type[]` or `type[size]`: used to represent an array composed of elements of the same type.
                1. Structure Types:

                2. struct: used to define a structure containing members of different types.
                3. - `struct`: used to define a structure containing members of different types.
                  1. Class Types:

                  2. class: used to define a custom type with attributes and methods.
                  3. - `class`: used to define a custom type with attributes and methods.
                    1. Union Types:

                    2. union: used to define a special data type that can store different data types at the same memory location.
                    3. - `union`: used to define a special data type that can store different data types at the same memory location.

                      In C++, the size of a type (i.e., the number of bytes occupied) depends on the compiler and the computer architecture. However, the C++ standard specifies the minimum ranges for different integer types rather than specific byte counts, so that code runs correctly on different systems.

                      Note that the ranges above are only the minimum requirements of the C++ standard; in practice, these types may occupy more bytes on many systems—for example, on many modern computers int usually occupies 4 bytes while long may occupy 8 bytes.

                      Below we will explain how to define, declare, and use variables of various types.

                      Variable Definition in C++

                      A variable definition tells the compiler where and how to create the storage for the variable.

                      A variable definition specifies a data type and includes a list of one or more variables of that type, as shown below:

                      
                      type variable_list;
                      

                      Here,type must be a valid C++ data type, which can be char, wchar_t, int, float, double, bool, or any user-defined object,variable_list and can consist of one or more identifier names separated by commas. Several valid declarations are listed below:

                      int

                      i

                      ,

                      j

                      ,

                      k

                      ;

                      char

                      c

                      ,

                      ch

                      ;

                      float

                      f

                      ,

                      salary

                      ;

                      double

                      d

                      ;

                      line int i, j, k; declares and defines the variables i, j, and k, instructing the compiler to create variables named i, j, k of type int.

                      A variable may be initialized (assigned an initial value) at the time of declaration. An initializer consists of an equal sign followed by a constant expression, as shown below:

                      type

                      variable_name

                      =

                      value

                      ;

                      A few examples are listed below:

                      extern

                      int

                      d

                      =

                      3

                      ,

                      f

                      =

                      5

                      ;

                      // declaration of d and f

                      int

                      d

                      =

                      3

                      ,

                      f

                      =

                      5

                      ;

                      // define and initialize d and f

                      byte

                      z

                      =

                      22

                      ;

                      // define and initialize z

                      char

                      x

                      = '

                      x

                      ';

                      // the value of variable x is 'x'

                      Definitions without initialization: variables with static storage duration are implicitly initialized to NULL (all bytes are 0); the initial values of all other variables are undefined.

                      Variable Declaration in C++

                      A variable declaration assures the compiler that a variable exists with the given type and name, so the compiler can continue further compilation without knowing the variable's full details. A variable declaration is meaningful only at compile time; at program linking the compiler needs the actual variable definition.

                      Variable declarations are very useful when you use multiple files and define a variable in only one of them (the file that defines the variable is available at program linking). You can use extern the keyword to declare a variable anywhere. Although you can declare a variable multiple times in a C++ program, the variable can be defined only once in a file, function, or block.

                      Example

                      Try the example below, where the variables are declared at the top but defined and initialized inside the main function:

                      Example

                      #include

                      <
                      iostream
                      using
                      namespace
                      std

                      ;

                      // variable declaration

                      extern

                      int

                      a

                      ,

                      b

                      ;

                      extern

                      int

                      c

                      ;

                      extern

                      float

                      f

                      ;

                      int

                      main

                      (

                      )

                      {

                      // variable definition

                      int

                      a

                      ,

                      b

                      ;

                      int

                      c

                      ;

                      float

                      f

                      ;

                      // actual initialization

                      a

                      =

                      10

                      ;

                      b

                      =

                      20

                      ;

                      c

                      =

                      a

                      b

                      ;

                      cout

                      <<

                      c

                      <<

                      endl

                      ;

                      f

                      =

                      70

                      .0

                      /

                      3

                      .0

                      ;

                      cout

                      <<

                      f

                      <<

                      endl

                      ;

                      return

                      0

                      ;

                      }

                      When the code above is compiled and executed, it produces the following result:

                      
                      30
                      23.3333
                      

                      Likewise, when declaring a function, you provide a function name, while the actual definition of the function can be placed anywhere. For example:

                      // Function declaration

                      int

                      func

                      (

                      )

                      ;

                      int

                      main

                      (

                      )

                      {

                      // function call

                      int

                      i

                      =

                      func

                      (

                      )

                      ;

                      }

                      // Function definition

                      int

                      func

                      (

                      )

                      {

                      return

                      0

                      ;

                      }

                      Lvalues and Rvalues in C++

                      There are two kinds of expressions in C++:

                      Variables are lvalues, so they can appear on the left side of an assignment. Numeric literals are rvalues, so they cannot be assigned to and cannot appear on the left side of an assignment. The following is a valid statement:

                      
                      int g = 20;
                      

                      But the following is not a valid statement and will produce a compile-time error:

                      
                      10 = 20;