Skip to main content

C++ Variables and Data Types

3 min read
Share:
On this page (9sections)

Variables

A variable is reserved memory location for storing some values.That means to tell the compiler about storage of the variable with help of data-type.

the general syntax is,

data-type variable_name = 10;

for example,

int myvariable = 10;

the variable is also referred as an Identifier.

C++ Identifiers

A C++ identifier is a name used for identifying any user defined things.That names refer a variable, function, class, template, or any other custom-defined.Identifiers are sequences of characters

Rules:

  • Identifier can be include letters uppercase(A-Z) and lowercase(a-z)
  • Identifier can be decimal digits(0-9).
  • The first character of an identifier cannot be a digit.
  • You Can Include underscore character ?_? in identifiers.The first character of an identifier cannot be an underscore(Some Compilers Accept underscore as a first Character).
  • Lowercase letters and uppercase letters are distinct, such that foo and FOO are two different identifiers.
  • When using GNU extensions, you can also include the dollar sign character ?$? in identifiers.

Fundamental data types

Data TypeDescriptionSizeLimit
charCharacter or small integer.1byte signed: -128 to 127unsigned: 0 to 255
short(intshort)Short Integer2bytes

signed: -32768 to 32767

unsigned: 0 to 65535

| | int | Integer | 4bytes |

signed: -2147483648 to 2147483647

unsigned: 0 to 4294967295

| | long int (long) | Long integer. | 4bytes |

signed: -2147483648 to 2147483647

unsigned: 0 to 4294967295

| | bool | Boolean value | It can take one of two values: true or false |

1byte

true or false

| | float | Floating point number. | 4bytes |

+/- 3.4e +/- 38 (~7 digits)

| | double | precision floating point number. | 8bytes |

+/- 1.7e +/- 308 (~15 digits)

| | long double | double precision floating point number. | 8bytes |

+/- 1.7e +/- 308 (~15 digits)

| | wchar_t | Wide character. | 2 or 4 bytes |

1 wide character

|

Declaration Of Variables

In C++, all the variables must be declared before to use or initial statements of the block or main or function or global. variables should specify with data type.
It binds a data type and an identifier with the variable. The data type allows the compiler to execute statements correctly.

int a;
float number;

simple example program declares the variable, assign value and printing variable.

// variable declaration in c++

#include <iostream>
using namespace std;

int main ()
{
  // declaring variables, Its name is b
  int a;

  // assining values in a:
  a = 5;

  // just printing a value
  cout << a;

  // terminate the program:
  return 0;
}
5

Operating Of Variables Example Program

// variable declaration and Operating variables in c++

#include <iostream>
using namespace std;

int main ()
{
  // declaring variables , Its name are a,b and c
  int a,b,c;

  // assining values in a:
  a = 5;
  b = 10;
  
  //Operating variables
  c = a + b;
  
  // just printing a result c
  cout << c;

  // terminate the program:
  return 0;
}
15

Introduction to strings

  • The C++ language supports strings.
  • strings data type (string class type) support through the standard string class and Its supports all the basics operations.
  • strings is not a fundamental type.
  • It behaves in a mostly similar way as basic data types.
  • C++ supports also The C-style character string(Array of Char).

Simple Example Program for C++ string

// string variable declaration in c++
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  // declaring variable str for string type
  string str = "Hello C++ String";
  
  // just printing string 
  cout << str;
  
  // terminate the program:
  return 0;
}
Hello C++ String

Continue learning with these related tutorials and programs:

Related Tutorials

Search tutorials