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.

DeepSeek search and fetch – $0.75 per 1000 successful requests

https://api-docs.deepseek.com/guides/search/
189•fagnerbrack•3h ago•89 comments

Show HN: Sphinx – A hook that quizzes you on your AI's code before shipping

https://sphinx-six-theta.vercel.app/
130•critiquebrowser•5h ago•33 comments

How the Elizabeth Holmes documentary “You Can See Everything” is deceiving

https://www.readtheprofile.com/p/elizabeth-holmes-documentary-you-can-see-everything
140•MaysonL•6h ago•143 comments

Bleached corals thrive within days when touching healthy neighbour

https://www.nature.com/articles/d41586-026-03266-x
458•alkyon•1d ago•99 comments

Lean now ships with verified kernels

https://leodemoura.github.io/blog/2026-10-15-lean-now-ships-with-verified-kernels/
137•matt_d•2d ago•47 comments

Teal – A statically-typed dialect of Lua

https://teal-language.org/
27•Lyngbakr•1d ago•6 comments

Go Piss, Girl: The plight of the public potty

https://thebaffler.com/latest/go-piss-girl-wagner
17•prismatic•1d ago•22 comments

Dam removals are bringing fish back across the northeastern United States

https://www.smithsonianmag.com/science-nature/dam-removals-are-bringing-fish-back-across-the-nort...
277•sohkamyung•1d ago•66 comments

DeepSeek-v4.1-Flash-1017

https://huggingface.co/deepseek-ai/DeepSeek-V4.1-Flash-1017
860•theykk•1d ago•655 comments

Pele – Mermaid diagrams as themeable SVG

https://pele.run/
439•dchest•18h ago•39 comments

Furifont, a Japanese font that shows furigana

https://furifont.antfu.me/
348•plaguna•3d ago•70 comments

Fun Extending the C Preprocessor

https://www.davidpriver.com/Fun-Extending-the-C-Preprocessor.html
27•david2ndaccount•2d ago•4 comments

Making chicken transparent using Dorito dust

https://chillphysicsenjoyer.substack.com/p/making-chicken-transparent-using
304•crescit_eundo•20h ago•81 comments

Southern ground hornbill gets 3D printed beak prostheses at Artis Zoo

https://nltimes.nl/2026/10/15/world-first-southern-ground-hornbill-gets-3d-printed-beak-prosthese...
31•jacquesm•2d ago•3 comments

The Commodore 64 hardware simulator: break chips and pins, see the results

https://github.com/antirez/c64-hw-simulator
189•antirez•3d ago•28 comments

Interactive Linear Programming in the Browser

https://lpviz.net/
34•klamike•22h ago•0 comments

Parser Combinators

https://t3.at/blog/2026-10-17-parser-combinators/
4•schindlabua•1h ago•0 comments

NY announces game ownership bill requiring ownership rather than licensing

https://www.tomshardware.com/video-games/new-york-rep-announces-game-ownership-bill-that-would-re...
292•guiambros•9h ago•105 comments

You could have invented transformers from memory

https://alexwang.ai/posts/associative-memory/
565•speckx•1d ago•127 comments

Matthew Prince on why Cloudflare offers a free version and always will (2016)

https://webmasters.stackexchange.com/questions/88659/how-can-cloudflare-offer-a-free-cdn-with-unl...
96•thatslast•5h ago•75 comments

Custom server to revive neato bots

https://github.com/vacuula/vacuula
47•matttah•3d ago•1 comments

Gold is now the top U.S. export

https://www.numismaticnews.net/gold-now-top-u-s-export
333•iamnothere•17h ago•182 comments

Show HN: High-quality duck audio synthesizer

https://celphase.github.io/duck-pond/
140•celphase•20h ago•30 comments

Codex Desktop continuously generates Crashpad pending dumps

https://github.com/openai/codex/issues/25921
133•AIorNot•16h ago•53 comments

Notes on the Complex Roots of Unity

https://eli.thegreenplace.net/2026/notes-on-the-complex-roots-of-unity/
26•sks_15•5h ago•3 comments

A Mole Infiltrated the Highest Ranks of American Militias (2025)

https://www.propublica.org/article/ap3-oath-keepers-militia-mole
91•georgecmu•5h ago•18 comments

Superoptimizing an FPU Emulator

https://purplesyringa.moe/blog/superoptimizing-an-fpu-emulator/
26•dverlaeckt80•1d ago•2 comments

Show HN: Llmspeed – feel how fast a local LLM runs before you buy the hardware

https://llmspeed.dev/
294•albinstman•1d ago•82 comments

Show HN: Esp32sim – Run ESP32 firmware in the browser

https://github.com/mikroverk/esp32sim
167•joakimeriksson•20h ago•32 comments

Solar surpasses nuclear power globally for first time

https://www.pv-magazine.com/2026/09/29/solar-surpasses-nuclear-power-globally-for-first-time/
612•xbmcuser•20h ago•380 comments