Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Quick question about the c compiler
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
DtY




PostPosted: Fri May 15, 2009 10:30 pm   Post subject: Quick question about the c compiler

I guess this is more a question of what C compilers generally do, but also specifically how gcc will do it.

If you do some basic math on two constants, will it be evaluated at compile time, or at run time, like:
C:
#include <stdio.h>
#define WIDTH 800
int main(void) {
    int x = WIDTH-64;
    printf("%d",x);
    return 0;
}

Would it be compiled as "make x 800-64", or would the compiler see two constants, and take care of that step itself, "make x 736"?
Sponsor
Sponsor
Sponsor
sponsor
Zeroth




PostPosted: Fri May 15, 2009 10:35 pm   Post subject: Re: Quick question about the c compiler

A neat little command switch for gcc is -S which translates the source code to assembly code. Looking at the assembly code, I see that GCC, on my computer at least, precalculated the value of the variable, because GCC will try to do as many math operations as it can ahead of time.
DtY




PostPosted: Fri May 15, 2009 10:43 pm   Post subject: RE:Quick question about the c compiler

Ah, thanks
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: