Constants

Constants
There are two main ways of defining constants in C++, and we'll review and compare both of them, seing their advantages and disadvantages.

What are constants? Let’s see what Microsoft Encarta has to say:

constant – mathematics, quantity with fixed value: a quantity that retains a fixed value in any circumstances or throughout a particular set of calculations. Pi, the ratio of the circumference to the radius of any circle, is a constant.

Microsoft® Encarta® Reference Library 2003. © 1993-2002 Microsoft Corporation. All rights reserved.

That ain’t very helpful, however you can say that mathematical constants are similar to programming constants. In programming, constants are variables that allow their value to be set once, in the definition, and never changed after that. So it’s like a normal variable, only that you can assign a value to it when it is defined, however you can’t change that value later in the program.

In C++, mainly there are two ways of defining constants, one is the old way borrowed from old C, and the rather new, standard ANSI/ISO compliant way.

Let’s see the old, C way first:

#define MyConst 2005

The #define directive is a pre-processor directive, just like anything else prefixed with “#” Using the line #define MyConst 2005 we tell the pre-processor to replace each occurange of MyConst in the file, with the number 2005.

That sounds great, but why is the second method ANSI/ISO compliant, and should it be used instead of #define? First, let’s have a look at the second way of defining a constant:

const int MyConst = 2005;

First we can notice here, is that we defined a type for the constant, int. So one of the reasons why using this medthod is better than using #define is that you can define a clear data type such as int, double or string. It’s true that using #define we can define a data type to a certain extent. For example we can use the suffix “L” and MyConst will now use the long data type :

#define MyConst 2005L

Also, another advantage of using the second method is that you can define the scope of the constant, you can set it either private or global, while using the first method (#define), the constant can only be global.

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top