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•7mo 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.

The Illustrated Transformer

https://jalammar.github.io/illustrated-transformer/
280•auraham•7h ago•56 comments

It's Always TCP_NODELAY

https://brooker.co.za/blog/2024/05/09/nagle.html
164•eieio•5h ago•42 comments

Snitch – a friendly netstat alternative for humans

https://github.com/karol-broda/snitch
12•karol-broda•1h ago•5 comments

Flock Exposed Its AI-Powered Cameras to the Internet. We Tracked Ourselves

https://www.404media.co/flock-exposed-its-ai-powered-cameras-to-the-internet-we-tracked-ourselves/
416•chaps•10h ago•357 comments

Ultrasound Cancer Treatment: Sound Waves Fight Tumors

https://spectrum.ieee.org/ultrasound-cancer-treatment
184•rbanffy•7h ago•49 comments

GLM-4.7: Advancing the Coding Capability

https://z.ai/blog/glm-4.7
254•pretext•8h ago•109 comments

Claude Code gets native LSP support

https://github.com/anthropics/claude-code/blob/main/CHANGELOG.md
319•JamesSwift•11h ago•168 comments

The Garbage Collection Handbook

https://gchandbook.org/index.html
151•andsoitis•7h ago•11 comments

FPGAs Need a New Future

https://www.allaboutcircuits.com/industry-articles/fpgas-need-a-new-future/
85•thawawaycold•3d ago•51 comments

NIST was 5 μs off UTC after last week's power cut

https://www.jeffgeerling.com/blog/2025/nist-was-5-μs-utc-after-last-weeks-power-cut
193•jtokoph•9h ago•94 comments

Pulled 60 Minutes segment on CECOT

https://archive.org/details/60minutes-cecotsegment
41•DustinEchoes•1h ago•2 comments

Laptop Isn't Ready for LLMs. That's About to Change

https://spectrum.ieee.org/ai-models-locally
5•barqawiz•2h ago•1 comments

Show HN: C-compiler to compile TCC for live-bootstrap

https://github.com/FransFaase/MES-replacement
26•fjfaase•5d ago•4 comments

Scaling LLMs to Larger Codebases

https://blog.kierangill.xyz/oversight-and-guidance
212•kierangill•11h ago•87 comments

Our New Sam Audio Model Transforms Audio Editing

https://about.fb.com/news/2025/12/our-new-sam-audio-model-transforms-audio-editing/
23•ushakov•6d ago•5 comments

Universal Reasoning Model (53.8% pass 1 ARC1 and 16.0% ARC 2)

https://arxiv.org/abs/2512.14693
67•marojejian•8h ago•7 comments

Lotusbail npm package found to be harvesting WhatsApp messages and contacts

https://www.koi.ai/blog/npm-package-with-56k-downloads-malware-stealing-whatsapp-messages
218•sohkamyung•4h ago•138 comments

How the RESISTORS put computing into 1960s counter-culture

https://spectrum.ieee.org/teenage-hackers
40•rbanffy•5d ago•6 comments

The biggest CRT ever made: Sony's PVM-4300

https://dfarq.homeip.net/the-biggest-crt-ever-made-sonys-pvm-4300/
224•giuliomagnifico•14h ago•143 comments

Satellites reveal heat leaking from largest US cryptocurrency mining center

https://www.space.com/space-exploration/satellites/satellites-reveal-heat-leaking-from-largest-us...
57•troglo-byte•3h ago•46 comments

NYC Spends $200 Million on Cell Service for School Chromebooks

https://nysfocus.com/2025/12/22/eric-adams-school-chromebooks-contract
15•h2si•1h ago•19 comments

Tc – Theodore Calvin's language-agnostic testing framework

https://github.com/ahoward/tc
15•mooreds•4h ago•4 comments

Things I learnt about passkeys when building passkeybot

https://enzom.dev/b/passkeys/
102•emadda•8h ago•59 comments

Uplane (YC F25) Is Hiring Founding Engineers (Full-Stack and AI)

https://www.useparallel.com/uplane1/careers
1•MarvinStarter•10h ago

Debian's Git Transition

https://diziet.dreamwidth.org/20436.html
198•all-along•18h ago•69 comments

There Is No Future for Online Safety Without Privacy and Security

https://itsfoss.com/news/alexander-linton-interview/
55•abdelhousni•4h ago•30 comments

Hybrid Aerial Underwater Drone – Bachelor Project [video]

https://www.youtube.com/watch?v=g7vmPFZrYAk
40•nhma•18h ago•21 comments

Programming languages used for music

https://timthompson.com/plum/cgi/showlist.cgi?sort=name&concise=yes
247•ofalkaed•2d ago•88 comments

US blocks all offshore wind construction, says reason is classified

https://arstechnica.com/science/2025/12/us-government-finds-new-excuse-to-stop-construction-of-of...
478•rbanffy•7h ago•394 comments

The Rise of SQL:the second programming language everyone needs to know

https://spectrum.ieee.org/the-rise-of-sql
114•b-man•4d ago•99 comments