A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
ConstantsThere are two main ways of defining constants in C++, and we'll review and compare both of them, seing their advantages and disadvantages. |
On Thursday, July 28th 2005 at 09:51 AM By Andrew Pociu (View Profile) ![]() ![]() ![]() ![]() (Rated 3.8 with 5 votes) |
|||
|
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:
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:
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 :
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. |
||||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
||||
|
||||
Current Commentsgood
if i declare 2005 as
#define myconst 2005
how much memory myconst gonna take up.
and how much memory if i define it as
#define 2005L
Related Tutorials
Related Source Code
C++ Job Search