C++ Variables and Datatypes

In this tutorial we will understand the concept of C++ Variables and Datatypes.

Variables
  • A variable provides us with a named storage that our programs can manipulate.
  • Each variable in C++ has a specific type, which determines the size and layout of the variables memory; the range of values that can be stored within that memory and the set of operations that can be applied on that variable.
  • Each variable needs an identifier(name of the variable that we assign) that distinguishes itself from others.
  • The name of a variable can be composed of letter, digits, and underscore(_) characters. It must begin with either a word or underscore character.
  • Upper and lower case letters are distinct because C++ is case sensitive.
  • E.g. int x = 5; char letter = ‘a’;
Identifiers (name that we assign to a variable or any entity in general)
  • A valid identifier, is a sequence of one or more letters, digits or underscore(_) characters. Neither spaces nor puncutation marks or symbols can be a part of identifiers.
  • Only letters, digits and underscores are allowed.
  • Another rule is that while creating these identifiers, use of C++ inbuilt keywords is not allowed. Some keywords (for e.g. for, while, int) are reserved keywords which cannot be used as identifiers.
  • The standard reserved keywords are as follows –
    • and, double, not_eq, throw, and_eq, dynamic_cast, operator, true, asm, else, or, try, auto, enum, or_eq, typedef, bitand, explicit, private, typeid, bitor, extern, protected, typename, bool, false, public, union, break, oat, register, unsigned, case, fro, reinterpret-cast, using, catch, friend, return, virtual, char, goto, short, void, class, if, signed, volatile, compl, inline, sizeof, wchar_t, const, int, static, while, const-cast, long, static_cast, xor, continue, mutable, struct, xor_eq, default, namespace, switch, delete, new, template, do, not, this
Data Types in C++
  • All variables use data-type during declaration to restrict the type of data to be stored.
  • Therefore, we can say that data types are used to tell the variables the type of data it can store.
  • Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data-type with which it is declared.
  • Every data type requires different amount of memory.

In C++ Programming Data types can be categorized broadly in 3 types –

  1. Primitive/Buit-in Data Types.
  2. Derived Datatypes.
  3. User-Defined Data types.
1. Primitive / Built-in Data Types

These contain the basic inbuilt data types provided by C++ itself and are futher classified as follows-

  1. integral type
    • int
    • char
  2. floating type
    • float
    • double
  3. void
2. Derived Data Types

These contain datatypes which are built using the basic/primitive datatypes and are provided by C++ itself however they are not considered as the basic types. For example –

  1. Arrays
  2. Pointers
  3. References etc
3. User – Defined Data Types

These datatypes are custom datatypes that we create according to our requirements. Custom datatypes can be combinations of multiple basic datatypes. For example –

  1. Structures
  2. Unions
  3. Classes
  4. Enumerations etc

 

C++ data types

C++ Data Types Diagram

Data Type, Size and Range of Primitive Datatypes –

*the value of size and range depends on the system the program is compiled on(they depend on system architecture)

TypeTypical Bit WidthTypical Range
char1byte-127 to 127 or 0 to 255
unsigned char1byte0 to 255
signed char1byte-127 to 127
int4bytes-2147483648 to 2147483647
unsigned int4bytes0 to 4294967295
signed int4bytes-2147483648 to 2147483647
short int2bytes-32768 to 32767
unsigned short intRange0 to 65,535
signed short intRange-32768 to 32767
long int4bytes-2,147,483,648 to 2,147,483,647
signed long int4bytessame as long int
unsigned long int4bytes0 to 4,294,967,295
float4bytes+/- 3.4e +/- 38 (~7 digits)
double8bytes+/- 1.7e +/- 308 (~15 digits)
long double8bytes+/- 1.7e +/- 308 (~15 digits)
wchar_t2 or 4 bytes1 wide character

Run Online

YouTube Video Tutorial Link on Variables & Data Types in C++

Leave a Reply

Your email address will not be published. Required fields are marked *