LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 06-25-2005, 08:17 AM   #1
b123coder
Member
 
Registered: Nov 2004
Location: india
Distribution: Fedora core 1
Posts: 67

Rep: Reputation: 15
diff between #define and const defined variable


hello,
Can anybody tell me what is the difference between #define and const defined variable? I think both defines variables as const type but is there really any difference between them?
 
Old 06-25-2005, 08:30 AM   #2
LinuxLala
Senior Member
 
Registered: Aug 2003
Location: New Delhi, India
Distribution: Fedora 7
Posts: 1,305

Rep: Reputation: 45
I believe that #define makes the variable global. On the other hand, you can declare local constant variables.

Other than that, I am not aware of any difference.
 
Old 06-25-2005, 08:33 AM   #3
LinuxLala
Senior Member
 
Registered: Aug 2003
Location: New Delhi, India
Distribution: Fedora 7
Posts: 1,305

Rep: Reputation: 45
http://cplus.about.com/od/beginnerct.../aa021702a.htm

Good explanation here.

Cheers!
 
Old 06-25-2005, 09:38 AM   #4
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,192

Rep: Reputation: 101Reputation: 101
When you use #define, all the constants get replaced with their actual values before compilation by the preprocessor, where as a const variable is implmented as a variable and you get the the overhead associated with implementing a normal variable.

FYI const variables can be globally defined aswell
 
Old 06-25-2005, 09:41 AM   #5
LinuxLala
Senior Member
 
Registered: Aug 2003
Location: New Delhi, India
Distribution: Fedora 7
Posts: 1,305

Rep: Reputation: 45
Like I said, const *can* be global or local but #define *makes* them global
 
Old 06-25-2005, 11:21 AM   #6
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,192

Rep: Reputation: 101Reputation: 101
Sorry,

You said that #define makes constants global, and you can declare const variables local, you never said they could be global aswell, but maybe you were implying that? it wasn't clear to me which is why I added the last line in my previous post
 
Old 06-25-2005, 12:08 PM   #7
LinuxLala
Senior Member
 
Registered: Aug 2003
Location: New Delhi, India
Distribution: Fedora 7
Posts: 1,305

Rep: Reputation: 45
Hey phil,

no sorry needed buddy. I didn't make that point very clear. Your post made the meaning clearer, and that's a good thing.

Cheers!
 
Old 06-27-2005, 08:02 AM   #8
mehuljv
Member
 
Registered: Nov 2004
Posts: 72

Rep: Reputation: 15
hi,

"#define" is useful where memory is constrain... like RTOS.. because it will not get store in memory. if memory is not a constrain than u can use either of these. But u can use pointer to the constant variable while to define u cant. using pointer to the constant variable u can even change the data bt u cant change the data of the defined one because it never stores in memry.

Regards
Mehul.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++: #define OR const int ? Hady Programming 5 10-07-2005 11:43 AM
how to define user-defined commands in linux mannoulla Linux - General 1 04-05-2005 09:01 PM
global const int X = DEFINED (shows value in debugger?) abs Programming 2 09-25-2004 11:09 AM
how am i define an environment variable? yenonn Slackware 2 12-19-2003 07:44 AM
how to define a global variable Anniebaby Programming 1 11-09-2003 10:43 PM


All times are GMT -5. The time now is 10:48 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration