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.

Amazon Web Services – Four Years and Out

https://www.adventuresinoss.com/aws-four-years/
102•RyeCombinator•1h ago•16 comments

Microsoft open-sources "the earliest DOS source code discovered to date"

https://arstechnica.com/gadgets/2026/04/microsoft-open-sources-the-earliest-dos-source-code-disco...
166•DamnInteresting•5h ago•34 comments

Why Is Vivado 2026.1 Dropping Linux Support for Free Tier?

https://adaptivesupport.amd.com/s/question/0D5Pd00001YQLdMKAX/why-is-vivado-20261-dropping-linux-...
62•zdw•2h ago•12 comments

Scammers are abusing an internal Microsoft account to send spam links

https://techcrunch.com/2026/05/21/scammers-are-abusing-an-internal-microsoft-account-to-send-spam/
108•spike021•5h ago•27 comments

Wake up! 16b

https://hellmood.111mb.de/wake_up_16b_writeup.html
153•MaximilianEmel•6h ago•10 comments

Alexander Grothendieck Revolutionized 20th-Century Mathematics

https://www.quantamagazine.org/how-alexander-grothendieck-revolutionized-20th-century-mathematics...
33•anujbans•3h ago•4 comments

Time to talk about my writerdeck

https://veronicaexplains.net/my-first-writerdeck/
350•hggh•11h ago•203 comments

Green card seekers must leave U.S. to apply, Trump administration says

https://www.nytimes.com/2026/05/22/us/politics/green-card-changes-trump.html
791•tlhunter•1d ago•1337 comments

Neoclassical C++: segmented iterators revisited

https://boostedcpp.net/2026/05/18/neoclassical-c-segmented-iterators-revisited-1/
12•ibobev•1d ago•0 comments

On The <dl> (2021)

https://benmyers.dev/blog/on-the-dl/
377•ravenical•17h ago•110 comments

My two-part desk setup (2025)

https://arslan.io/2025/11/18/my-two-part-desk-setup/
259•James72689•3d ago•152 comments

'Fuck you, Bambu': How one private message could change the face of 3D printing

https://www.theverge.com/tech/931532/bambu-agpl-pawel-jarczak-open-source-threat-dmca-github
59•tambourine_man•2h ago•19 comments

Sales and Dungeons: Thermal printer TTRPG utility

https://sales-and-dungeons.app/
82•hyperific•2d ago•27 comments

Buildcraft Is a Compiler Problem

https://mitander.xyz/posts/buildcraft-is-a-compiler-problem/
13•mitander•1d ago•1 comments

My I3-Emacs Integration

https://khz.ac/software/i3-integration.html
53•nosolace•7h ago•11 comments

Judson's Last Ride

https://www.realclearpolitics.com/articles/2026/05/22/judsons_last_ride_154150.html
83•NaOH•18h ago•4 comments

The Art of Money Getting

https://kk.org/cooltools/book-freak-210-the-art-of-money-getting/
256•dxs•17h ago•147 comments

Schlitz Is Gone, but First It's Getting One Last Hurrah

https://www.milwaukeemag.com/schlitz-is-gone/
22•NaOH•2d ago•9 comments

Byrne's Euclid

https://www.c82.net/euclid/
40•layer8•8h ago•12 comments

Air France and Airbus found guilty of manslaughter over 2009 plane crash

https://www.bbc.com/news/articles/czd2qmdvmq6o
59•baal80spam•10h ago•33 comments

Hengefinder: Finding when the sun aligns with your street

https://victoriaritvo.com/blog/hengefinder/
132•evakhoury•1d ago•29 comments

New map reveals lost roads of the Roman Empire

https://www.scientificamerican.com/article/new-high-resolution-map-transforms-what-we-know-about-...
68•sohkamyung•3d ago•8 comments

The C64 Dead Test Font

https://www.masswerk.at/nowgobang/2026/c64-dead-test-font
8•masswerk•2h ago•0 comments

Reverse engineering circuitry in a Spacelab computer from 1980

https://www.righto.com/2026/05/reverse-engineering-spacelab-computer.html
98•elpocko•14h ago•20 comments

.NET (OK, C#) finally gets union types

https://andrewlock.net/exploring-the-dotnet-11-preview-2-dotnet-gets-union-types/
178•ingve•1d ago•164 comments

80386 microcode disassembled

https://www.reenigne.org/blog/80386-microcode-disassembled/
241•nand2mario•18h ago•46 comments

Show HN: Anyone interested in a tool helps to explore C++ ASTs

https://uvic-aurora.github.io/acav-manual/index.html
35•leomicv•2d ago•3 comments

Kindle loyalists scramble as Amazon turns page on old e-readers

https://www.reuters.com/business/retail-consumer/kindle-loyalists-scramble-amazon-turns-page-old-...
153•cf100clunk•4d ago•167 comments

-​-dangerously-skip-reading-code

https://olano.dev/blog/dangerously-skip/
135•fagnerbrack•20h ago•129 comments

PHP's Oddities

https://flowtwo.io/post/php%27s-oddities
111•thejoeflow•4d ago•132 comments