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•11mo ago

Comments

wahern•11mo 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•11mo 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•11mo ago
What language is that? Is it available everywhere (everywhere) C is?
mitthrowaway2•11mo 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•11mo 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•11mo 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•11mo ago
Learning such a language doesn’t mean I can use it.
o11c•11mo 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•11mo 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•11mo 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•11mo ago
This reminds me of the days when Boost was a thing. It was full of tricks like this.
usrnm•11mo ago
It still is a thing, though.
cperciva•11mo 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•11mo 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.

Vercel Says Internal Systems Hit in Breach

https://decipher.sc/2026/04/19/vercel-says-internal-systems-hit-in-breach/
238•whiteyford•2h ago•38 comments

Archive of Byte magazine, starting with issue #1 in 1975

https://archive.org/details/byte-magazine-1975-09
426•DamnInteresting•2d ago•108 comments

Show HN: Faceoff – A terminal UI for following NHL games

https://www.vincentgregoire.com/faceoff/
11•vcf•36m ago•2 comments

Nanopass Framework: Clean Compiler Creation Language

https://nanopass.org/
81•NordStreamYacht•4d ago•14 comments

Notion leaks email addresses of all editors of any public page

https://twitter.com/weezerOSINT/status/2045849358462222720
149•Tiberium•3h ago•37 comments

Game devs explain the tricks involved with letting you pause a game

https://kotaku.com/video-game-devs-explain-how-pausing-works-and-sometimes-it-gets-weird-2000686339
332•speckx•3d ago•183 comments

The seven programming ur-languages (2022)

https://madhadron.com/programming/seven_ur_languages.html
187•helloplanets•10h ago•73 comments

SPEAKE(a)R: Turn Speakers to Microphones for Fun and Profit [pdf] (2017)

https://www.usenix.org/system/files/conference/woot17/woot17-paper-guri.pdf
131•Eridanus2•9h ago•58 comments

Show HN: Shader Lab, like Photoshop but for shaders

https://eng.basement.studio/tools/shader-lab
100•ragojose•3d ago•22 comments

Turtle WoW classic server announces shutdown after Blizzard wins injunction

https://www.pcgamer.com/games/world-of-warcraft/turtle-wow-classic-server-announces-shutdown-afte...
70•Brajeshwar•2h ago•46 comments

What are skiplists good for?

https://antithesis.com/blog/2026/skiptrees/
219•mfiguiere•2d ago•46 comments

College instructor turns to typewriters to curb AI-written work

https://sentinelcolorado.com/uncategorized/a-college-instructor-turns-to-typewriters-to-curb-ai-w...
409•gnabgib•23h ago•372 comments

NIST scientists create 'any wavelength' lasers

https://www.nist.gov/news-events/news/2026/04/any-color-you-nist-scientists-create-any-wavelength...
385•rbanffy•21h ago•166 comments

Reading Input from an USB RFID Card Reader

https://kevwe.com/blog/usb-rfid-reader
13•kevwedotse•2d ago•3 comments

Show HN: Prompt-to-Excalidraw demo with Gemma 4 E2B in the browser (3.1GB)

https://teamchong.github.io/turboquant-wasm/draw.html
55•teamchong•7h ago•22 comments

Pairwise Order of a Sequence of Elements

https://morwenn.github.io//presortedness/2026/04/11/TSB010-pairwise-order-of-a-sequence-of-elemen...
18•ibobev•2d ago•1 comments

Anonymous request-token comparisons from Opus 4.6 and Opus 4.7

https://tokens.billchambers.me/leaderboard
587•anabranch•1d ago•553 comments

Reverse Engineering ME2's USB with a Heat Gun and a Knife

https://github.com/coremaze/ME2-Writeup
6•Bawoosette•1d ago•0 comments

The electromechanical angle computer inside the B-52 bomber's star tracker

https://www.righto.com/2026/04/B-52-star-tracker-angle-computer.html
392•NelsonMinar•1d ago•100 comments

When moving fast, talking is the first thing to break

https://daverupert.com/2026/04/more-talk-less-grok/
65•Brajeshwar•3h ago•28 comments

Notes from the SF Peptide Scene

https://12gramsofcarbon.com/p/notes-from-the-sf-peptide-scene
81•theahura•3h ago•64 comments

Minimal Viable Programs (2014)

https://joearms.github.io/published/2014-06-25-minimal-viable-program.html
26•bachmeier•4d ago•5 comments

Binary GCD

https://en.algorithmica.org/hpc/algorithms/gcd/#binary-gcd
57•tosh•9h ago•1 comments

Why Japan has such good railways

https://worksinprogress.co/issue/why-japan-has-such-good-railways/
507•RickJWagner•1d ago•467 comments

The creative software industry has declared war on Adobe

https://www.theverge.com/tech/913765/adobe-rivals-free-creative-software-app-updates
125•tambourine_man•4h ago•97 comments

Matt Mullenweg Overrules Core Committers; Puts Akismet on WP 7's Connector List

https://www.therepository.email/matt-mullenweg-overrules-core-committers-to-put-akismet-on-wordpr...
44•mooreds•3h ago•34 comments

Ask HN: How did you land your first projects as a solo engineer/consultant?

192•modelcroissant•9h ago•86 comments

The world in which IPv6 was a good design (2017)

https://apenwarr.ca/log/20170810
165•signa11•15h ago•56 comments

It's cool to care (2025)

https://alexwlchan.net/2025/cool-to-care/
68•surprisetalk•4d ago•33 comments

Updating Gun Rocket through 10 years of Unity Engine

https://jackpritz.com/blog/updating-gun-rocket-through-10-years-of-unity-engine
106•tyleo•3d ago•51 comments