Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA 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.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis 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.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating 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.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

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.

On Thursday, July 28th 2005 at 09:51 AM
By Andrew Pociu (View Profile)
****-   (Rated 3.8 with 5 votes)
Contextual Ads
More C++ Resources
Advertisement
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.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by prakash chauhan on Thursday, January 26th 2006 at 11:36 AM

good

by prakash chauhan on Thursday, January 26th 2006 at 11:38 AM

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


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C++ Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia

Other Resources