Define a macro swap(t,x,y) that interchanges two arguments of type t. - C Preprocessor

C examples for Preprocessor:Macro

Description

Define a macro swap(t,x,y) that interchanges two arguments of type t.

Demo Code

#include <stdio.h>
#include <string.h>

#define swap(t, x, y) {t tmp; tmp = x; x = y; y = tmp;}

int main(void)
{
    int ia = 20, ib = 35;
    printf("before swap: %d, %d\n", ia, ib);
    swap(int, ia, ib)
    printf(" after swap: %d, %d\n", ia, ib);

    double da = 0.1234, db = 9876.34;
    printf("before swap: %6g, %6g\n", da, db);
    swap(double, da, db)
    printf(" after swap: %6g, %6g\n", da, db);

    return 0;/*from   ww w . ja  va  2  s . co  m*/
}

Result


Related Tutorials