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

Comments

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

Defeating a 40-year-old copy protection dongle

https://dmitrybrant.com/2026/02/01/defeating-a-40-year-old-copy-protection-dongle
557•zdw•13h ago•170 comments

Apple's MacBook Pro DFU port documentation is wrong

https://lapcatsoftware.com/articles/2026/2/1.html
107•zdw•7h ago•20 comments

My iPhone 16 Pro Max produces garbage output when running MLX LLMs

https://journal.rafaelcosta.me/my-thousand-dollar-iphone-cant-do-math/
302•rafaelcosta•13h ago•130 comments

Show HN: Wikipedia as a doomscrollable social media feed

https://xikipedia.org
208•rebane2001•10h ago•74 comments

Show HN: NanoClaw – “Clawdbot” in 500 lines of TS with Apple container isolation

https://github.com/gavrielc/nanoclaw
376•jimminyx•11h ago•134 comments

Ratchets in Software Development

https://qntm.org/ratchet
29•nvader•3d ago•5 comments

Actors: A Model of Concurrent Computation [pdf] (1985)

https://apps.dtic.mil/sti/tr/pdf/ADA157917.pdf
89•kioku•9h ago•33 comments

What Most People Miss About Getting Promoted – By Yue Zhao

https://news.theuncommonexecutive.com/p/what-most-people-miss-about-getting
8•yuezhao•1h ago•23 comments

Contracts in Nix

https://sraka.xyz/posts/contracts.html
57•todsacerdoti•1d ago•14 comments

Apple I Advertisement (1976)

http://apple1.chez.com/Apple1project/Gallery/Gallery.htm
239•janandonly•16h ago•129 comments

Adventure Game Studio: OSS software for creating adventure games

https://www.adventuregamestudio.co.uk/
323•doener•20h ago•67 comments

Treasures found on HS2 route stored in secret warehouse

https://www.bbc.com/news/articles/c93v21q5xdvo
65•breve•12h ago•33 comments

Attilio Berni plays the sub-contrabass saxophone [video]

https://www.youtube.com/watch?v=9BiW2mVKk0w
8•surprisetalk•3d ago•3 comments

Ian's Shoelace Site

https://www.fieggen.com/shoelace/
170•righthand•15h ago•32 comments

Leaked Chats Expose the Daily Life of a Scam Compound's Enslaved Workforce

https://www.wired.com/story/the-red-bull-leaks/
123•smurda•5h ago•54 comments

Time Machine-style Backups with rsync (2018)

https://samuelhewitt.com/blog/2018-06-05-time-machine-style-backups-with-rsync
80•accrual•10h ago•35 comments

Building Your Own Efficient uint128 in C++

https://solidean.com/blog/2026/building-your-own-u128/
79•PaulHoule•13h ago•33 comments

Rev Up the Viral Factories

https://www.science.org/content/blog-post/rev-viral-factories
12•etiam•2d ago•0 comments

Efficient String Compression for Modern Database Systems

https://cedardb.com/blog/string_compression/
125•jandrewrogers•2d ago•32 comments

Two kinds of AI users are emerging

https://martinalderson.com/posts/two-kinds-of-ai-users-are-emerging/
203•martinald•10h ago•183 comments

Founding is a snowball

https://blog.bawolf.com/p/founding-is-a-snowball
82•bryantwolf•3d ago•27 comments

MicroPythonOS graphical operating system delivers Android-like user experience

https://www.cnx-software.com/2026/01/29/micropythonos-graphical-operating-system-delivers-android...
217•mikece•3d ago•80 comments

Netbird – Open Source Zero Trust Networking

https://netbird.io/
685•l1am0•1d ago•264 comments

FOSDEM 2026 – Open-Source Conference in Brussels – Day#1 Recap

https://gyptazy.com/blog/fosdem-2026-opensource-conference-brussels/
233•yannick2k•1d ago•162 comments

Building a Telegram Bot with Cloudflare Workers, Durable Objects and Grammy

https://flashblaze.xyz/posts/cloudflare-workers-durable-objects-telegram-bot/
22•flashblaze•7h ago•5 comments

Towards a science of scaling agent systems: When and why agent systems work

https://research.google/blog/towards-a-science-of-scaling-agent-systems-when-and-why-agent-system...
86•gmays•16h ago•30 comments

A Crisis comes to Wordle: Reusing old words

https://forkingmad.blog/wordle-crisis/
93•cyanbane•16h ago•103 comments

EU launches government satcom program in sovereignty push

https://spacenews.com/eu-launches-government-satcom-program-in-sovereignty-push/
9•benkan•1h ago•1 comments

Amiga Unix (Amix)

https://www.amigaunix.com/doku.php/home
127•donatj•23h ago•54 comments

Stop Using Pseudo-Types

https://f2r.github.io/en/stop-using-pseudo-types.html
13•speckx•4d ago•1 comments