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.

Level S4 solar radiation event

https://www.swpc.noaa.gov/news/g4-severe-geomagnetic-storm-levels-reached-19-jan-2026
394•WorldPeas•11h ago•136 comments

Reticulum, a secure and anonymous mesh networking stack

https://github.com/markqvist/Reticulum
166•brogu•7h ago•29 comments

The Overcomplexity of the Shadcn Radio Button

https://paulmakeswebsites.com/writing/shadcn-radio-button/
3•dbushell•23m ago•0 comments

x86 prefixes and escape opcodes flowchart

https://soc.me/interfaces/x86-prefixes-and-escape-opcodes-flowchart.html
30•gaul•4h ago•6 comments

Apple testing new App Store design that blurs the line between ads and results

https://9to5mac.com/2026/01/16/iphone-apple-app-store-search-results-ads-new-design/
337•ksec•15h ago•250 comments

Nanolang: A tiny experimental language designed to be targeted by coding LLMs

https://github.com/jordanhubbard/nanolang
139•Scramblejams•10h ago•94 comments

What came first: the CNAME or the A record?

https://blog.cloudflare.com/cname-a-record-order-dns-standards/
340•linolevan•14h ago•121 comments

Scaling long-running autonomous coding

https://simonwillison.net/2026/Jan/19/scaling-long-running-autonomous-coding/
85•srameshc•7h ago•28 comments

The coming industrialisation of exploit generation with LLMs

https://sean.heelan.io/2026/01/18/on-the-coming-industrialisation-of-exploit-generation-with-llms/
131•long•1d ago•93 comments

Show HN: Artificial Ivy in the Browser

https://da.nmcardle.com/grow
49•dnmc•4h ago•3 comments

Nova Launcher added Facebook and Google Ads tracking

https://lemdro.id/post/lemdro.id/35049920
202•celsoazevedo•6h ago•88 comments

Notes on Apple's Nano Texture (2025)

https://jon.bo/posts/nano-texture/
177•dsr12•13h ago•97 comments

3D printing my laptop ergonomic setup

https://www.ntietz.com/blog/3d-printing-my-laptop-ergonomic-setup/
44•kurinikku•8h ago•3 comments

Porsche sold more electrified cars in Europe in 2025 than pure gas-powered cars

https://newsroom.porsche.com/en/2026/company/porsche-deliveries-2025-41516.html
266•m463•6h ago•293 comments

British redcoat's lost memoir reveals realities of life as a disabled veteran

https://phys.org/news/2026-01-british-redcoat-lost-memoir-reveals.html
73•wglb•4d ago•58 comments

Understanding ZFS Scrubs and Data Integrity

https://klarasystems.com/articles/understanding-zfs-scrubs-and-data-integrity/
39•zdw•5d ago•15 comments

Face as a QR Code

https://bookofjoe2.blogspot.com/2025/12/your-face-as-qr-code.html
9•surprisetalk•3d ago•1 comments

Kahan on the 8087 and designing Intel's floating point (2016) [video]

https://www.youtube.com/watch?v=L-QVgbdt_qg
13•bananaboy•4d ago•0 comments

The assistant axis: situating and stabilizing the character of LLMs

https://www.anthropic.com/research/assistant-axis
85•mfiguiere•10h ago•12 comments

Targeted Bets: An alternative approach to the job hunt

https://www.seanmuirhead.com/blog/targeted-bets
57•seany62•10h ago•62 comments

I was a top 0.01% Cursor user, then switched to Claude Code 2.0

https://blog.silennai.com/claude-code
82•SilenN•22h ago•140 comments

F-16 Falcon Strike

https://webchrono.pl/F16FalconStrike/index.html
104•starkparker•4h ago•8 comments

From Nevada to Kansas by Glider

https://www.weglide.org/flight/978820
138•sammelaugust•4d ago•39 comments

The microstructure of wealth transfer in prediction markets

https://www.jbecker.dev/research/prediction-market-microstructure
145•jonbecker•15h ago•132 comments

Legal Structures for Latin American Startups (2021)

https://latamlist.com/legal-structures-for-latin-american-startups/
22•walterbell•6h ago•6 comments

How we made Python's packaging library 3x faster

https://iscinumpy.dev/post/packaging-faster/
65•rbanffy•3d ago•6 comments

Chatbot Psychosis

https://en.wikipedia.org/wiki/Chatbot_psychosis
39•tbmtbmtbmtbmtbm•2h ago•14 comments

Show HN: An interactive physics simulator with 1000’s of balls, in your terminal

https://github.com/minimaxir/ballin
57•minimaxir•14h ago•10 comments

I set all 376 Vim options and I'm still a fool

https://evanhahn.com/i-set-all-376-vim-options-and-im-still-a-fool/
123•todsacerdoti•3d ago•67 comments

Selling SaaS in Japan

https://embedworkflow.com/blog/what-saas-founders-should-know-about-entering-the-japanese-market/
62•ewf•4d ago•29 comments