Data Types & Variables in C++
Built-in Data Types
There are four built-in data types in C++:-
- Integer data type
- Floating point data type
- Void data type
- Char data type
Integer Data Type
An integer is an integral whole number without a decimal point. These numbers are used for counting. For example 26, 272, -342 are valid integers. Normally an integer can hold numbers from -32768 to 32767.
Floating Data Type
A floating point number has a decimal point. Even if it has an integral value, it must include a decimal point at the end. Valid floating point examples are 45.0, 34.23, -234.34.
Void Data Type
- It is used for following purposes :
- It specifies the return type of a function when the function is not returning any value.
- It indicates an empty parameter list on a function when no arguments are passed.
- A void pointer can be assigned a pointer value of any basic data type.
Char Data Type
Derived Data Type
C++ also permits four types of derived data types. As the name suggests, derived data types are basically derived from built-in data types.
There are four derived derived data types.
- Array
- Function
- Pointers
- Reference
We will study these data types in further chapters.
User Define Data Type
C++ has four types of user-defined data types.
As the name suggests, user-defined data types are defined by the programmers(you) during the development of software.
These four user-defined data types are :
- Structure
- Union
- Class
- Enumerator
We will study these data types in further chapters.
Type Conversion
In C++ object-oriented language, a smaller memory data type variables can be converted to a larger data type by the compiler.
Type conversion can be done by two ways:
1. Automatic Type Conversion
When an expression consists of more than one type of data element, the C++ compiler converts the smaller data type elements into larger data type elements. This process is known as implicit or automatic conversion.
2. Typecasting
Below statement allows the programmer to convert one data type into another data type.
Syntax:
aCharVar = static_cast<char>(an IntVar)
Here in the above syntax char variable will be converted into int variable after execution.
The C++ language has four typecast operators:
- static_cast
- reinterpret_cast
- const_cast
- dynamic_cast
Literals or Components
- An identifier that does not change its value during the execution of a program is known as a constant or a literal.
- Any attempt to change the value of a constant will result in an error message.
- A keyword const is added to the declaration of an identifier to make that identifier constant.
Syntax :
const datatype variable = value;
Example :
const float PI = 3.1415;
const char ch = 'A';
const int rate = 40;
Keywords
Below is the list of reserved keywords in C++. Since they are used by the language, these keywords are not available for re-definition or overloading.
| Keywords | Description |
|---|---|
| and | Alternative to && operator |
| and_eq | Alternative to &= operator |
| asm | Insert an assembly instruction |
| auto | Declare a local variable, or we can let the compiler to deduce the type of the variable from the initialization |
| bitand | Alternative to bitwise & operator |
| bitor | Alternative to | operator |
| bool | Declare a boolean variable |
| break | Break out of a loop |
| case | A block of code in a switch statement |
| catch | Handles exceptions from throw |
| char | Declare a character variabl |
| class | Declare a class |
| compl | Alternative to ~ operator |
| const | Declare immutable data or functions that do not change data |
| const_cast | Cast from const variables |
| continue | Bypass iterations of a loop |
| default | Default handler in a case statement |
| #define | All header files should have #define guards to prevent multiple inclusion. |
| delete | Make dynamic memory available |
| do | Looping construct |
| double | Declare a double-precision floating-point variable |
| dynamic_cast | Perform runtime casts |
| else | Alternate case for an if statement |
| enum | Create enumeration types |
| exit() | Ending a process |
| explicit | Only use constructors when they exactly match |
| export | Allows template definitions to be separated from their declarations |
| extern | Declares a variable or function and specifies that it has external linkage |
| extern | "C" enables C function call from C++ by forcing C-linkage |
| false | A constant representing the boolean false value |
| float | Declare a floating-point variable |
| for | Looping construct |
| friend | Grant non-member function access to private data |
| goto | Jump to a different part of the program |
| if | Execute code based on the result of a test |
| inline | Optimize calls to short functions |
| int | Declare an integer variable |
| long | Declare a long integer variable |
| mutable | Override a const variable |
| namespace | Partition the global namespace by defining a scope |
| new | Allocate dynamic memory for a new variable |
| not | Alternative to ! operator |
| not_eq | Alternative to != operator |
| operator | Create overloaded operator function |
| or | Alternative to || operator |
| or_eq | Alternative to |= operator |
| private | Declare private members of a class |
| protected | Declare protected members of a class |
| public | Declare public members of a class |
| register | Request that a variable be optimized for speed |
| reinterpret_cast | Change the type of a variable |
| short | Declare a short integer variable |
| signed | Modify variable type declarations |
| sizeof | Return the size of a variable or type |
| static | Create permanent storage for a variable |
| static_cast | Perform a non-polymorphic cast |
| struct | Define a new structure |
| switch | Execute code based on different possible values for a variable |
| template | Create generic functions |
| this | A pointer to the current object |
| throw | Throws an exception |
| true | A constant representing the boolean true value |
| try | Execute code that can throw an exception |
| typedef | Create a new type name from an existing type |
| typeid | Describes an object |
| typename | Declare a class or undefined type |
| union | A structure that assigns multiple variables to the same memory location |
| unsigned | Declare an unsigned integer variable |
| using | Import complete or partial namespaces into the current scope |
| virtual | Create a function that can be overridden by a derived class |
| void | Declare functions or data with no associated data type |
| volatile | Warn the compiler about variables that can be modified unexpectedly |
| void | Declare functions or data with no associated data type |
| wchar_t | Declare a wide-character variable |
| while | Looping construct: |
| xor | Alternative to ^ operator |
| xor_eq | Alternative to ^= operator |
Variables declaration in C++
Syntax :
datatype variable_name;
Example :
int a;
float f;
// bool is the new datatype, it can stores true or false value.
bool a;
char c;
There is a important difference between C and C++ regarding when local variable can be declared. In c, you must declare all local variables used within a block at the start of that block. You cannot declare a variable in a block after an "action" statement has occurred.
Example :
// Incorrect in C. Ok in C++
int function()
{
int num1;
num1 = 10;
/*
won't compile as a C program
but compiling as C++ program its OK
*/
int num2;
num2 = num1 * 5;
return num2;
}
