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

Sergey Brin's Unretirement

https://www.inc.com/jessica-stillman/google-co-founder-sergey-brins-unretirement-is-a-lesson-for-...
154•iancmceachern•5d ago•154 comments

The creator of Claude Code's Claude setup

https://twitter.com/bcherny/status/2007179832300581177
334•KothuRoti•4d ago•184 comments

Vector graphics on GPU

https://gasiulis.name/vector-graphics-on-gpu/
23•gsf_emergency_6•4d ago•1 comments

Show HN: TCP chat server written in C# and .NET 9, used in the terminal

https://github.com/Sieep-Coding/simple-chat-csharp
9•sieep•4d ago•0 comments

Stop Doom Scrolling, Start Doom Coding: Build via the terminal from your phone

https://github.com/rberg27/doom-coding
417•rbergamini27•13h ago•293 comments

Electronic nose for indoor mold detection and identification

https://advanced.onlinelibrary.wiley.com/doi/10.1002/adsr.202500124
108•PaulHoule•8h ago•54 comments

Opus 4.5 is not the normal AI agent experience that I have had thus far

https://burkeholland.github.io/posts/opus-4-5-change-everything/
541•tbassetto•15h ago•736 comments

Show HN: SMTP Tunnel – A SOCKS5 proxy disguised as email traffic to bypass DPI

https://github.com/x011/smtp-tunnel-proxy
52•lobito25•8h ago•18 comments

A 30B Qwen model walks into a Raspberry Pi and runs in real time

https://byteshape.com/blogs/Qwen3-30B-A3B-Instruct-2507/
229•dataminer•12h ago•64 comments

Rust is beyond object-oriented, part 3: Inheritance (2023)

https://www.thecodedmessage.com/posts/oop-3-inheritance/
23•zahrevsky•3d ago•28 comments

On the slow death of scaling

https://papers.ssrn.com/sol3/papers.cfm?abstract_id=5877662
64•sethbannon•5h ago•11 comments

Oral microbiome sequencing after taking probiotics

https://blog.booleanbiotech.com/oral-microbiome-biogaia
137•sethbannon•12h ago•59 comments

Vietnam bans unskippable ads

https://saigoneer.com/vietnam-news/28652-vienam-bans-unskippable-ads,-requires-skip-button-to-app...
1299•hoherd•16h ago•670 comments

We recreated Steve Jobs's 1975 Atari horoscope program

https://blog.adafruit.com/2026/01/06/we-recreated-steve-jobss-1975-atari-horoscope-program-and-yo...
61•ptorrone•8h ago•22 comments

Calling All Hackers: How money works (2024)

https://phrack.org/issues/71/17
239•krrishd•13h ago•155 comments

What *is* code? (2015)

https://www.bloomberg.com/graphics/2015-paul-ford-what-is-code/
30•bblcla•5d ago•10 comments

I wanted a camera that doesn't exist, so I built it

https://medium.com/@cristi.baluta/i-wanted-a-camera-that-doesnt-exist-so-i-built-it-5f9864533eb7
351•cyrc•4d ago•108 comments

CES 2026: Taking the Lids Off AMD's Venice and MI400 SoCs

https://chipsandcheese.com/p/ces-2026-taking-the-lids-off-amds
99•rbanffy•11h ago•56 comments

Show HN: VaultSandbox – Test your real MailGun/SES/etc. integration

https://vaultsandbox.com/
30•vaultsandbox•19h ago•2 comments

Microsoft probably killed my Snapdragon Dev Kit

https://jasoneckert.github.io/myblog/how-microsoft-killed-my-snapdragon-devkit/
158•jasoneckert•6h ago•87 comments

Launch HN: Tamarind Bio (YC W24) – AI Inference Provider for Drug Discovery

66•denizkavi•15h ago•17 comments

Two ways to crack a walnut, per Grothendieck (2025)

https://shreevatsa.net/post/grothendieck-approaches/
29•ethanseal•2d ago•6 comments

Comparing AI agents to cybersecurity professionals in real-world pen testing

https://arxiv.org/abs/2512.09882
95•littlexsparkee•12h ago•62 comments

Show HN: Open-source AI workflows with read-only auth scopes

https://github.com/seer-engg/seer
11•akshay326•4d ago•0 comments

High-Performance DBMSs with io_uring: When and How to use it

https://arxiv.org/abs/2512.04859
135•matt_d•13h ago•33 comments

Show HN: Make audio loops online

https://makeloops.online/
34•bilalba•1d ago•8 comments

Locating a Photo of a Vehicle in 30 Seconds with GeoSpy

https://geospy.ai/blog/locating-a-photo-of-a-vehicle-in-30-seconds-with-geospy
120•kachapopopow•15h ago•107 comments

Laylo (YC S20) – Head of Growth (Organic and Partners and Loops and AI) – Remote US

https://www.ycombinator.com/companies/laylo/jobs/ZtLHRXe-head-of-growth
1•amellin794•11h ago

Passing of Joe Mancuso

https://github.com/MasoniteFramework/masonite/discussions/853
171•wilsonfiifi•15h ago•16 comments

PassSeeds – hijacking Passkeys to unlock new cryptographic use cases

https://backalleycoder.com/posts/passseeds-an-experiment-in-hijacking-passkeys-to-unlock-cryptogr...
50•csuwldcat•8h ago•32 comments