c > efficiency

define.c


/* #define is used to define macros
 * The # means it is a preprocessor directive
 * Macros are identifiers which will be replaced
 * by their values/definitions before compilation
 * Macros can be used to define more than just constants
 * Macros can be object-like, chain, multi-line or function-like
 */

#include <stdio.h>
#include <math.h>

// a macro for a constant
#define g 9.81
// a macro for an expression
#define PI acos(-1.0)
// a macro for an expression with arguments
#define circle_area(r) (PI*r*r) 
// macro with mutiple arguments
#define max(a,b) ((a>b)?a:b)

int main(){
    float radius = 0.5;
    printf("The value of PI is %f\n",PI);
    printf("The area of a circle with radius %f m is %f m^2\n",radius,circle_area(radius));
    double new_radius = 4;
    printf("The area of a circle with radius %g m is %g m^2\n",new_radius,circle_area(new_radius));
    printf("The maximum of %d and %d is %d\n",5,9,max(5,9));

    return  0;
}