frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

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.

Project Gutenberg – keeps getting better

https://www.gutenberg.org/
943•JSeiko•17h ago•199 comments

Frontier AI has broken the open CTF format

https://kabir.au/blog/the-ctf-scene-is-dead
135•frays•3h ago•114 comments

Ploopy Bean: a trackpoint for every computer

https://ploopy.co/shop/bean-pointing-stick/
96•jibcage•3d ago•45 comments

I believe there are entire companies right now under AI psychosis

https://twitter.com/mitchellh/status/2055380239711457578
1369•reasonableklout•13h ago•674 comments

SQL patterns I use to catch transaction fraud

https://analytics.fixelsmith.com/posts/sql-fraud-patterns/
223•redbell•10h ago•72 comments

The bird eye was pushed to an evolutionary extreme

https://www.quantamagazine.org/how-the-bird-eye-was-pushed-to-an-evolutionary-extreme-20260513/
114•sohkamyung•2d ago•37 comments

EMiX: Emulating Beyond Single-FPGA Limits

https://arxiv.org/abs/2604.27012
12•PaulHoule•2d ago•1 comments

Additive Blending on the Nintendo 64

https://phoboslab.org/log/2026/05/n64-additive-blending
114•ibobev•19h ago•10 comments

Gaining control of every projector and camera on campus

https://www.edna.land/blogs/posts/scanning/
28•ednaordinary•2d ago•4 comments

The main thing about P2P meth is that there's so much of it (2021)

https://dynomight.net/p2p-meth/
123•tomjakubowski•10h ago•145 comments

A 0-click exploit chain for the Pixel 10

https://projectzero.google/2026/05/pixel-10-exploit.html
377•happyhardcore•20h ago•205 comments

Naturally Occurring Quasicrystals

https://johncarlosbaez.wordpress.com/2026/05/14/naturally-occurring-quasicrystals/
98•lukeplato•1d ago•9 comments

How to Write to SSDs [pdf]

https://www.vldb.org/pvldb/vol19/p1469-lee.pdf
112•matt_d•11h ago•13 comments

Charity – Categorical programming language (1998)

https://github.com/mietek/charity-lang/blob/master/doc/README.md
5•matteodelabre•3d ago•0 comments

The sigmoids won't save you

https://www.astralcodexten.com/p/the-sigmoids-wont-save-you
207•Tomte•23h ago•207 comments

England Runestones

https://en.wikipedia.org/wiki/England_runestones
47•cl3misch•3d ago•16 comments

I Bought a “Junk” PSP From Japan

https://gardinerbryant.com/i-bought-a-junk-psp-from-japan-heres-how-it-went/
62•Kate0CoolLibby•3d ago•28 comments

Research on mildew contamination affecting the sound quality of analog tapes

https://www.nature.com/articles/s40494-026-02592-7
21•crousto•1d ago•1 comments

Orthrus-Qwen3: up to 7.8×tokens/forward on Qwen3, identical output distribution

https://github.com/chiennv2000/orthrus
92•FranckDernoncou•11h ago•15 comments

Someone Shared a Real Monet Painting as AI and Asked for Critiques

https://petapixel.com/2026/05/14/someone-shared-a-real-monet-painting-as-ai-and-asked-for-critiques/
23•ZeljkoS•1h ago•18 comments

Bill to block publishers from killing online games advances in California

https://arstechnica.com/gaming/2026/05/bill-to-keep-online-games-playable-clears-key-hurdle-in-ca...
488•Lihh27•14h ago•305 comments

Show HN: Epiq – Distributed Git based issue tracker TUI

https://ljtn.github.io/epiq/
60•jolaflow•9h ago•23 comments

I designed a nibble-oriented CPU in Verilog to build a scientific calculator

https://github.com/gdevic/FPGA-Calculator
103•gdevic•16h ago•33 comments

ESP-EEG is an affordable 8-channel biosensing board

https://www.autodidacts.io/cerelog-esp-eeg-affordable-openbci-like-board/
50•surprisetalk•2d ago•14 comments

Image-blaster: Creates 3D environments, SFX, and meshes from a single image

https://github.com/neilsonnn/image-blaster
160•MattRogish•18h ago•31 comments

Show HN: Watch a neural net learn to play Snake

https://ppo.gradexp.xyz/
159•c1b•1d ago•37 comments

The Zulip Foundation

https://blog.zulip.com/2026/05/15/announcing-zulip-foundation/
273•boramalper•15h ago•71 comments

Erlang/OTP 29.0

https://www.erlang.org/news/188
209•pyinstallwoes•10h ago•38 comments

Bun Rust rewrite: "codebase fails basic miri checks, allows for UB in safe rust"

https://github.com/oven-sh/bun/issues/30719
412•ndiddy•17h ago•285 comments

ASCII by Jason Scott

https://ascii.textfiles.com/
194•bookofjoe•20h ago•22 comments