473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

#define p

#define q p

I must admit to a most elementary confusion. I have only ever used
#define to, in point of fact, #redefine . The above is standard. Is
the following:

#define UNNAMED_OS_32_LEAN_AND_MEAN

? frank
--------
-1
Jun 7 '06 #1
11 2366
Frank Silvermann <in*****@invalid.net> writes:
#define q p

I must admit to a most elementary confusion. I have only ever used
#define to, in point of fact, #redefine . The above is standard. Is
the following:

#define UNNAMED_OS_32_LEAN_AND_MEAN


Yes. The syntax for a definition of an object-like macro (one that
doesn't take arguments) is:

# define identifier replacement-list new-line

The replacement-list can be empty. If it is, then any occurrence of
the identifier is replaced by nothing. Such an identifier is usually
used with "#ifdef" or "#if defined(...)".

Incidentally, your
#define q p
isn't what's normally called a redefinition. A redefinition is a
#define for something that's already been defined as a macro. It's
allowed only if the replacement-list is identical to the existing one.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 7 '06 #2
Keith Thompson wrote:
Frank Silvermann <in*****@invalid.net> writes:
#define q p

I must admit to a most elementary confusion. I have only ever used
#define to, in point of fact, #redefine . The above is standard. Is
the following:

#define UNNAMED_OS_32_LEAN_AND_MEAN


Yes. The syntax for a definition of an object-like macro (one that
doesn't take arguments) is:

# define identifier replacement-list new-line

The replacement-list can be empty. If it is, then any occurrence of
the identifier is replaced by nothing. Such an identifier is usually
used with "#ifdef" or "#if defined(...)".

Incidentally, your
#define q p
isn't what's normally called a redefinition. A redefinition is a
#define for something that's already been defined as a macro. It's
allowed only if the replacement-list is identical to the existing one.


Let me try the question from a different angle: when the preprocessor
hits the source in stage 1, what does it do besides deciding what is
#defined ?
Jun 7 '06 #3
jjf

Keith Thompson wrote:
Frank Silvermann <in*****@invalid.net> writes:
#define q p

I must admit to a most elementary confusion. I have only ever used
#define to, in point of fact, #redefine . The above is standard. Is
the following:

#define UNNAMED_OS_32_LEAN_AND_MEAN


Yes. The syntax for a definition of an object-like macro (one that
doesn't take arguments) is:

# define identifier replacement-list new-line

The replacement-list can be empty.


I can't find where the Standard allows the replacement-list to be
empty. In such cases, the Standard usually follows the term with a
suffix of 'opt' in the syntax summary, and it doesn't in this case. Nor
can I find any mention of an empty replacement-list is the discussion
of object-like macros.

I know that what you describe is how all C implementations I've used
work, but from a quick scan I can't see how the Standard allows it.
Could you explain how you derive this, please.

Jun 8 '06 #4
jj*@bcs.org.uk writes:
Keith Thompson wrote:
Frank Silvermann <in*****@invalid.net> writes:
> #define q p
>
> I must admit to a most elementary confusion. I have only ever used
> #define to, in point of fact, #redefine . The above is standard. Is
> the following:
>
> #define UNNAMED_OS_32_LEAN_AND_MEAN


Yes. The syntax for a definition of an object-like macro (one that
doesn't take arguments) is:

# define identifier replacement-list new-line

The replacement-list can be empty.


I can't find where the Standard allows the replacement-list to be
empty. In such cases, the Standard usually follows the term with a
suffix of 'opt' in the syntax summary, and it doesn't in this case. Nor
can I find any mention of an empty replacement-list is the discussion
of object-like macros.

I know that what you describe is how all C implementations I've used
work, but from a quick scan I can't see how the Standard allows it.
Could you explain how you derive this, please.


C99 6.10:

control-line:
...
# define identifier replacement-list new-line

...

replacement-list:
pp-tokens(opt)

(where "opt" is a subscript).

C90 6.8 has the same thing.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 8 '06 #5
Frank Silvermann <in*****@invalid.net> writes:
Keith Thompson wrote:
Frank Silvermann <in*****@invalid.net> writes:
#define q p

I must admit to a most elementary confusion. I have only ever used
#define to, in point of fact, #redefine . The above is standard. Is
the following:

#define UNNAMED_OS_32_LEAN_AND_MEAN

Yes. The syntax for a definition of an object-like macro (one that
doesn't take arguments) is:
# define identifier replacement-list new-line
The replacement-list can be empty. If it is, then any occurrence of
the identifier is replaced by nothing. Such an identifier is usually
used with "#ifdef" or "#if defined(...)".
Incidentally, your
#define q p
isn't what's normally called a redefinition. A redefinition is a
#define for something that's already been defined as a macro. It's
allowed only if the replacement-list is identical to the existing one.


Let me try the question from a different angle: when the preprocessor
hits the source in stage 1, what does it do besides deciding what is
#defined ?


I'm not sure what you mean. If by "stage 1" you mean "translation
phase 1", all that does is some character mapping and trigraph
replacement. Preprocessing directives aren't processed until phase 4.

Can you re-phrase the question, preferably with an example?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 8 '06 #6
Keith Thompson wrote:
Frank Silvermann <in*****@invalid.net> writes:
Keith Thompson wrote:
Frank Silvermann <in*****@invalid.net> writes:
#define q p

I must admit to a most elementary confusion. I have only ever used
#define to, in point of fact, #redefine . The above is standard. Is
the following:

#define UNNAMED_OS_32_LEAN_AND_MEAN
Yes. The syntax for a definition of an object-like macro (one that
doesn't take arguments) is:
# define identifier replacement-list new-line
The replacement-list can be empty. If it is, then any occurrence of
the identifier is replaced by nothing. Such an identifier is usually
used with "#ifdef" or "#if defined(...)".
Incidentally, your
#define q p
isn't what's normally called a redefinition. A redefinition is a
#define for something that's already been defined as a macro. It's
allowed only if the replacement-list is identical to the existing one.

Let me try the question from a different angle: when the preprocessor
hits the source in stage 1, what does it do besides deciding what is
#defined ?


I'm not sure what you mean. If by "stage 1" you mean "translation
phase 1", all that does is some character mapping and trigraph
replacement. Preprocessing directives aren't processed until phase 4.

Can you re-phrase the question, preferably with an example?

Yeah. What does the preprocessor do with:
#define SWAP(m, n) (tmp = m, m = n, n= tmp)
? This statement is different from
#define SOMETHING
, and according to the above, the preprocessor replaces SOMETHING with
nothing. (?)

The direction I'm headed with this is to wonder about the types of 'm'
and 'n'. frank
Jun 8 '06 #7
Frank Silvermann <in*****@invalid.net> writes:
[...]
Yeah. What does the preprocessor do with:
#define SWAP(m, n) (tmp = m, m = n, n= tmp)
?
This is a "function-like" macro. An invocation of it requires
arguments in parentheses. SWAP(foo, bar) is replaced by the
definition of SWAP, with each occurrence of m replaced by foo,
and each occurrence of n replaced by bar.
This statement is different from
#define SOMETHING
, and according to the above, the preprocessor replaces SOMETHING with
nothing. (?)
Right.
The direction I'm headed with this is to wonder about the types of 'm'
and 'n'. frank


They don't have types. Macro replacement just does textual
substitution (actually it works on tokens, but it's pretty much the
same thing). The preprocessor has no concept of types or expressions.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 8 '06 #8

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
Frank Silvermann <in*****@invalid.net> writes:
Yeah. What does the preprocessor do with:
#define SWAP(m, n) (tmp = m, m = n, n= tmp)
? This is a "function-like" macro. An invocation of it requires
arguments in parentheses. SWAP(foo, bar) is replaced by the
definition of SWAP, with each occurrence of m replaced by foo,
and each occurrence of n replaced by bar.

I agologize in advance for confusion with my newsreaders, the other one of
which is behaving badly despite having been killed and re-installed.
Anyways, am I right to think that if I have this macro, then I'm going to
want to have a 'tmp', 'm' and 'n' of the same type, and that this type could
be different depending on what is in scope during the SWAP?
This statement is different from
#define SOMETHING
, and according to the above, the preprocessor replaces SOMETHING with
nothing. (?)


Right.

But this amnesia comes after the #IFDEF's, probably right at the end of step
4?
The direction I'm headed with this is to wonder about the types of 'm'
and 'n'. frank


They don't have types. Macro replacement just does textual
substitution (actually it works on tokens, but it's pretty much the
same thing). The preprocessor has no concept of types or expressions.

Given that a person is going to do fewer than ten thousand of these swaps,
and that whatever is passed by value isn't bulky, I wonder aloud as to the
style of it. joe
---------
OT:
When I killed thunderbird and reinstalled, it had all the same settings and
it looks like appwizard gone awry. (?)
Jun 8 '06 #9
"Joe Smith" <gr**********@netzero.net> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
Frank Silvermann <in*****@invalid.net> writes:

Yeah. What does the preprocessor do with:
#define SWAP(m, n) (tmp = m, m = n, n= tmp)
?
This is a "function-like" macro. An invocation of it requires
arguments in parentheses. SWAP(foo, bar) is replaced by the
definition of SWAP, with each occurrence of m replaced by foo,
and each occurrence of n replaced by bar.

I agologize in advance for confusion with my newsreaders, the other one of
which is behaving badly despite having been killed and re-installed.
Anyways, am I right to think that if I have this macro, then I'm going to
want to have a 'tmp', 'm' and 'n' of the same type, and that this type could
be different depending on what is in scope during the SWAP?


Right. This:

SWAP(foo, bar)

is *exactly* equivalent to this:

(tmp = foo, foo = bar, bar= tmp)

so exactly the same considerations apply.

Incidentally, it's generally safer to fully parenthesize each macro
argument in a macro definition:

#define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)

It shouldn't matter for any reasonable invocation of SWAP(), but it's
easier to *always* use parentheses than to try to figure out when
they're not required (and force anyone reading and/or maintaining the
code to stop and think about it).
This statement is different from
#define SOMETHING
, and according to the above, the preprocessor replaces SOMETHING with
nothing. (?)


Right.

But this amnesia comes after the #IFDEF's, probably right at the end of step
4?


Yes, assuming that "step 4" means "translation phase 4".

Here's my usual example of preprocessor abuse:

#include <stdio.h>

#define SIX 1+5
#define NINE 8+1

int main(void)
{
printf("%d * %d = %d\n", SIX, NINE, SIX * NINE);
return 0;
}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 8 '06 #10
Keith Thompson wrote:
"Joe Smith" <gr**********@netzero.net> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
Frank Silvermann <in*****@invalid.net> writes:
Yeah. What does the preprocessor do with:
#define SWAP(m, n) (tmp = m, m = n, n= tmp)
?
This is a "function-like" macro. An invocation of it requires
arguments in parentheses. SWAP(foo, bar) is replaced by the
definition of SWAP, with each occurrence of m replaced by foo,
and each occurrence of n replaced by bar.

I agologize in advance for confusion with my newsreaders, the other one of
which is behaving badly despite having been killed and re-installed.
Anyways, am I right to think that if I have this macro, then I'm going to
want to have a 'tmp', 'm' and 'n' of the same type, and that this type could
be different depending on what is in scope during the SWAP?


Right. This:

SWAP(foo, bar)

is *exactly* equivalent to this:

(tmp = foo, foo = bar, bar= tmp)

so exactly the same considerations apply.

Incidentally, it's generally safer to fully parenthesize each macro
argument in a macro definition:

#define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)

It shouldn't matter for any reasonable invocation of SWAP(), but it's
easier to *always* use parentheses than to try to figure out when
they're not required (and force anyone reading and/or maintaining the
code to stop and think about it).
This statement is different from
#define SOMETHING
, and according to the above, the preprocessor replaces SOMETHING with
nothing. (?)
Right.

But this amnesia comes after the #IFDEF's, probably right at the end of step
4?


Yes, assuming that "step 4" means "translation phase 4".

Here's my usual example of preprocessor abuse:

#include <stdio.h>

#define SIX 1+5
#define NINE 8+1

int main(void)
{
printf("%d * %d = %d\n", SIX, NINE, SIX * NINE);
return 0;
}

Fun example. The fellow who was top-posting today wrote:
"Btw... nice use, of algebraic precedence (always nice to see)."
I didn't want to ruin his day and tell him that algebraists typically
work right to left and that telling left from right is the hardest thing
we do. Your example shows good form not just for a walking C
encyclopedia, but the next guy as well. frank
Jun 9 '06 #11
On Thu, 08 Jun 2006 19:00:49 GMT, Keith Thompson <ks***@mib.org>
wrote:
Frank Silvermann <in*****@invalid.net> writes:

The direction I'm headed with this is to wonder about the types of 'm'
and 'n'. frank


They don't have types. Macro replacement just does textual
substitution (actually it works on tokens, but it's pretty much the
same thing). The preprocessor has no concept of types or expressions.


<pedantic> Preprocessor _macros_ have no concept of types, or of
expressions beyond matching/nested parentheses.

Preprocessor #if and #elif do have expressions _almost_ the same as
constant (compile-time) expressions in the language, but types limited
to the largest integer types, [u]long in C90 and [u]intmax_t in C99.

- David.Thompson1 at worldnet.att.net
Jun 19 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Bryan Parkoff | last post by:
"#define" can only be inside the global scope before main() function. "#if" can be tested after "#define" is executed. The problem is that "#define" can't be inside main() function. I do not wish...
2
by: Andreas | last post by:
Hi! I'm using an IplImage library from camellia.sourceforge.net, and the testbench calls a file only containing code like the one below. In cam_morphomaths_code.c the real computation is made,...
3
by: theotyflos | last post by:
Hi all, I have the following: /*--- SNIP ---*/ typedef struct Argument_s { char *address; int type;
9
by: pozz | last post by:
Hi all, I have the below #defines #define NUMBER1 30 #define NUMBER2 50 #define SUM (NUMBER1+NUMBER2) #define STRING1 "Byte: \x30" #define STRING2 "Byte: \x50"...
34
by: BQ | last post by:
Hello Is there a way to declare 'FUNCT' via a define so that if its parameter x, a constant, is greater than 35, it returns 56, if not, 20. I would like that at compile time, not at run time. ...
17
by: niraj.tiwari | last post by:
What is meaning of the following define:- #define x(argl...) x1(##argl)
71
by: David T. Ashley | last post by:
Where is the best place to define TRUE and FALSE? Are they in any of the standard include files, ever? Do any standards apply? What I've traditionally done is something like: #ifndef...
6
by: anirbid.banerjee | last post by:
Hi, I need to write a macro which would have a lot many conditional #ifdef ... #endif blocks in it. #define _xx_macro (x) { ... \ ... \ /* some code (); */ #ifdef _SOME_STMT \ ... \ ... \
23
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
2
by: badc0de | last post by:
Hello.. a header file of one of my project has macro definitions like this (for example) #define PART_A_SORT_1_LABEL_STRING1 100 #define PART_A_SORT_1_LABEL_STRING2 200 #define...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.