Useful C #macros
Visit macros »
This is a collection of commonly used C Macros I found on internet, and as suggested by my friends.
Using C Macros, we can extend C with a lot of useful functionality. For example, we can create loops like foreach which are very useful and handy when it comes to arrays. These are available in most of high level languages but not in C. But we got macros in C and so we can add on our own!
Just download macros.h from here and include it to the top.
#include "macros.h"
Note that there is an ASSET
macro useful for debugging purpose. To enable it, you need to #define DEBUG
before including macros.h
.
For docs, visit ramdeoshubham.com/macros to learn more about the available macros and their usage.
The source is very clear and the macros available are too common you will find them everywhere. 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.
- All macros are in UPPERCASE.
- In case of any Boolean Macro, the
IS_ prefix
should be used. For example,IS_EVEN(x)
. And they should return a1
or0
value. - 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.
- 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 intoPASTE2(a,b)
which further expands intoa##b
.
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 programmers. I can only take credit for the efforts I made to collect and bringing it to you.
Copyright 2017, Shubham Ramdeo. Code released under the MIT License.
#Handpicked C #Macros with ♥ #love !
Don't forget to ★STAR
if you like it.