Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Compiler Question

Status
Not open for further replies.

electroRF

Member
Hi,

I have a basic compiler question, which actually might be a pre-processor question.

Please see the following simple code:
C:
#define LEN       50
#define WID       20
#define AREA      LEN * WID

int TotalArea(int num)
{
    int retTotalArea = num * AREA;
    return retTotalArea;
}

Will the AREA Define be computed at Compiling time, or at run time?

meaning, will the embedded processor compute by itself the 20*50, or will it be done at compilation?

Thanks.
 
Yes.. I'm a little tired, but I don't see any problems there.. other than style. The AREA will be calculated at compile time and is treated as literal constant.

Better (safer) style would be to write
C:
#define LEN       (50)
#define WID       (20)
#define AREA      ((LEN)*(WID))

int TotalArea(int num)
{
   int retTotalArea = num * AREA;
   return retTotalArea;
}

The parentheses are important in the AREA define if and when you use the AREA in more complicated equation. And if you modify the LEN and WID to something more complicated also.
 
Thank you misterT :)

That's great that I can use multiplications of numbers knowing that they would be calculated at compilation time.

You're right about the extra parenthesis.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top