Get the latest tech news

Detecting if an expression is constant in C


Here's a fun little language puzzle: implement a macro that takes an expression as an argument and: There's a number of ways to solve this, depending on which C standard you're using and whether compiler extensions are allowed or not. Here are a few I've come across along with some pros and cons.

May change the type of the expression due to the addition, which is subject to the typical integer promotion rules. (Note: the immediate int cast makes floating constant e.g 1.1 work, but not things like 1.1 + 2.2, without emitting warnings that is). Here's a neat (or cursed) little solution I came up with: declare the enum inside a function parameter to give it "scope":

Get the Android app

Or read this on Hacker News

Read more on:

Photo of expression

expression

Related news:

News photo

X helped senators update the Kids Online Safety Act so it can't be ‘used to stifle expression’

News photo

46% of players use gaming for self-expression and creativity, report suggests

News photo

Adam Curtis on the dangers of self-expression (2017)