Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Integer Literals

Reply
Thread Tools

Integer Literals

 
 
bradeck@gmail.com
Guest
Posts: n/a
 
      10-18-2006
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?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-18-2006
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


 
Reply With Quote
 
 
 
 
bradeck@gmail.com
Guest
Posts: n/a
 
      10-18-2006
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:
> 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


 
Reply With Quote
 
Stuart Redmann
Guest
Posts: n/a
 
      10-19-2006
wrote:
> Victor Bazarov wrote:
>> 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
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-19-2006
* :
> 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?
 
Reply With Quote
 
bradeck@gmail.com
Guest
Posts: n/a
 
      10-19-2006
Thanks y'all!

Alf P. Steinbach wrote:
> * :
> > 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?


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Integer Literals Tricky VHDL 4 04-07-2008 07:43 PM
negative integer literals Ivan Novick C++ 15 12-10-2006 09:24 PM
Java: byte literals and short literals John Goche Java 8 01-17-2006 11:12 PM
Template argument deduction on integer literals Bart Samwel C++ 14 04-22-2005 10:03 PM
Generic class literals - e.g,, Class<Map<String, Integer>>.class Purush Java 4 04-13-2005 08:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57