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

Comments

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

Motorola announces a partnership with GrapheneOS

https://motorolanews.com/motorola-three-new-b2b-solutions-at-mwc-2026/
1853•km•14h ago•661 comments

New iPad Air, powered by M4

https://www.apple.com/newsroom/2026/03/apple-introduces-the-new-ipad-air-powered-by-m4/
247•Garbage•7h ago•408 comments

First in-utero stem cell therapy for fetal spina bifida repair is safe: study

https://health.ucdavis.edu/news/headlines/first-ever-in-utero-stem-cell-therapy-for-fetal-spina-b...
197•gmays•6h ago•21 comments

Show HN: Govbase – Follow a bill from source text to news bias to social posts

https://govbase.com
99•foxfoxx•4h ago•52 comments

"That Shape Had None" – A Horror of Substrate Independence (Short Fiction)

https://starlightconvenience.net/#that-shape-had-none
41•casmalia•2h ago•7 comments

LFortran Compiles Fpm

https://lfortran.org/blog/2026/02/lfortran-compiles-fpm/
19•wtlin•2d ago•1 comments

Show HN: uBlock filter list to blur all Instagram Reels

https://gist.github.com/shraiwi/009c652da6ce8c99a6e1e0c86fe66886
45•shraiwi•1h ago•9 comments

Ask HN: Who is hiring? (March 2026)

124•whoishiring•5h ago•159 comments

Launch HN: OctaPulse (YC W26) – Robotics and computer vision for fish farming

45•rohxnsxngh•4h ago•21 comments

British Columbia to end time changes, adopt year-round daylight time

https://www.cbc.ca/news/canada/british-columbia/b-c-adopting-year-round-daylight-time-9.7111657
51•ireflect•49m ago•11 comments

Ask HN: Who wants to be hired? (March 2026)

34•whoishiring•5h ago•108 comments

How to talk to anyone and why you should

https://www.theguardian.com/lifeandstyle/2026/feb/24/stranger-secret-how-to-talk-to-anyone-why-yo...
483•Looky1173•13h ago•480 comments

Packaging a Gleam app into a single executable

https://www.dhzdhd.dev/blog/gleam-executable
62•todsacerdoti•5h ago•3 comments

Parallel coding agents with tmux and Markdown specs

https://schipper.ai/posts/parallel-coding-agents/
83•schipperai•6h ago•56 comments

Reflex (YC W23) Is Hiring Software Engineers – Python

https://www.ycombinator.com/companies/reflex/jobs
1•apetuskey•4h ago

A case for Go as the best language for AI agents

https://getbruin.com/blog/go-is-the-best-language-for-agents/
97•karakanb•2h ago•136 comments

Inside the M4 Apple Neural Engine, Part 1: Reverse Engineering

https://maderix.substack.com/p/inside-the-m4-apple-neural-engine
201•zdw•1d ago•54 comments

iPhone 17e

https://www.apple.com/newsroom/2026/03/apple-introduces-iphone-17e/
114•meetpateltech•7h ago•89 comments

Use the Mikado Method to do safe changes in a complex codebase

https://understandlegacycode.com/blog/a-process-to-do-safe-changes-in-a-complex-codebase/
124•foenix•4d ago•63 comments

Show HN: Pianoterm – Run shell commands from your Piano. A Linux CLI tool

https://github.com/vustagc/pianoterm
5•vustagc•30m ago•0 comments

Why Objective-C

https://inessential.com/2026/02/27/why-objective-c.html
97•ingve•2d ago•89 comments

Zclaw – The 888 KiB Assistant

https://zclaw.dev
62•kristianpaul•2d ago•34 comments

Build your own Command Line with ANSI escape codes (2016)

https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html
17•vinhnx•2d ago•3 comments

Boss-CSS: I created another "CSS-in-JS" lib

https://dev.to/wintercounter/boss-css-i-created-another-css-in-js-lib-and-here-is-why-23kc
4•wintercounter•1h ago•0 comments

Language Model Contains Personality Subnetworks

https://arxiv.org/abs/2602.07164
35•PaulHoule•5h ago•20 comments

Microsoft bans the word "Microslop" on its Discord, then locks the server

https://www.windowslatest.com/2026/03/02/microsoft-gets-tired-of-microslop-bans-the-word-on-its-d...
935•robtherobber•10h ago•407 comments

19th century silent film that first captured a robot attack

https://www.npr.org/2026/02/28/nx-s1-5730373/georges-melies-robot-film-1897-library-of-congress-g...
24•ynac•6h ago•5 comments

Making Video Games in 2025 (without an engine)

https://www.noelberry.ca/posts/making_games_in_2025/
381•alvivar•3d ago•179 comments

Notes on Lagrange Interpolating Polynomials

https://eli.thegreenplace.net/2026/notes-on-lagrange-interpolating-polynomials/
33•ibobev•5h ago•9 comments

OpenClaw surpasses React to become the most-starred software project on GitHub

https://www.star-history.com/blog/openclaw-surpasses-react-most-starred-software
223•whit537•7h ago•251 comments