Hand picked C Macros!

This is a collection of commonly used C Macros I found on internet, and as suggested by my friends.

How to use

Just download macros.h from here and include it to the top.

#include "macros.h"

Note that there is an ASSERT macro useful for debugging purpose. To enable it, you need to #define DEBUG before including macros.h.

Available macros

Boolean

We get TRUE as 1 and FALSE as 0.

  while (TRUE) {
    if( (x > 5) == FALSE ) break;
    x++; printf("hi ");
  }

Operators

These can be used to perform logical operations more conveniently. Specially, == is EQUALS and = is IS. So the age old confusion gets clear here.

x = 5;
if( x EQUALS 0 OR x > 10 AND x NOTEQUALS 15)
{
  x IS 1;
}
else if ( NOT (x > 0) ) {
  x IS 0;
}
else x IS 100;
printf("\n%d\n",x);

Loops

A simple infinite loop. It needs to be break from the scope to stop it.

FOREVER{
  if( x > 5 ) break;
  x++;printf("hi ");
}

A simple from - to ranged loop.

int i;
RANGE (i, 10, 20){
  printf("%d ", i);
}

/* OUTPUT:
   10 11 12 13 14 15 16 17 18 19 20 */

It works in both direction way without any modifications required. It will decide on its on when to decrement or increment. Its smart isn’t it?

int i;
RANGE (i, 5, -5){
  printf("%d ", i);
}

/* OUTPUT:
   5 4 3 2 1 0 -1 -2 -3 -4 -5 */

A C implementation of foreach loop, it will go through each element of an array, and will perform operations on each element as returned into pointer item variable.

STRING A[] = {"apple", "banana", "orange", "grapes"};
FOREACH (STRING *i, A){
  printf("I love %s\n", *i);
}

/* OUTPUT:
   I love apple
   I love banana
   I love orange
   I love grapes */

See also STRING in strings section.

Obviously, for single line statements, {} are not required.

Math

There are a lot of macros for mathematical purposes.

These can be used to convert angles, where PI is a constant.

printf("%d ", RAD2DEG(PI) );

/* OUTPUT:
   180 */

Functioning of above Macros are quite obvious. (TL;D write!)

For example, ISNAN (0.0/0.0), It Checks if x is NOT A NUMBER.

These two are interesting. Also if you look at their #defines, they don’t have any temporary variable.

int x = 10, y = 5, z = 0;
SORT(x,y); /*now x should be smaller one*/
SWAP(x,z); /*now z and x swaps */
printf("%d", z);

/* OUTPUT:
   5 */

Returns 1 on positive and -1 on negative.

For example:

int x;
printf("Enter a num: ");
scanf("%d", &x);
if ( IS_EVEN (x) && IS_BETWEEN(x, 0, 20) ){
  printf("working... \n");
}

/* OUTPUT:
   Enter a num: 12
   working...*/

Bits

People use constant 1024 many times, now you can get it simply by BIT(10).

Arrays

int arr[] = {1,2,3,4,5,6,7,8,9,10};
if(IS_ARRAY(arr)) {
  SET(arr, ARRAY_SIZE(arr)-4, 10);
  ZERO(arr, 3);
}
RANGE(x, 0, 9) printf("%d ", arr[x]);

/* OUTPUT:
0 0 0 10 10 10 7 8 9 10 */

Strings

Its translates to char*.

STRING x = "hello";
STRING list[] = {"book", "ball", "bucket"};

Concatenation of two pure strings.

 puts ( CAT ("PenPineapple", "ApplePen") );

 /* OUTPUT:
    PenPineappleApplePen */

Expands s as a string.

puts ( STR (This is a fruit) );

/* OUTPUT:
   This is a fruit */

Tokens

a = 10;
b = 20;
ab = 0;
x = PASTE(a,b);
PRINT_TOKEN( x );

/* OUTPUT:
   x is 0 */

Debugging

ASSERT Macro will require DEBUG to be defined. Also on error, it will return -1 to exit. TRY performs x. Only on error, it shows custom error message. LOG is reverse, it produces message on success of x.

#define DEBUG /* To use ASSERT */
#include "macro.h"
...
  LOG(x=5,"Now square of x is %d", x*x);
  TRY(0>10, "We already knew its an error\n");
  ASSERT(1>2);

/* OUTPUT:
   test.c:125: Now square of x is 25
   test.c:126:We already knew its an error
   1>2 - Failed On Aug  7 2017 At 22:36:23 In File test.c At Line 12

Saturation

A saturating macro which does v++ and w– respectively on every call, and does nothing on reaching L limit.

a = 0, b = 7;
for(x=0; x<10; x++){
  SATINC(a,7);
  SATDEC(b,0);
  printf("%d %d, ",a,b);
}

/* OUTPUT:
   1 6, 2 5, 3 4, 4 3, 5 2, 6 1, 7 0, 7 0, 7 0, 7 0, */

Statements

Macros involving multiple statements needs braces which are safer to define with a do_while(0) for single execution. STMT expands statements the same way.

STMT (printf("it should be in a macro...\n"););

This Macro make it sure that the statements within it are performed only once.

a = 1;
RANGE(x,0,10) {
/*ONCE(*/
  a++;
/*);*/
}
printf("%d\n", a);

/* With ONCE */
a = 1;
RANGE(x,0,10) {
ONCE(
  a++;
);
}
printf("%d\n", a);

/* OUTPUT:
   12
   2 */

See also FOREVER in Loops section.

Extern C

These macros are for C++ users. To compile a piece of C code in C++ environment, you can use these macros as follows:

//somecppfile.cpp

// CPP
EXTERN_C_START

    /* C */

    #include <stdio.h>
    int main(void)
    {
      int new = 10;
      printf("%d", new);
    }

EXTERN_C_END
// CPP

/* OUTPUT:
   10 */

As ‘new’ is a C++ keyword, this would never be compiled with a C++ compiler, but its valid in C. So we used those macros to mix C in C++.

Contribute

Macros are usually insulted for their dirty behavior. But we can see that some features of C language cannot be used to their max without macros. A good Macro is : Portable, safe, involves as less arbitrary variables as possible. Although I am trying my best, there might be some exception errors, please correct if you find any. Also, if I missed some other interesting macros, please contribute to make this collection complete and more usable.

The following conventions were followed:

  1. All macros are in UPPERCASE.
  2. In case of any Boolean Macro, the IS_ prefix should be used. For example, IS_EVEN(x). And they should return a 1 or 0 value.
  3. Macros for loops and conditionals should not end with braces so that the user can have the option for single line as well as block style.
  4. A helper macro, which are expanded by some other main macro, usually they are not intended to be used directly. So they should be mentioned with their expansion level as prefix. For example, PASTE(a,b) expands into PASTE2(a,b) which further expands into a##b.

Caught a mistake or want to contribute to this documentation? You can edit this page on Github

Credit

The real credits goes to all those great C coders who came up with such creative use of C Macros to extend C. I made this collection in honor of The C language and those unknown coders. I can only take credit for the efforts I made to collect and bringing it to you. A special thanks to Mohit Khatri for designing looks of this site that make this topic look so interesting here.

Don’t forget to ★ STAR this project if you like it.

with
© Shubham Ramdeo
Sitemap