473,700 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

#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_L EAN_AND_MEAN

? frank
--------
-1
Jun 7 '06 #1
11 2401
Frank Silvermann <in*****@invali d.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_L EAN_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_Keit h) 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*****@invali d.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_L EAN_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*****@invali d.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_L EAN_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*****@invali d.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_L EAN_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_Keit h) 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*****@invali d.net> writes:
Keith Thompson wrote:
Frank Silvermann <in*****@invali d.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_L EAN_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 "translatio n
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_Keit h) 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*****@invali d.net> writes:
Keith Thompson wrote:
Frank Silvermann <in*****@invali d.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_L EAN_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 "translatio n
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*****@invali d.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_Keit h) 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.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
Frank Silvermann <in*****@invali d.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**********@n etzero.net> writes:
"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
Frank Silvermann <in*****@invali d.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 "translatio n 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_Keit h) 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

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

Similar topics

18
8921
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 to create multiple functions which they look almost identical in C++ source code. The C++ compiler should be able to compile one Test() function into two almost identical functions before they are translated into the machine language object. ...
2
5589
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, but I don't understand where cam_morphomaths_code.c is included and how to set what parameters to use in the testbench. How does this code execure? I don't understand a bit...
3
10575
by: theotyflos | last post by:
Hi all, I have the following: /*--- SNIP ---*/ typedef struct Argument_s { char *address; int type;
9
3090
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" If I change NUMBER1 and NUMBER2 I must change STRING1 and STRING2 consequently.
34
3195
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. So: #define FUNCT(x) x>35?56:20
17
2771
by: niraj.tiwari | last post by:
What is meaning of the following define:- #define x(argl...) x1(##argl)
71
33193
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 (TRUE) #define TRUE (1)
6
27853
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
3909
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
2297
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 PART_A_SORT_1_LABEL_STRING3 300 #define PART_A_SORT_1_LABEL_STRING4 400 #define PART_A_SORT_1_LABEL_STRING5 500
0
8725
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8644
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9214
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8970
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8924
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6560
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5902
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2027
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.