LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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
  Search this Thread
Old 12-03-2003, 12:49 PM   #1
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
question on #define


can we do a #define at any point in the code?

say i perform some task, and get a result xyz.

can i do a '#define RESULT xyx" at some point in the code?

thanks.
 
Old 12-03-2003, 12:58 PM   #2
MartinN
Member
 
Registered: Nov 2003
Location: Ronneby, Sweden
Posts: 555

Rep: Reputation: 30
A define is expanded by the C pre-processor. What it does is that it replaces, textually, the first argument of the define with the second before passing the code on to the compiler.

Because of that, your question doesn't make sense! If the "answer" is not known at compile time, then (obviously) the pre-processor can't make the replacing.

Regards
Martin
 
Old 12-03-2003, 01:06 PM   #3
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
right! thanks!
 
Old 12-03-2003, 02:04 PM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
#define will expand macros which can do things:
Code:
#define zout(z) memset(&z,0x00,sizeof(z))

.........
zout(mystring);
will expand to:
memset(&mystring,0x00,sizeof(mystring));
You also #define multiline macros which are really more like a way to inline a function:

Code:
// this function does a complex square calc 
#define CMPLXsqr_old(out)	\     
   (out).y = (old.x+old.x) * old.y;\  
   (out).x = tempsqrx - tempsqry
 
Old 12-03-2003, 03:12 PM   #5
MartinN
Member
 
Registered: Nov 2003
Location: Ronneby, Sweden
Posts: 555

Rep: Reputation: 30
Now I must raise a finger of warningabout the use of pre-processor macros.

Look at this seemingly innocent macro:
Code:
#define MAX(x, y)   ((x)>(y)?(x): (y))
Now consider what happens if we call this with
Code:
MAX(++j, ++k);
That looks pretty straight forward and correct, right? Wrong! Well what happens is that whichever variable is bigger gets increased twice! Why? Because the macro has been textually expanded to
Code:
++j > ++k ? ++j : ++k;
This is why inline is a good keyword in C++.

Regards
Martin
 
Old 12-03-2003, 04:40 PM   #6
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
right. thanks.
 
Old 12-03-2003, 04:42 PM   #7
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
But is not available for a lot of plain C implementations.
 
Old 12-03-2003, 05:14 PM   #8
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
u mean, inline isnt ?
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ question #define ... Bluesuperman Programming 4 02-01-2005 09:49 AM
Question: How to define an SQL HAVING clause in relational algebra? jdruin Linux - Software 1 11-08-2004 07:50 AM
define jhon Linux - Networking 2 08-30-2004 07:42 PM
define main linuxanswer Programming 8 11-05-2003 02:02 PM
using #define Jo_Nak Programming 4 06-11-2003 01:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:30 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration