Site icon Learn 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. The IDE we are going to be working in is Dev cpp.

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,

[crayon-66068d15a8834323036225/]

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,

[crayon-66068d15a883a466648759/]

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.

[crayon-66068d15a883c729197508/]

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,

[crayon-66068d15a883e311305623/]

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.

Exit mobile version