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 (integer or floating) 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. Here's a neat (or cursed) little solution I came up with: declare the enum inside a function parameter to give it "scope": UPDATE: Multiple readers have pointed out that casting the result of sizeof to void silences the unused warnings (duh!

Get the Android app

Or read this on Hacker News

Read more on:

Photo of expression

expression

Related news:

News photo

Detecting if an expression is constant in C

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