Strange Preprocessor Behavior 
Author Message
 Strange Preprocessor Behavior

Check out this code:

  #define X -foo

  void main ( void )
  {
    int foo = 10;

    -X;               // <-- Should expand to '--foo' ???

    printf( "\nfoo: %d\n", foo );
  }

I would expect the '-X' to expand to '--foo' causing foo to be decremented.
However, the program prints out 10 instead of 9.  Does anyone know why
this would happen.  Note that if I force my compiler to only preprocess
the code, then compile that code as a seperate stage, the program outputs a
9 ( as expected ).  I'm using MSC 8.0, but have also tried this with and
older Borland compiler with the same results.  I've read that a "strictly
ANSI compliant" complier will generate this behavior, but I don't
understand why.

Jeff Bisset



Fri, 23 May 1997 17:37:56 GMT  
 Strange Preprocessor Behavior

Quote:
>Check out this code:

'k.

Quote:
>  #define X -foo

...okay...

Quote:
>  void main ( void )

This is an incorrect declaration; use 'int main(void)' or just 'main()' for
short test programs.

Quote:
>  {
>    int foo = 10;

>    -X;               // <-- Should expand to '--foo' ???

The '//' is a syntax error.  Please use C comments when writing C.  Like the
void main, this is something illegal your compiler happens to allow; be wary of
extensions.

Your problem is that ANSI makes it quite clear that all tokens have implicit
whitespace between them, especially when preprocessing.
Thus, your line expands to
- -foo;
(Which is '10'.)

Quote:
>    printf( "\nfoo: %d\n", foo );
>  }
>I would expect the '-X' to expand to '--foo' causing foo to be decremented.
>However, the program prints out 10 instead of 9.  Does anyone know why
>this would happen.  Note that if I force my compiler to only preprocess
>the code, then compile that code as a seperate stage, the program outputs a
>9 ( as expected ).  I'm using MSC 8.0, but have also tried this with and
>older Borland compiler with the same results.  I've read that a "strictly
>ANSI compliant" complier will generate this behavior, but I don't
>understand why.
>Jeff Bisset


Well, now you know.

I would consider the behavior change when preprocessing/compiling seperately
to be a bug.  It's not illegal, but it's definitely rude.

-seebs
p.s.: Note, no 'go away, DOS user.'  See?  I told you we
answer legitimate questions.
--

C/Unix proto-wizard -- C/Unix questions? Send mail for help.



Fri, 23 May 1997 23:25:37 GMT  
 Strange Preprocessor Behavior

|> Check out this code:
|>
|>   #define X -foo
|>
|>   void main ( void )

Once you do this (declare main as void) your compiler is allowed to do whatever
it likes: The standard does not guarantee that this program will work or that it
won't delete all your files.

|>   {
|>     int foo = 10;
|>
|>     -X;               // <-- Should expand to '--foo' ???
|>
|>     printf( "\nfoo: %d\n", foo );

It is very bad practice not to return an integer from main. A `return 0;' is
strongly suggested.

|>   }
|>
|> I would expect the '-X' to expand to '--foo' causing foo to be decremented.

See below. Some pre-ANSI compilers used to do this.

|> However, the program prints out 10 instead of 9.  Does anyone know why
|> this would happen.  Note that if I force my compiler to only preprocess
|> the code, then compile that code as a seperate stage, the program outputs a
|> 9 ( as expected ).  I'm using MSC 8.0, but have also tried this with and

What the compiler does when you tell it to `only preprocess' is not dictated by
the standard. It can do whatever it likes.

|> older Borland compiler with the same results.  I've read that a "strictly
|> ANSI compliant" complier will generate this behavior, but I don't
|> understand why.
|>

ANSI preprocessing is defined in terms of preprocessing tokens: it is not a
`string' replacement on the source code.

To be specific, the define line defines the preprocessing token to be an
object-like macro expanding to the pair of preprocessing tokens - and foo.
When the preprocessor sees -X, it sees two preprocessing tokens - and X. X now
expands to the two tokens - and foo, giving three token -, - and foo: which
ultimately has the same effect as the code - - foo: not the same as --foo.

You can form one preprocessing token out of two of them in a macro using the
operator ##. Try for example

#define A(X,Y) X##Y

int main(void) {
  int x=10;
  A(-,-)x;
  printf("%d\n",x);
  return 0;

Quote:
}

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87544-0285,USA H:#3,802,9 St,NM87545

<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]


Sat, 24 May 1997 03:54:01 GMT  
 Strange Preprocessor Behavior

Quote:

> Check out this code:
>   #define X -foo
>   void main ( void )
>   {
>     int foo = 10;
>     -X;               // <-- Should expand to '--foo' ???
>     printf( "\nfoo: %d\n", foo );
>   }
> I would expect the '-X' to expand to '--foo' causing foo to be decremented.
> However, the program prints out 10 instead of 9.  Does anyone know why
> this would happen.  Note that if I force my compiler to only preprocess
> the code, then compile that code as a seperate stage, the program outputs a
> 9 ( as expected ).  I'm using MSC 8.0, but have also tried this with and
> older Borland compiler with the same results.  I've read that a "strictly
> ANSI compliant" complier will generate this behavior, but I don't
> understand why.

In C the preprocessor breaks the input into tokens.  These tokens, not
the text are passed to the compiler.

The preprocessor sees -X as two preprocessor tokens.  X is expanded into
two preprocessor tokens, - and foo.  The full expansion end up being three
preprocessor tokens -, -, and foo which are converted to tokens and passed
to the compiler.

In short, once the preprocessor breaks text up into preprocessor tokens,
they can never be recombined to form a single token again.

--
Mike Rubenstein



Wed, 28 May 1997 06:01:42 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Strange, very strange OnGetMinMaxInfo() behavior

2. Strange, very strange OnGetMinMaxInfo() behavior

3. Please explain this [ANSI] preprocessor behavior

4. Behavior of #error preprocessor directive

5. strange output from the preprocessor

6. Strange C-preprocessor problem

7. Strange constant behavior - why?

8. Asynchronous CallBack strange behavior

9. ArrayList help (strange behavior)

10. Strange behavior in program

11. Strange tabcontrol behavior

12. xmldom.LoadXml - causes strange behavior

 

 
Powered by phpBB® Forum Software