Best C Marco Preprocessor Tutorial? (Book or Website)

It seems to me that the Macro Preprocessor can be so useful for avoiding writing duplicate code, yet to me, optimal and correct usage are not very obvious.

There are lots of tech books written about very small niches, like "Understanding and Using Pointers" (which I personally found a lot less useful than ASM Step by Step).

Yet I haven't found any books about "Mastering C Macros" or something like that.

Of course we can learn everything on our own via trial and error and digging through the infinite codebase that is Github... But I sure find a structured "tutorial"/"exercises" style introduction speeds me up a lot, especially for tools that I haven't used much.

Are there a few chapters of some of the more popular C books that focus on preprocessor macros?
Of course there are websites that I've seen, and if that's the way to go, it would be immensely helpful to have some recommendations for the best ones. The best thing about book versions is that you can read reviews to make sure the author really knows what they're talking about...

πŸ‘︎ 17
πŸ’¬︎
πŸ‘€︎ u/J_A_P_H
πŸ“…︎ Jan 15 2022
🚨︎ report
Let's write an optimizing Brainfuck interpreter using only the C Preprocessor github.com/camel-cdr/bfcp…
πŸ‘︎ 91
πŸ’¬︎
πŸ‘€︎ u/camel-cdr
πŸ“…︎ Dec 04 2021
🚨︎ report
Equivalent to C WORDSIZE et al preprocessor definitions?

Hi! I'm interested in making a D wrapper for GNU Lightning for a part of a hobby language implementation. The problem is I'm immediately hitting a wall in terms of how to translate lightning.h (available in GNU Lightning releases from here to D. It contains myriad preprocessor directives, most of which can be trivially translated to D, but some of which seems somewhat trickier. Take the below code (lines 35-58 in lightning.h):

#ifndef __WORDSIZE
#  if defined(WORDSIZE)				/* ppc darwin */
#    define __WORDSIZE		WORDSIZE
#  elif defined(__SIZEOF_POINTER__)		/* ppc aix */
#    define __WORDSIZE		(__SIZEOF_POINTER__ << 3)
#  elif defined(_ILP32)				/* hppa hp-ux */
#    define __WORDSIZE		32
#  elif defined(_LP64)				/* ia64 hp-ux (with cc +DD64) */
#    define __WORDSIZE		64
#  elif defined(_MIPS_SZPTR)			/* mips irix */
#    if _MIPS_SZPTR == 32
#      define __WORDSIZE	32
#    else
#      define __WORDSIZE	64
#    endif
#  else						/* From FreeBSD 9.1 stdint.h */
#    if defined(UINTPTR_MAX) && defined(UINT64_MAX) && \
	(UINTPTR_MAX == UINT64_MAX)
#      define __WORDSIZE	64
#    else
#      define __WORDSIZE	32
#    endif
#  endif
#endif

I guess the problem might just be that I don't really... get what this code is doing. I know that the builtin D version specifications can probably help me, but would my naive approach of just making a long list of all the builtin versions that would specify 64-bit or 32-bit and assigning __WORDSIZE appropriately really get the job done? Although the above code is still just an elif chain, it seems somewhat more complex than the approach I was contemplating.

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/humbleSolipsist
πŸ“…︎ Jan 04 2022
🚨︎ report
I created a C preprocessor in Python

While doing some C source generation for another project, I looked for some ways to do C preprocessing and analysis of defined macros in python. I couldn't find any nice ways to do what I wanted, so I ended up making my own.

This ingests source or header files and collects definitions for inspection or expansion.
It also emits the preprocessed source.

Take a look here

There are definitely some edge cases that I haven't handled, but its pretty reliable and works on realistic code bases.

πŸ‘︎ 16
πŸ’¬︎
πŸ‘€︎ u/Lambodragon
πŸ“…︎ Nov 23 2021
🚨︎ report
Who needs C++? C preprocessor meta programming is the future. github.com/camel-cdr/bfcp…
πŸ‘︎ 113
πŸ’¬︎
πŸ‘€︎ u/camel-cdr
πŸ“…︎ Dec 05 2021
🚨︎ report
Why so some C++ libraries define the namespace as a preprocessor macro?

E.g.

#define RAPIDJSON_NAMESPACE rapidjson
#define RAPIDJSON_NAMESPACE_BEGIN namespace rapidjson {

Like obviously this makes it easy to change the namespace by only touching one place, but I don't see why anyone would want to change that in the first place.

πŸ‘︎ 13
πŸ’¬︎
πŸ‘€︎ u/G01denW01f11
πŸ“…︎ Oct 27 2021
🚨︎ report
I wrote an optimizing brainfuck interpreter using only the C Preprocessor, here is how github.com/camel-cdr/bfcp…
πŸ‘︎ 13
πŸ’¬︎
πŸ‘€︎ u/camel-cdr
πŸ“…︎ Dec 11 2021
🚨︎ report
Simple C Preprocessor Failure

I'm trying to determine the number of flash blocks needed for reserving some flash space. I never get the "extra" +1 block. Is % not a valid operator? I don't get an error but the #else line never gets used. Or maybe it's something dumb.

#define QUOTIENT ((NUM_ITEMS*ITEM_SIZE)/FLASH_PAGE_SIZE)
#define REMAINDER  ((NUM_ITEMS*ITEM_SIZE)%FLASH_PAGE_SIZE)
#if REMAINDER == 0
  #define  FLASH_BLOCKS QUOTIENT
#else
  #define  FLASH_BLOCKS (QUOTIENT+1)
#endif
πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/mtechgroup
πŸ“…︎ Oct 19 2021
🚨︎ report
A Ξ»-calculus interpreter written in C preprocessor macros github.com/Hirrolot/metal…
πŸ‘︎ 72
πŸ’¬︎
πŸ‘€︎ u/jackasstacular
πŸ“…︎ Oct 17 2021
🚨︎ report
What’s the Point of the C Preprocessor, Actually? hirrolot.github.io/posts/…
πŸ‘︎ 44
πŸ’¬︎
πŸ‘€︎ u/Hirrolot
πŸ“…︎ Aug 02 2021
🚨︎ report
What’s the Point of the C Preprocessor, Actually? hirrolot.github.io/posts/…
πŸ‘︎ 9
πŸ’¬︎
πŸ‘€︎ u/Hirrolot
πŸ“…︎ Aug 02 2021
🚨︎ report
I created a Ξ»-calculus interpreter written entirely in C preprocessor macros github.com/Hirrolot/metal…
πŸ‘︎ 104
πŸ’¬︎
πŸ‘€︎ u/Hirrolot
πŸ“…︎ Jun 05 2021
🚨︎ report
What’s the Point of the C Preprocessor, Actually? hirrolot.github.io/posts/…
πŸ‘︎ 16
πŸ’¬︎
πŸ‘€︎ u/Hirrolot
πŸ“…︎ Aug 02 2021
🚨︎ report
get the preprocessed output of the c code without using preprocessor extension in c
  1. // C program to illustrate macros
  2. #include <stdio.h>
  3. // Macro definition
  4. #define AREA(l, b) (l * b)
  5. // Driver Code
  6. int main()
  7. {
  8. `// Given lengths l1 and l2`
    
  9. `int l1 = 10, l2 = 5, area;`
    
  10. `// Find the area using macros`
    
  11. `area = AREA(l1, l2);`
    
  12. `// Print the area`
    
  13. `printf("Area of rectangle"`
    
  14. 	`" is: %d",`
    
  15. 	`area);`
    
  16. `return 0;`
    
  17. }

this is the macro program, i want preprocessed output of the program without using preprocessor extension,

we can exclude library the header files.

can anybody please help me with this how would i do so,

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/Cprogrammingblogs
πŸ“…︎ Sep 07 2021
🚨︎ report
Metalang99: Full-blown preprocessor metaprogramming for pure C github.com/Hirrolot/metal…
πŸ‘︎ 95
πŸ’¬︎
πŸ‘€︎ u/Hirrolot
πŸ“…︎ Mar 27 2021
🚨︎ report
Metalang99: Full-blown preprocessor metaprogramming for C/C++ github.com/Hirrolot/metal…
πŸ‘︎ 33
πŸ’¬︎
πŸ‘€︎ u/Hirrolot
πŸ“…︎ May 08 2021
🚨︎ report
I want to see all the intermediate files (like one made by the preprocessor, assembler, etc) while compiling a C++ program. How to do that?

I want to see all the intermediate files (like one made by the preprocessor, assembler, etc) while compiling a C++ program. How to do that? Are there compiler flags to do that?

πŸ‘︎ 2
πŸ’¬︎
πŸ“…︎ Apr 25 2021
🚨︎ report
The C++ preprocessor doesn't understand anything about C++, and certainly not templates devblogs.microsoft.com/ol…
πŸ‘︎ 199
πŸ’¬︎
πŸ‘€︎ u/nikbackm
πŸ“…︎ May 09 2020
🚨︎ report
Hirrolot/awesome-c-preprocessor: A list of awesome C preprocessor stuff github.com/Hirrolot/aweso…
πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/Hirrolot
πŸ“…︎ May 13 2021
🚨︎ report
"Sometimes, while programming in C, I start imagining ways to implement classes using the C Preprocessor." reddit.com/r/ProgrammerHu…
πŸ‘︎ 97
πŸ’¬︎
πŸ‘€︎ u/xigoi
πŸ“…︎ Oct 13 2020
🚨︎ report
I created a brainfuck interpreter written entirely in C preprocessor directives github.com/Ferdi265/prepr…
πŸ‘︎ 2k
πŸ’¬︎
πŸ‘€︎ u/Ferdi265
πŸ“…︎ Jan 25 2020
🚨︎ report
Hirrolot/awesome-c-preprocessor: A list of awesome C preprocessor stuff github.com/Hirrolot/aweso…
πŸ‘︎ 11
πŸ’¬︎
πŸ‘€︎ u/Hirrolot
πŸ“…︎ May 13 2021
🚨︎ report
Metalang99: Full-blown preprocessor metaprogramming for pure C github.com/Hirrolot/metal…
πŸ‘︎ 75
πŸ’¬︎
πŸ‘€︎ u/Hirrolot
πŸ“…︎ Mar 27 2021
🚨︎ report
Metalang99: Full-blown preprocessor metaprogramming for pure C github.com/Hirrolot/metal…
πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/Hirrolot
πŸ“…︎ Mar 27 2021
🚨︎ report
Need preprocessor magic to automate my way of adding some kind of reflection to C structures

In order to help me write a lot of code needed to access MySql or to simplify import & export boilerplate or when I need xml, csv, json, etc. representations of my data, I have created structures that hold additional descriptions for internal structures.

One is RecFieldOffset. There I keep information about the type and position of a variable inside of a classic C structures together with its name so I can create an SQL WHERE clause, SQL bindings or whatever. Structures can even contain other structures but for the sake of my post here, I'll skip that part.

typedef struct  _RecFieldOffset  {
   short  fCType;
   char  *fCName;
   short  fCCount;
   short  fCLength;  // for [x][y]
   short  fCFlags;   // flags like skip, convert encoding,...
   long   fCOffset;  // Calculate at init

}  RecFieldOffset;

So for every struct I need to first create a typedef for that struct and then its description and yes, sometimes when I change the first part I forget the second step. This of course is bad and I wonder if I can somehow automate those two parts into one with some preprocessor macros.

Here's one example. A struct CustomerRecord and its description.

typedef struct  {      // Original C struct
   long      k_cust_cd;
   char      k_customer[42];
   char      k_adrdess[4][48];
   ...
   char      k_filler[190];
} CustomerRecord;

RecFieldOffset  rfoCustomerRecord[] =  {     // Description
   { kTlong, "k_cust_cd", 0 },
   { kTchar, "k_customer", 42,  },
   { kTchar, "k_adrdess", 4, 48,  },
   ...
   { kTchar, "k_filler", 190, 0, kTFlagSkip },
   { 0, NULL, 0  }
};

What I need would be something like this in a header file that is included twice, but with different settings so it produces two different constructs for the compiler.

MY_START CustomerRecord

MY_LONG  k_cust_cd, 0, 0, 0
MY_CHAR  k_customer, 42, 0, 0
MY_CHAR. k_address, 4, 48, 0
...
MY_CHAR. k_filler, 190, 0, MY_SKIP

MY_END CustomerRecord

Any ideas? At least a reference to something similar or a good preprocessor tutorial that deals with more complex stuff.

πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/idelovski
πŸ“…︎ Jan 21 2021
🚨︎ report
Announcing full support for a C/C++ conformant preprocessor in MSVC | C++ Team Blog devblogs.microsoft.com/cp…
πŸ‘︎ 120
πŸ’¬︎
πŸ‘€︎ u/MaximRouiller
πŸ“…︎ Mar 27 2020
🚨︎ report
Worst features of C, ignoring the standard library and the preprocessor?
πŸ‘︎ 8
πŸ’¬︎
πŸ‘€︎ u/oj002
πŸ“…︎ Oct 06 2020
🚨︎ report
Why does everyone say that the C preprocessor sucks?

I broadly don't really use the preprocessor much beyond include guards and basic replacement marcos, but I keep hearing that the preprocessor sucks and never explain why. The best I've heard is in the plan 9 papers where they say that #if "greatly complicates the preprocessor, is never necessary, and is usually abused."

πŸ‘︎ 17
πŸ’¬︎
πŸ‘€︎ u/coat_immediate
πŸ“…︎ Jul 09 2020
🚨︎ report
I wrote a C preprocessor in python, it's 25 times slower than GCC. github.com/cheery/pytci
πŸ‘︎ 1k
πŸ’¬︎
πŸ‘€︎ u/htuhola
πŸ“…︎ Oct 18 2015
🚨︎ report
[Lots of shit about the C preprocessor...] And did I mention that generics have the very same problem? old.reddit.com/r/golang/c…
πŸ‘︎ 71
πŸ’¬︎
πŸ‘€︎ u/cmov
πŸ“…︎ Jul 13 2019
🚨︎ report
Announcing full support for a C/C++ conformant preprocessor in MSVC | C++ Team Blog devblogs.microsoft.com/cp…
πŸ‘︎ 8
πŸ’¬︎
πŸ‘€︎ u/ThomasMaurerCH
πŸ“…︎ Mar 27 2020
🚨︎ report
C++/CUDA any reason to run function in header file and run via preprocessor directive?

Sort of a weird question, but I'm parsing through some c++/cuda code someone else has written and they call a function via #include "theFunction.h" in the middle of their main, where theFunction header file is for all intents and purposes just runs the (cuda) function, e.g.

runFunction<<<BkGd, ThBk>>>(gFval, gIAval);
cudaDeviceSynchronize();

Is there any sensible reason I don't know about to do this via preprocessor directive or is the author just being unconventional here?

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/still_tyler
πŸ“…︎ Jul 02 2020
🚨︎ report
C struct serialization using preprocessor macros natecraun.net/articles/st…
πŸ‘︎ 10
πŸ’¬︎
πŸ‘€︎ u/jonarne
πŸ“…︎ Sep 06 2019
🚨︎ report
"Every programmer should know C, because it's a simple and beautiful language that teaches a lot about how programs and computers actually work." "I don't know about beautiful. The preprocessor is a straight-up ugly hack, and the way types are declared is fairly insane." reddit.com/r/programming/…
πŸ‘︎ 25
πŸ’¬︎
πŸ‘€︎ u/LAUAR
πŸ“…︎ Apr 21 2019
🚨︎ report
weird issue with fortran code using the c preprocessor

Hi, I have come across this weird fortran F90 file that contains sth like this:

#include "magic.h"
TYPE, BIND(C) :: foo(bar)

and inside magic.h there is:

#define foo(arg) foo_##arg

so the above line after the preprocessor is run should become

TYPE, BIND(C) :: foo_bar

if I use cpp -P to process the file this is exactly what I get but if I invoke the preprocessor via the gfortran interface I get this:

TYPE, BIND(C) :: foo_##bar

which is almost what I expected as the ## are mysteriously not removed. Any ideas are more than welcomed as this is driving me crazy!

πŸ‘︎ 7
πŸ’¬︎
πŸ‘€︎ u/pattakosn
πŸ“…︎ Dec 06 2019
🚨︎ report
We use the preprocessor - a tech implemented by a drunk bell labs intern on LSD sometime in the late 60s, early 70s - says unknown C++ wanker looking for attention cor3ntin.github.io/posts/…
πŸ‘︎ 29
πŸ’¬︎
πŸ“…︎ Oct 29 2018
🚨︎ report
A more powerful macro preprocessor for C/C++ github.com/blackhole89/ma…
πŸ‘︎ 48
πŸ’¬︎
πŸ‘€︎ u/disposableoranges
πŸ“…︎ May 09 2018
🚨︎ report
The year is 2017 - Is the preprocessor still needed in C++? foonathan.net/blog/2017/0…
πŸ‘︎ 71
πŸ’¬︎
πŸ‘€︎ u/vormestrand
πŸ“…︎ May 08 2017
🚨︎ report

Please note that this site uses cookies to personalise content and adverts, to provide social media features, and to analyse web traffic. Click here for more information.