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

What is the difference between #define and const?

On Monday, May 21st 2007 at 01:13 AM
By Andrew Pociu (View Profile)
****-   (Rated 4 with 11 votes)
Advertisement
More C++ Resources
While they both serve a similar purpose, #define and const act differently. When using #define the identifier gets replaced by the specified value by the compiler, before the code is turned into binary. This means that the compiler makes the substitution when you compile the application. Take for example:

#define number 108



In this case every instance of "number" will be replaced by the actual number 108 in your code, and this means the final compiled program will have the number 108 (in binary).
On the other hand, when you use const and the application runs, memory is allocated for the constant and the value gets replaced when the applicaton is ran:

const int number = 108;

Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this Knowledge Base article
Comment Current Comments
by vidya on Wednesday, March 5th 2008 at 05:08 AM

Const is a declaration of constant, compiler will do appropriate checks during the compile time. #define is a macro compiler will see a value. Speaking in terms of memory const declaration could be more efficient:

const short x = 1;
// this will allocate 16 bits for x (actually it depends on the machine,
I assume unix platform with 32 bits integers)

#define x (short)1
// this will probably allocate 32 bits to hold the constant 1.

Use const instead of #define mainly because of compiler checks. And inline functions instead of macros as well. Preprocessor is the obsolete remnant from early C days, there's no use of it in the oop environment.
2)
no diffence realy in memory, both are compiled as numbers, but with const's you can define it per namespace or class so that you can use it like mylinklist::maxmemebers, it looks better and more readable.
3)
#define macro does not take any space on stack, but it gets replaced in the code by its definition thereby increasing the size of exe.

Constant variable occupies space on stack and does not increase the size of the exe.


by ramya on Monday, September 8th 2008 at 06:45 AM

excellent performance on this topic

by rasha najeeb on Wednesday, February 24th 2010 at 03:33 AM

الفرق بين const,#define

by rasha najeeb on Wednesday, February 24th 2010 at 03:33 AM

الفرق بين const,#define?

by rasha najeeb on Wednesday, February 24th 2010 at 03:33 AM

الفرق بين const,#define?

by rasha najeeb on Wednesday, February 24th 2010 at 03:33 AM

الفرق بين const,#define?

by on Thursday, January 19th 2012 at 06:15 AM

Once you have recreated the problem and captured these steps, you can save them to a file and send it to your support person, who can then open it up and view


Comment Comment on this Knowledge Base article
Name: Email:
Message:
Knowledge Base Related Knowledge Base Articles
There are no related KB articles.

Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs C++ Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Ads

From the creators of Geekpedia, a revolutionary new coupon website!

BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.

Coupons
Discover Geekpedia
Other Resources