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•1y ago

Comments

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

The foundations of a provably secure operating system (PSOS) (1979) [pdf]

http://www.csl.sri.com/users/neumann/psos.pdf
39•rurban•1h ago•12 comments

GenCAD

https://gencad.github.io/
348•dagenix•13h ago•86 comments

Where Are the Vibecoded Photoshops?

https://indiepixel.de/blog/posts/where-are-the-vibecoded-photoshops/
113•gizmo64k•1h ago•87 comments

Crystals found inside wreckage from the first nuclear bomb test

https://www.scientificamerican.com/article/strange-crystals-found-inside-wreckage-from-the-first-...
91•jumploops•2d ago•33 comments

Multiple commencement speakers booed for AI comments during graduation speeches

https://www.nbcnews.com/video/multiple-commencement-speakers-booed-for-ai-comments-during-graduat...
84•wrxd•2h ago•53 comments

It is time to give up the dualism introduced by the debate on consciousness

https://www.noemamag.com/there-is-no-hard-problem-of-consciousness/
111•ahalbert4•8h ago•277 comments

I turned a $80 RK3562 Android tablet into a Debian Linux workstation

https://github.com/tech4bot/rk3562deb
380•tech4bot•22h ago•174 comments

Ask an Astronaut: 333 hours of Q&A footage with astronauts

https://askanastronaut.issinrealtime.org/
157•gaws•2d ago•13 comments

Jank now has its own custom IR

https://jank-lang.org/blog/2026-05-08-optimization/
156•DASD•2d ago•29 comments

Show HN: Semble – Code search for agents that uses 98% fewer tokens than grep

https://github.com/MinishLab/semble
349•Bibabomas•19h ago•116 comments

Prolog Coding Horror

https://www.metalevel.at/prolog/horror
150•RohanAdwankar•14h ago•61 comments

Profunctor Equipment in Haskell

https://bartoszmilewski.com/2026/05/16/profunctor-equipment-in-haskell/
26•g0xA52A2A•1d ago•4 comments

kharp – k version 3 Language Interpreter in C#

https://github.com/ERufian/ksharp
37•tosh•1d ago•4 comments

WriteUp: 16 Bytes of x86 that turn Matrix rain into sound

https://hellmood.111mb.de//wake_up_16b_writeup.html
150•HellMood•12h ago•31 comments

A Good Lemma Is Worth a Thousand Theorems (2007)

https://sites.math.rutgers.edu/~zeilberg/Opinion82.html
66•susam•2d ago•13 comments

Why is Google Maps back to showing old satellite images of Altadena?

https://www.reddit.com/r/pasadena/s/94BHlkE84r
35•tgrowazay•3h ago•17 comments

Tesla Solar Roof is on life support as it pivot to panels

https://electrek.co/2026/05/14/tesla-solar-roof-promise-vs-reality-pivot-panels/
274•celsoazevedo•1d ago•276 comments

Magical Realism: “Northern Exposure” 25 Years Later (2015)

https://www.rogerebert.com/streaming/magical-realism-nothern-exposure-25-years-later
119•walterbell•2d ago•52 comments

Two EA-18 fighter jets collide at Mountain Home airshow, pilots ejected safely

https://idahonews.com/news/local/two-f-18-fighter-jets-have-crashed-during-an-airshow-at-mountain...
204•ChrisArchitect•13h ago•202 comments

Build a Radio Wave Detector with Balls of Aluminum Foil

https://www.wired.com/story/build-a-radio-wave-detector-with-balls-of-aluminum-foil/
17•Brajeshwar•2d ago•5 comments

CUDA Books

https://github.com/alternbits/awesome-cuda-books
201•dariubs•22h ago•51 comments

Graphing Scientific Calculator Based on the ESP32

https://github.com/El-EnderJ/NeoCalculator
5•uticus•2d ago•1 comments

Hindenburg’s Smoking Room

https://www.airships.net/hindenburg-smoking-room/
208•crescit_eundo•3d ago•166 comments

Prolog Basics Explained with Pokémon

https://unplannedobsolescence.com/blog/prolog-basics-pokemon/
258•birdculture•3d ago•41 comments

NASA still maintains some of the Voyager spacecraft code from the 70s era

https://spacedaily.com/nasa-still-maintains-some-of-the-voyager-spacecraft-code-in-a-1970s-era-pr...
5•redbell•39m ago•2 comments

I don't think AI will make your processes go faster

https://frederickvanbrabant.com/blog/2026-05-15-i-dont-think-ai-will-make-your-processes-go-faster/
608•TheEdonian•23h ago•410 comments

Cannibalistic attacks between gray seals leave telltale “corkscrew” injuries

https://www.science.org/content/article/scientists-id-corkscrew-killer-behind-gruesome-seal-deaths
70•gmays•3d ago•30 comments

High-Entropy Alloy

https://en.wikipedia.org/wiki/High-entropy_alloy
139•leonidasrup•4d ago•23 comments

Trials on veterans suggest ibogaine could provide a new treatment for PTSD

https://www.bbc.com/future/article/20260514-how-hallucinogenic-ibogaine-helps-veterans-overcome-ptsd
99•bushwart•23h ago•107 comments

Show HN: Mezz, a curl-able WiFi sandbox for IoT pentesting

https://github.com/ABGEO/mezz
22•ABGEO•2d ago•7 comments