GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Feb-2006, 06:16
Red83 Red83 is offline
Junior Member
 
Join Date: Feb 2006
Posts: 30
Red83 is on a distinguished road

preprocessor question??


I was looking at a piece of code and trying to figure out how the preprocessor would expand it. I really can't seem to figure it out correctly.
Could someone help me out? I just need a little explanation. I know it's not formatted very well, but I wanted to give exactly what I'm seeing. My guess is that it will just be expanded to just int N, but I'm not sure.
Thanks a bunch.


CPP / C++ / C Code:
#define USEFLOAT 0
int N;
#if USEFLOAT
float X;
#else
double X;
#endif
  #2  
Old 21-Feb-2006, 07:24
davis
 
Posts: n/a

Re: preprocessor question??


Quote:
Originally Posted by Red83
I was looking at a piece of code and trying to figure out how the preprocessor would expand it. I really can't seem to figure it out correctly.
Could someone help me out? I just need a little explanation. I know it's not formatted very well, but I wanted to give exactly what I'm seeing. My guess is that it will just be expanded to just int N, but I'm not sure.
Thanks a bunch.


CPP / C++ / C Code:
#define USEFLOAT 0
int N;
#if USEFLOAT
float X;
#else
double X;
#endif

...you don't need to ask these kinds of questions. Just drop it into a piece of code and build it (or try to), you'll see what happens.

CPP / C++ / C Code:
#include <stdio.h>

#define USEFLOAT 0
int N;
#if USEFLOAT
float X;
#else
double X;
#endif

int main(void)
{
    N = 1;
    X = 1.05;

    printf( "N = %d\n", N );
    printf( "sizeof( X ) = %d\n", sizeof( X ) );

    return 0;
}

// output:

N = 1
sizeof( X ) = 8

Here we see that N is being defined, otherwise we'd get an error telling us that (in main) that N is an undefined symbol. And, X is a double, because it uses 8 bytes of storage whereas float is a 4-byte type.

Also, you can see exactly what your preprocessor does by taking a look at your tool chain reference to see how to run just the preprocessor and then stop and look at its output. If you want to test macros for proper/expected expansion, you can comment-out any header files that you include, run the preprocessor and look at its output.

What book are you using and what tool chain?



:davis:
  #3  
Old 21-Feb-2006, 07:38
Red83 Red83 is offline
Junior Member
 
Join Date: Feb 2006
Posts: 30
Red83 is on a distinguished road

Re: preprocessor question??


My book is "C by dissection" by Al Kelley and Ira Pohl. I'm sorry, but I'm not sure what the tool chain is.
  #4  
Old 21-Feb-2006, 07:52
davis
 
Posts: n/a

Re: preprocessor question??


Quote:
Originally Posted by Red83
My book is "C by dissection" by Al Kelley and Ira Pohl. I'm sorry, but I'm not sure what the tool chain is.

What compiler/software are you using?

...also, when mentioning books, you may want to include the copyright date and/or any relevant revision/edition information. If you're using the 4th edition, the copyright date is 2000. The reason for this is so that we know whether or not you're using an outdated (pre-standards) book. If you were, we'd want to point you toward something more recent.


:davis:
  #5  
Old 21-Feb-2006, 07:59
Red83 Red83 is offline
Junior Member
 
Join Date: Feb 2006
Posts: 30
Red83 is on a distinguished road

Re: preprocessor question??


The book is a 4th edition, and my software is VisualStudio.NET.
I just wasn't familiar with the tool chain terminology.
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:33.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.