How to use ifndef to ensure macros are not defined multiple times in C

2 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>

#ifndef COUNT
#define COUNT 10
#endif

int main(void) {

    for (int i = 0; i < COUNT; i++) {
        printf("%2d", i);
    }

    return 0;
}




/*
run:

 0 1 2 3 4 5 6 7 8 9

*/

 



answered May 6, 2021 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>

#define COUNT 5

#ifndef COUNT
#define COUNT 10
#endif


int main(void) {

    for (int i = 0; i < COUNT; i++) {
        printf("%2d", i);
    }

    return 0;
}




/*
run:

 0 1 2 3 4

*/

 



answered May 6, 2021 by avibootz

Related questions

...