Search code examples
carea

#define in C: what is happening


Can someone explain what is happening at every step? I know the final output is 140.5, but I am unsure why that is. What is happening at each line that is resulting in 140.5?

#define PI 3.1
#define calcCircleArea(r) (PI * (r) * (r))
#define calcCylinderArea(r,h) (calcCircleArea(r) * h)
int main() {
    double i = calcCylinderArea(3.0,5.0 + 1); printf("%g", i);
}

Solution

  • Step 0

    calcCylinderArea(3.0,5.0+1)
    

    Step 1

    (calcCircleArea(3.0)*5.0+1)
    

    notice that it is not (5.0+1).
    Problem begins here.

    Step 2

    ((PI*(3.0)*(3.0))*5.0+1)
    

    Step 3

    ((3.1*(3.0)*(3.0))*5.0+1)