DEV Community

Lam
Lam

Posted on

3 3

C Preprocessor cheatsheet

The C preprocessor is the macro preprocessor for the C, Objective-C, and C++ computer programming languages. The preprocessor provides the ability for the inclusion of header files, macro expansions, conditional compilation, and line control. C Preprocessor Cheat Sheet is quick reference for the C macro preprocessor, which can be used independently of C/C++.

File and line

#define LOG(msg) console.log(__FILE__, __LINE__, msg)
#=> console.log("file.txt", 3, "hey")

Enter fullscreen mode Exit fullscreen mode

Stringification

#define STR(name) #name
char * a = STR(object);   #=> char * a = "object";

Enter fullscreen mode Exit fullscreen mode

Token concat

#define DST(name) name##_s name##_t
DST(object);   #=> object_s object_t;

Enter fullscreen mode Exit fullscreen mode

Macro

#define DEG(x) ((x) * 57.29)

Enter fullscreen mode Exit fullscreen mode

Error

#if VERSION == 2.0
  #error Unsupported
  #warning Not really supported
#endif

Enter fullscreen mode Exit fullscreen mode

[Reference] If

#ifdef DEBUG
  console.log('hi');
#elif defined VERBOSE
  ...
#else
  ...
#endif

Enter fullscreen mode Exit fullscreen mode

Defines

#define FOO
#define FOO "hello"

#undef FOO

Enter fullscreen mode Exit fullscreen mode

Includes

#include "file"

Enter fullscreen mode Exit fullscreen mode

Compiling

$ cpp -P file > outfile

Enter fullscreen mode Exit fullscreen mode

Reference

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay