473,473 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Integer Literals

A question recently came up of late in some interviewing techniques
discussions and I vaguely remember this being an ANSI C++ related issue
but cannot remember the specifics. Basically if you have the following:

#define foo(x) x*x

..
..
..

foo(3+2);

Beside the obvious feau paux of not wrapping the macro parameter in
parens, the common misconception is the actual macro side affect - i.e.
the addition will take place before the substitution:

i.e. foo(3+2) = foo(5) = 5*5 = 25

Deeper insight believes that the substitution will take place first:

i.e. foo(3+2) = 3+2*3+2 = 3+(2*3)+2 = 11

However, it is my understanding the preprocessors can actually combine
literals (it's left to the implementation) giving a result of the
original first inclination:

i.e. foo(3+2) = foo(5) = 5*5 = 25

Now, in this case the point would be moot (and thuis the side affect
generated) if these were variables and not interger literals:

i.e.
int y= 3;
int z = 2;
foo(y+z) = y+z*y+z = y+(z*y)+z = (at run time) 3+(2*3)+2 = 11

Anyway, the question comes down to whether or not the preprocessor is
allowed to do mathematical calculations on integer literals. Anybody
know?

Oct 18 '06 #1
5 2158
br*****@gmail.com wrote:
A question recently came up of late in some interviewing techniques
discussions and I vaguely remember this being an ANSI C++ related
issue but cannot remember the specifics. Basically if you have the
following:

#define foo(x) x*x

.
.
.

foo(3+2);

Beside the obvious feau paux of not wrapping the macro parameter in
parens, the common misconception is the actual macro side affect -
i.e. the addition will take place before the substitution:

i.e. foo(3+2) = foo(5) = 5*5 = 25

Deeper insight believes that the substitution will take place first:

i.e. foo(3+2) = 3+2*3+2 = 3+(2*3)+2 = 11

However, it is my understanding the preprocessors can actually combine
literals (it's left to the implementation) giving a result of the
original first inclination:

i.e. foo(3+2) = foo(5) = 5*5 = 25

Now, in this case the point would be moot (and thuis the side affect
generated) if these were variables and not interger literals:

i.e.
int y= 3;
int z = 2;
foo(y+z) = y+z*y+z = y+(z*y)+z = (at run time) 3+(2*3)+2 = 11

Anyway, the question comes down to whether or not the preprocessor is
allowed to do mathematical calculations on integer literals. Anybody
know?
The Standard mandates the order of things happening in "2.1 Phases of
translation" which states that the source file is decomposed into
preprocessing tokens, then directives are executed and macros are
expanded. No tranlsation happens between decomposing into tokens (step
3) and substituting macros (step 4). All translation happens _after_
the processing steps 1 through 6 are done.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 18 '06 #2
OK - wish I had the standard to clarify as I don't quite understand
your response. The three items you state don't (seemingly) deal with
integer literas but "preprocessing tokens, then directives are executed
and macros are expanded". In this case are we talking the mathematical
work on integer literals are tokens or translations?

Victor Bazarov wrote:
br*****@gmail.com wrote:
A question recently came up of late in some interviewing techniques
discussions and I vaguely remember this being an ANSI C++ related
issue but cannot remember the specifics. Basically if you have the
following:

#define foo(x) x*x

.
.
.

foo(3+2);

Beside the obvious feau paux of not wrapping the macro parameter in
parens, the common misconception is the actual macro side affect -
i.e. the addition will take place before the substitution:

i.e. foo(3+2) = foo(5) = 5*5 = 25

Deeper insight believes that the substitution will take place first:

i.e. foo(3+2) = 3+2*3+2 = 3+(2*3)+2 = 11

However, it is my understanding the preprocessors can actually combine
literals (it's left to the implementation) giving a result of the
original first inclination:

i.e. foo(3+2) = foo(5) = 5*5 = 25

Now, in this case the point would be moot (and thuis the side affect
generated) if these were variables and not interger literals:

i.e.
int y= 3;
int z = 2;
foo(y+z) = y+z*y+z = y+(z*y)+z = (at run time) 3+(2*3)+2 = 11

Anyway, the question comes down to whether or not the preprocessor is
allowed to do mathematical calculations on integer literals. Anybody
know?

The Standard mandates the order of things happening in "2.1 Phases of
translation" which states that the source file is decomposed into
preprocessing tokens, then directives are executed and macros are
expanded. No tranlsation happens between decomposing into tokens (step
3) and substituting macros (step 4). All translation happens _after_
the processing steps 1 through 6 are done.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 18 '06 #3
br*****@gmail.com wrote:
Victor Bazarov wrote:
>>br*****@gmail.com wrote:
[snipped question about whether arithmetic computations on literals are
performed before or after macro substitution]
>>
The Standard mandates the order of things happening in "2.1 Phases of
translation" which states that the source file is decomposed into
preprocessing tokens, then directives are executed and macros are
expanded. No tranlsation happens between decomposing into tokens (step
3) and substituting macros (step 4). All translation happens _after_
the processing steps 1 through 6 are done.
OK - wish I had the standard to clarify as I don't quite understand
your response. The three items you state don't (seemingly) deal with
integer literas but "preprocessing tokens, then directives are
executed
and macros are expanded". In this case are we talking the mathematical
work on integer literals are tokens or translations?
Mathematical computation is clearly not part of the tokenization
process, as tokenization means breaking a text into smaller tokens that
will be processed one at a time. The tokenizer doesn't know anything
about the semantics of the language constructs. Concerning your original
question I have to say that mathematical computations on literals are
performed _after_ the expansion of macros, thus it is non-conforming if
preprocessors do anything else but macro expansion or sources including.
>>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Had you not top-posted your reply, you may had got an answer from Victor
(who has certainly better knowledge of these issues than I).

Regards,
Stuart
Oct 19 '06 #4
* br*****@gmail.com:
Anyway, the question comes down to whether or not the preprocessor is
allowed to do mathematical calculations on integer literals. Anybody
know?
Not in ordinary macro expansion.

However, the controlling expression of an #if or #elif is an integral
constant expression, which is evaluated by the preprocessor.

So, the literal answer is "yes", but the answer in the context of
ordinary macro expansion is "no", except when the macro is used as a
controlling expression for an #if or #elif (in which case it's expanded
before the preprocessor evaluates the expression).

Hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 19 '06 #5
Thanks y'all!

Alf P. Steinbach wrote:
* br*****@gmail.com:
Anyway, the question comes down to whether or not the preprocessor is
allowed to do mathematical calculations on integer literals. Anybody
know?

Not in ordinary macro expansion.

However, the controlling expression of an #if or #elif is an integral
constant expression, which is evaluated by the preprocessor.

So, the literal answer is "yes", but the answer in the context of
ordinary macro expansion is "no", except when the macro is used as a
controlling expression for an #if or #elif (in which case it's expanded
before the preprocessor evaluates the expression).

Hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 19 '06 #6

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

Similar topics

25
by: bruce.james.lee | last post by:
hi i have a problem with integer subtraction in C. printf("%d", c < (a - b)); a is got from a #define and is 0x80000000 and b is got from input and is also 0x80000000. c is ffffffff (-1). Now,...
61
by: John Baker | last post by:
When declaring an integer, you can specify the size by using int16, int32, or int64, with plain integer being int32. Is integer the accepted default in the programming community? If so, is...
16
by: Stefan Wallentowitz | last post by:
Hello together! I'm searching for a statistic about the value ranges of integers in a set of "standard" c-programs. I'm specifically interested in the avarage ratio of assignments etc. of...
33
by: gk245 | last post by:
I mean, anything that follows a 0 is automatically turned into a octal number. I want to have a integer variable that will hold numbers as integers even if they begin with a zero. for example:...
6
by: John Dann | last post by:
I'm trying to use a third party .Net charting control. One of its methods takes a standard 4-byte integer as a parameter (to specify a colour actually, but it's done as a standard integer value and...
15
by: Ivan Novick | last post by:
Hi, Is it possible to have negative integer literal or only positive? As far as I understand, the code below would be a positive integer literal and the unary negative operator. x = -3.2; ...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
7
by: Gary Brown | last post by:
Hi, I have a whole bunch of integer constants that are best given in octal (PDP-1 opcodes). It makes a huge difference in readability and authenticity if these can be entered in octal. I...
4
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Here's a macro that Mathew Hendry posted back in the year 2000 for achieving binary integer literals that evaluate to compile-time constants: #define BIN8(n)\ (((0x##n##ul&1<<...
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
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
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...
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.