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

Dev-C++: Learn About Constants And Literals In The C++ Programming Language

Constants

In C++ we learnt that a variable is a container (storage area in memory location) to hold data (integer numbers, floating numbers, strings etc.), Also we explain that 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 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.

We can also use constant variables. In C++, there are two ways to define constants. We can use #define preprocessors and we can use const keyword before the variable type.

Using The #define Preprocessor

The preprocessor #define is used to define constants at the outside of main() processor, generally header files has many constants. The #define preprocessor works like replace command in editors like notepad or word. You define some constants, something like “use this instead of where you see this”, and when your codes are being compiled each things defined are handled as these constants. Advantages of using #define preprocessor are they are constant and can not be changed, they are faster than variables, they hold less space in memory than a variable.

For example, we can define GRAVITY as below,

Note that when using #define preprocessor here we didn’t use “=” and we didnt use “;” at the end. In general usage all things defined with #define are written in CAPITALS, this is not a rule but easy to understand that variable is constant. When compilation, this code above returns to same as below, both has same functionality,

As you see #define preprocessor function replaces 9.81 where it see every GRAVITY term. This code below is completely same in output as above two , but this is much slower and holds much memory while our gravity is constant in above.

As you see when coding you should define which variable is constant or not, the way you define make sense on the speed and memory usage.

Using The const Keyword

We can use also use const prefix to declare constant variables with their specific types. The main difference between #define and const, const keywords can be use inside functions. See an example as below,

This can be applied to any types int, float, double, etc. Please compare this last example with the previous examples.

Literals

Character literals are enclosed in single quotes that are used to define some special codes inside strings which are closed with “…”. For example L (uppercase only) is used to define wide character strings or Unicode Strings (e.g., L’x’) and should be stored in wchar_t type of variable . Here are literals mostly used when printing texts.

In C and C++ Programming Language, Inside strings (texts) , backslash is used with certain characters and it is called Escape Sequence. When they are preceded by a backslash they will have special meaning. For example, when printing a text, ‘\n’ at the end of string represents new line and ‘\t’ represents tab char. Here are some of these escape sequence codes,

Escape Sequence Description
\n Newline
\r Carriage Return
\t Tab Character
\\ \ Character
\’ ‘ character
\” ” character
\? ? character
\a Alert or Bell
\b Backspace
\f Form feed
\v Vertical tab
\oooo Octal number of one to four digits
\xhh . . . Hexadecimal number in more digits

Don’t have Dev-C++ yet? Download the latest Dev-C++ over on GitHub.

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++

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)

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

You Need To Know How To Correctly Remove Characters From C++ Strings

en_USEnglish