frontpage.
newsnewestaskshowjobs

Open Source @Github

fp.

Open in hackernews

Detecting if an expression is constant in C

https://nrk.neocities.org/articles/c-constexpr-macro#detecting-if-an-expression-is-constant-in-c
49•signa11•1y ago

Comments

wahern•1y ago
> This works. But both gcc and clang warn about the enum being anonymous... even though that's exactly what I wanted to do. And this cannot be silenced with #pragma since it's a macro, so the warning occurs at the location where the macro is invoked.

You can use _Pragma instead of #pragma. E.g.

  #define C(x) ( \
    _Pragma("clang diagnostic push") \
    _Pragma("clang diagnostic ignored \"-Wvisibility\"") \
    (x) + 0*sizeof(void (*)(enum { tmp = (int)(x) })) \
    _Pragma("clang diagnostic pop") \
  )
EDIT: Alas, GCC is a little pickier about where _Pragma is allowed so you may need to use a statement expression. Also, it seems GCC 14 doesn't have a -W switch that will disable the anonymous enum warning.
pjc50•1y ago
It's remarkable that people will say that doing this kind of thing is better than learning a language which actually lets you enforce this with the type system.

(or even just insist that users use the version of the language which supports "constexpr"!)

oguz-ismail•1y ago
What language is that? Is it available everywhere (everywhere) C is?
mitthrowaway2•1y ago
Indeed, usually if I'm using C these days it's because I only have access to a c compiler for my target platform, or because I'm modifying an existing C codebase.
uecker•1y ago
I do not think anybody said this. The point is that these macros work for early versions of C. If you need to support early versions of C, learning another language is not a solution. If you don't have to, you can use C23's constexpr.
trealira•1y ago
C used to seem like a beautiful and simple language to me, but as I used it and learned more about it, it seemed more complex under the surface, and kind of janky as well. It's just utilitarian.
wat10000•1y ago
Learning such a language doesn’t mean I can use it.
o11c•1y ago
The problem is that no such language exists.

There are many languages that provide one particular feature that C doesn't provide, but they do this at the cost of excluding numerous other features that C widely relies on.

kjs3•1y ago
"I have no idea what problem you're trying to solve, what the constraints are, what the use cases might be, what tools are available on the platform, what the job or regulations require, what the skillsets of the people involved are, what the timeline is...but I'm absolutely, unshakably certain that I have a magic bullet that will make all your problems go away."

FTFY.

sleirsgoevy•1y ago
The Linux kernel has even a way to determine whether the expression is compile-time, WITHOUT aborting compilation in either case.

The trick is this (copied vebratim from Linux):

#define __is_constexpr(x) (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))

Explanation: if x is a constant expression, then multiplying it by zero yields a constant 0, and casting a constant 0 to void* makes a null pointer constant. And the ternary expression, if one of its sides is a null pointer constant, collapses to the type of the other side (thus the type of the returned pointer will be int*, and the sizeof will match). And if x was not constant, then the lefthand side would not be considered a null pointer constant by type inference, the type of the ternary expression will be void*, and the sizeof check will not match.

With a few more clever tricks, it's even possible to implement a compile-time "type ternary expression", like this: TYPE_IF(2 * 2 == 4, int, long). This is left as an exercise for the reader.

amelius•1y ago
This reminds me of the days when Boost was a thing. It was full of tricks like this.
usrnm•1y ago
It still is a thing, though.
cperciva•1y ago
With a few more clever tricks...

I did this with my PARSENUM macro (https://github.com/Tarsnap/libcperciva/blob/master/util/pars...) to parse strings into floating-point, unsigned integer, or signed integer types (and check bounds) using a single interface.

bobbyi•1y ago
I thought this would work:

#define C(x) (sizeof(char[x]), x)

sizeof is a compile-time operation so x need to be known at compile time.

It didn't work as expected. It turns out there is an exception and the standard says that sizeof is actually calculated at runtime specifically for variable length arrays:

> The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

Introducing System One Models and Jev

https://typesafe.ai/blog/introducing-system-one-models-and-jev
873•albelfio•7h ago•279 comments

Show HN: An e-ink frame that hears birds and draws them as 1800s illustrations

https://github.com/arnegiacomo/fugleramme
1369•arnemunthekaas•14h ago•183 comments

Negativland, Culture Jamming, and the Art of Making Something New

https://blog.archive.org/2026/09/11/negativland-culture-jamming-and-the-art-of-making-something-new/
13•bananaboy•32m ago•4 comments

An update on Wayback Machine access

https://blog.archive.org/2026/09/15/an-update-on-wayback-machine-access/
420•ChrisArchitect•9h ago•222 comments

Gemini 3.8 Live and 3.8 Live Extended Thinking

https://blog.google/innovation-and-ai/models-and-research/gemini-models/gemini-3-8-live-gemini-3-...
330•leumon•9h ago•203 comments

German Rheinmetall open-sources its Battlesuite connected weapon system protcol

https://rheinmetall.github.io/onboardapi-documentation/9.10.0/index.html
147•summarity•6h ago•44 comments

Stay discoverable in search while disallowing AI training

https://blog.cloudflare.com/accountable-mixed-use-ai-crawlers/
23•djfergus•53m ago•12 comments

Recreating Voodoo Graphics and a Late-1990s Gaming PC on an FPGA

https://nand2mario.github.io/posts/2026/zsst-voodoo/
53•zdw•4h ago•10 comments

Building a Linux GPU Driver for the M4 Mac Mini in One Month

https://codyho.dev/blog/gpu-driver/
186•ADevWithAnIdea•7h ago•114 comments

We got admin access to Baseten's production GitHub in 25 minutes

https://www.strix.ai/blog/baseten-harbor-github-pat-takeover
240•bearsyankees•9h ago•130 comments

Saving Jet Fuel

https://tech.marksblogg.com/scikit-decide-openap-optimal-flight-planning.html
45•marklit•4h ago•19 comments

Apple Reference Image: A New Approach for Verified Photography

https://security.apple.com/blog/apple-reference-image/
16•imwally•1h ago•7 comments

Jean-Pierre Serre turns 100

https://mathshistory.st-andrews.ac.uk/Biographies/Serre/
103•jzox•6h ago•16 comments

Sierra digital cameras on the Apple II

https://www.colino.net/wordpress/archives/2026/09/11/sierra-digital-cameras-on-the-apple-ii/
26•ibobev•2d ago•4 comments

Chopping up books when they're physically too big

https://attainablefelicity.mattkirkland.com/20260915/cut-up-your-books.html
136•matt_kirkland•8h ago•131 comments

An interactive world map of the stories cultures have told

https://originmap.sunnyguha.com/
7•momentmaker•2d ago•0 comments

Learning to solve hard problems in RL for LLMs by never giving up

https://mnoukhov.github.io/posts/ngu/
49•natolambert•8h ago•0 comments

The Beauty of Roundabouts

https://gruhn.me/blog/2026-09-14/
25•ngruhn•1d ago•33 comments

Show HN: Capsule – Single-file web apps that save their data into SQLite

https://withcapsule.app/
295•bashtian•13h ago•120 comments

WangNet – 1.8 MB, zero-dependency Numberwang adjudication in 11 languages

https://github.com/GraafHenk/numberwang
119•Liogra123•7h ago•46 comments

Let's make quality the norm again

https://www.forbrukerradet.no/short-life/
334•ingve•17h ago•337 comments

A single firm is behind OpenAI, Anthropic, and Meta hacking scandals

https://www.effort.news/irregular
503•yusufozkan•1d ago•175 comments

Suspected sabotage causes major Netherlands rail disruption

https://www.bbc.com/news/articles/c8ly49w9g1edo
449•choult•16h ago•398 comments

Most people prefer traditional architecture

https://www.worksinprogress.news/p/do-people-prefer-traditional-architecture
278•alihm•1d ago•242 comments

Show HN: Pizza Bot – An inbox for AI agents that work in the background

https://github.com/pizza-bot-app/pizza-bot
35•jd_•11h ago•18 comments

Show HN: Hacking a $20 4G wireless hotspot into a texting device

https://bkovac.github.io/modem-thing/
181•bobili1234•13h ago•31 comments

Jiga (YC W21) Is Hiring Product Engineer (Remote/US)

https://jiga.io/about-us/?ashby_jid=0b75d72d-c92b-4dca-8062-09d298ada0bd
1•grmmph•10h ago

The madman's guide to stamp collecting

https://www.wsj.com/arts-culture/books/the-madmans-guide-to-stamp-collecting-review-sticking-to-w...
7•centrosphere•1d ago•0 comments

Data races and the limits of ThreadSanitizer in C and Go

https://theconsensus.dev/p/2026/09/06/data-races-and-the-limits-of-threadsanitizer-in-c-and-go.html
33•matt_d•2d ago•1 comments

The Inference Hardware Revolution of 2026

https://spectrum.ieee.org/inference-hardware-revolution
117•vinhnx•12h ago•12 comments