frontpage.
newsnewestaskshowjobs

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.

LLMs reward expertise

https://www.seangoedecke.com/llms-reward-expertise/
427•MaxMussio•4h ago•189 comments

Ten advances in mathematics and theoretical computer science

https://openai.com/index/ten-advances-in-mathematics/
428•milkshakes•9h ago•709 comments

Amazonian civilization had estimated 3M people in 3% of forest area

https://www.science.org/content/article/odd-shapes-hidden-dense-amazon-rainforest-reveal-sprawlin...
15•marojejian•5d ago•6 comments

Ask HN: Who is hiring? (August 2026)

103•whoishiring•10h ago•102 comments

Devtools must be open source

https://blog.exe.dev/devtools-must-be-open-source
501•bryanmikaelian•11h ago•177 comments

Windows XP 2002 for the Itanium: Unbridled rage

https://virtuallyfun.com/2026/08/03/windows-xp-2002-for-the-itanium-unbridled-rage/
54•jandeboevrie•3h ago•24 comments

Ask HN: Who wants to be hired? (August 2026)

52•whoishiring•10h ago•177 comments

Smaller, faster, safer: running Kimi and GLM at scale

https://blog.cloudflare.com/smaller-faster-safer-models/
142•ascorbic•8h ago•39 comments

MiniMax H3 Day-0 Support in ComfyUI: Open Weights, Native Audio, and 2K Video

https://blog.comfy.org/p/minimax-h3-day-0-support-in-comfyui
253•vblanco•12h ago•79 comments

Prevent cognitive debt by manually retyping LLM-generated code

https://ankursethi.com/blog/prevent-cognitive-debt-by-manually-retyping-llm-generated-code/
381•mpweiher•16h ago•327 comments

Celebrating 45 Years of Kermit with the First New C-Kermit Release in 15 Years

https://changelog.complete.org/archives/44456-celebrating-45-years-of-kermit-with-the-first-new-c...
123•roryirvine•8h ago•35 comments

There Will Come Soft Rains (1950) [pdf]

https://users.wpi.edu/~zrbutzke/Docs/BradburyStories(1).pdf
13•pmg101•2h ago•3 comments

200 Milliseconds

https://200ms.thenodebook.com
184•dimitarpanov•2d ago•59 comments

ZX Spectrum System Tour: Text Mode

https://bumbershootsoft.wordpress.com/2026/05/30/zx-spectrum-system-tour-text-mode/
21•rbanffy•4h ago•0 comments

Andy Pavlo joins ClickHouse to establish ClickHouse Labs

https://clickhouse.com/blog/andy-pavlo-joins-clickhouse
271•nikolay_sivko•11h ago•56 comments

Launch HN: Hoplite (YC S26) – Effortlessly deploy cloud coding agents

https://hoplite.sh
58•BenceRed•9h ago•51 comments

Replacing the Kobo Libra H2O Battery

https://ei3lh.eu/2025/11/20/replacing-the-kobo-libra-h2o-battery/
46•austinallegro•4d ago•14 comments

They Forgot What Happened Last Time: Hacking the Windows 365 Link [video]

https://media.ccc.de/v/emf2026-93-1-they-forgot-what-happened-last-time
10•Jimmc414•3d ago•0 comments

How Hollywood stopped making movies in Hollywood

https://www.statsignificant.com/p/how-hollywood-stopped-making-movies
164•speckx•6d ago•192 comments

The Dunning-Kruger effect may just be a data artefact (2020)

https://www.mcgill.ca/oss/article/critical-thinking/dunning-kruger-effect-probably-not-real
122•audreyfei•6h ago•127 comments

Frame selection is the whole game: notes on making LLMs watch video

https://leoaido.com/how-llms-watch-video/
7•cortexosmain•12h ago•1 comments

Bonsai: Janestreet's UI Library

https://github.com/janestreet/bonsai
302•KolmogorovComp•17h ago•125 comments

AirLLM 70B inference with single 4GB GPU

https://github.com/lyogavin/airllm
187•Anon84•14h ago•76 comments

Decades-old fish sauce at abandoned factory in Canada finally being removed

https://defector.com/abandoned-fish-sauce-canada-interview
194•ohjeez•3d ago•211 comments

Twenty Years of Pandoc

https://pandoc.org/twenty-years-of-pandoc.html
107•fiddlosopher•10h ago•14 comments

Massively Parallel Postgres Backups

https://planetscale.com/blog/massively-parallel-postgres-backups
87•ksec•3d ago•11 comments

KisakCOD – open-source reimplementation of Call of Duty 4 Multiplayer

https://github.com/SwagSoftware/KisakCOD
37•skibz•6h ago•3 comments

Kelly Criterion Simulator

https://kellysimulator.com/
55•aleyan•3d ago•26 comments

The Billable Usage API: programmatic cost visibility for Cloudflare

https://blog.cloudflare.com/billable-usage-api/
45•ashleypeacock•8h ago•7 comments

ZX Spectrum System Tour: Sound

https://bumbershootsoft.wordpress.com/2026/08/01/zx-spectrum-system-tour-sound/
32•ibobev•7h ago•7 comments