C++Introduction to C++Language FeatureLearn C++

How To: Use Variables In C++ Programming

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,

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.

int is used to define integer variables, here are few samples,

float is used to define single-precision floating point variables.

double is used to define double-precision floating point variables.

char is used to define one byte character that means it can have 0 to 255

char[] is used to define more than one byte characters (these are also called as array of chars, they are ASCII format strings)

wchar_t is a wide character type of char and it is used to define two byte characters that supports Unicode characters

string or String is used to stores texts in modern way, such as “abcd1234”. String values are surrounded by double quotes

UnicodeString is used to define Unicode Strings, this is the most modern way to define string variables

auto is used to define variables automatically by the initializer

void Represents the absence of type. Generally used to define functions which has no returning value.

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,

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++C++11C++14C++17Introduction to C++Learn C++

This Is How To Get A Substring of a Wide String in C++

C++C++11C++14C++17Introduction to C++Learn C++

How To Access Individual Character Elements Of A C++ String

C++C++11C++14C++17Introduction to C++Learn C++

The Right Way To Access Character Elements Of A Wide String

C++Language FeatureLearn C++

You NEED to Learn To Use JSON (JavaScript Object Notation)

Worth reading...
Easily Learn Powerful C++ Programming Language Structure
en_USEnglish