20-CS-4003-001 Organization of Programming Languages Fall 2017

Lambda calculus, Type theory, Formal semantics, Program analysis

    Prev     Next     All lectures        Code

Macros in the C language

#include <stdio.h>

int x = 2;
int f (int z) { int temp = x; x += 2*z; return temp; }

/* side effects may cause error */
#define MIN(X,Y)  ((X) < (Y) ? (X) : (Y))
int MIN_fix(X,Y) { if (X < Y) return X; else return Y; }

int main () {
   unsigned char b = 0x3, c = 0x1;
   printf("result> %d, %d\n", MIN(b+c, f(3)), MIN_fix(b+c, f(3)));
 -  But this does not do the trick in the MIN macro to the left. The problem is that function f should be evaluated once but it is evaluated twice in the macro and the first time it is evaluated affects what is returned the second time.