Variables are containers for storing data values in different types. These values are mostly stored in the memory location (RAM) of device (PC, tablet, mobile, IoT…). A variable can be defined in it’s type and with it’s name with a given value, this variable name can be used to read it’s value in memory location or to write a new value in to this memory location. Type of the variable can not be changed in runtime but it’s value can be copied to another variable in another type.
In general we can summarize setting a variable in this format,
1 2 3 |
variabletype variable = value; |
Here are most used variable types used in C++;
bool is used to define Boolean type variables, it has true or false value. Can be used like on/off switches.
1 2 3 4 5 6 7 8 9 |
bool position_of_switch = true; bool x,y,z; x = true; y = false; if(x) cout << "x is true"; |
int is used to define integer variables, here are few samples,
1 2 3 4 5 6 7 8 9 10 11 |
int user_age; user_age = 23; int x,y,z; x = 100; y = 200; z = 0; int a = 5, b = 12, c = 13; |
float is used to define single-precision floating point variables.
1 2 3 4 5 6 7 8 9 10 11 |
float user_weight; user_weight = 0.8; float x,y,z; x = 100.5; y = 200.22; z = -0.5; float a = 5.08, b = 12.01, c = 13.5; |
double is used to define double-precision floating point variables.
1 2 3 4 5 6 7 8 9 10 11 |
double user_weight; user_weight = 0.8; double x,y,z; x = 100.5; y = 200.22; z = -0.5; double a = 5.08, b = 12.01, c = 13.5; |
char is used to define one byte character that means it can have 0 to 255
1 2 3 4 5 6 7 8 9 |
char c = 255; char a; a = 'A'; //character 'A' is 65th character in ASCII format, so it's value is 65 char a, b, c; |
char[] is used to define more than one byte characters (these are also called as array of chars, they are ASCII format strings)
1 2 3 4 5 6 |
char code[]="ABCDEFG"; char name[32]; strcpy( name, "Ben Johnson"); // copy text to char array |
wchar_t is a wide character type of char and it is used to define two byte characters that supports Unicode characters
1 2 3 4 5 6 |
wchar_t code[] = "ABCDEFG"; wchar_t name[32]; wstrcpy( name, "Ben Johnson"); // copy text to char array |
string or String is used to stores texts in modern way, such as “abcd1234”. String values are surrounded by double quotes
1 2 3 |
string name = "Ben Johnson"; |
UnicodeString is used to define Unicode Strings, this is the most modern way to define string variables
1 2 3 4 5 |
UnicodeString ustr; ustr = L"This is Test"; |
auto is used to define variables automatically by the initializer
1 2 3 4 5 |
auto x = 5; // integer auto f = 3.14 // float |
void Represents the absence of type. Generally used to define functions which has no returning value.
1 2 3 |
void *v; |
Global and Local Variables
The placement of variable definitions as given above is very important. Generally they should be declared before all, at least before the usage. You can declare all given types above as a global variable or local variable. Global Variables are defined in the main code lines out of any functions, generally after headers or before the main() function, and they can be used in any function inside. Local Variables are only used inside the function that they were declared.
For example if you define int gx = 15; before the main() or outside of any function, it is a global variable, if you define int lx = 20; inside a function i.e. in main() function it is a local variable. These can be float, bool, double, string or any type defined by user. See this example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> int gx=15; // this is global variable int main(int argc, char** argv) { int lx = 25; // this is local variable works only in this procedure std::cout << "global variable gx=" << gx << '\n'; std::cout << "local variable lx=" << lx << '\n'; return 0; } |