UIU: Learn C by Examples : C Preprocessor


Preprocessor are processed before compilation.


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

// simple text substitution 
#define SIZE 10

int main (int argc, char *argv[])
{
    int a [SIZE];

    printf ("%d\n", SIZE);

    switch (10) {
        case SIZE: 
            printf ("TEN\n");
            break;
    }

    return 0;
}

// @END_OF_SOURCE_CODE

const int SIZE 10; works same
#define has no type, it’s just text substitution, doesn’t have type


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

#define TEXT "printf"

int main (int argc, char *argv[])
{

    TEXT ("%d\n", 10);


    return 0;
}

// @END_OF_SOURCE_CODE

const can go before or after

#include <iostream>
#include <sstream>
#include <vector>

#define FORMAT "%d %s\n"
#define PRINT printf("Hello world\n");

using namespace std;


int main( int argc, char ** argv ) {


    printf (FORMAT, 10, "UIU");

    PRINT

    printf ("this ");

    return 0;
}
// @BEGIN_OF_SOURCE_CODE

#include <stdio.h>

#define release

#ifdef debug
#define SIZE 10
#else
#define SIZE 100000
#endif


int main(int argc, const char ** argv)
{
    int a [SIZE];

    printf ("%d\n", SIZE);


    return 0;
}

// @END_OF_SOURCE_CODE

debugger are really smart now, still it would be a nice feature to have

// @BEGIN_OF_SOURCE_CODE

#include <stdio.h>

#define debug

#ifdef debug
// windows people should write only out.txt 
#define FILE_OUT freopen("/Users/shahabuddin/out.txt", "w", stdout);
#else 
#define FILE_OUT
#endif


int main(int argc, const char ** argv)
{
    FILE_OUT

    int x;

    scanf ("%d", &x);

    printf ("%d %d\n", x, x);


    return 0;
}

// @END_OF_SOURCE_CODE
// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

#define MULTIPLY(a, b) (a) * (b)

int main (int argc, char *argv[])
{
    printf ("%d\n", MULTIPLY(2, 3 + 1));

    return 0;
}

// @END_OF_SOURCE_CODE

be careful to use macro

// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

#define MULTIPLY(a, b) (a) * (b) * (b)

int M = 1;

int inc() {
    M++;
    return M;
}

int main (int argc, char *argv[])
{
    printf ("%d\n", MULTIPLY(2, inc()));

    return 0;
}

// @END_OF_SOURCE_CODE

Problem 1


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

#define Sum(a, b) a + b

int main (int argc, char *argv[])
{
    printf ("%d\n", Sum(5, 6));

    return 0;
}

// @END_OF_SOURCE_CODE

Problem 2


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

#define Swap(a, b, c) c t; t = a, a = b, b = t

int main (int argc, char *argv[])
{
    int x = 10, y = 20;

    Swap(x, y, int);

    printf ("%d %d\n", x, y);

    return 0;
}

// @END_OF_SOURCE_CODE

Problem 3


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

#define Swap(a, b) typeof(a) t; t = a, a = b, b = t

int main (int argc, char *argv[])
{
    int x = 10, y = 20;

    Swap(x, y);

    printf ("%d %d\n", x, y);

    return 0;
}

// @END_OF_SOURCE_CODE

Problem 4


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

#define MIN(a, b) a < b ? a : b
#define MAX(a, b) a > b ? a : b

int main (int argc, char *argv[])
{
    printf("%d\n", MIN(5, 6));

    printf("%d\n", MAX(5, 6));

    return 0;
}

// @END_OF_SOURCE_CODE

Problem 5


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

#define LL long long

inline LL Power(int b, int p) {
    LL ret = 1; for ( int i = 1; i <= p; i++ ) ret *= b;
    return ret;
}

int main (int argc, char *argv[])
{
    printf ("%lld\n", Power(2, 3));

    return 0;
}

// @END_OF_SOURCE_CODE

Problem 6


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

#define F(n) for (int i = 0; i < n; i++ )

int main (int argc, char *argv[])
{
    F(5) {
        printf ("Hello World!\n");
    }

    return 0;
}

// @END_OF_SOURCE_CODE

Problem 7


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

#define F(i, a, b) for ( int i = a; i < b; i++ )

int main (int argc, char *argv[])
{
    F(i, 5, 10) {
        printf ("%d\n", i);
    }

    return 0;
}

// @END_OF_SOURCE_CODE

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s