Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you make an incrementing compiler constant?

Tags:

c

While sounding nonsensical.....

I want a Constant where every time you use it it will increment by 1

int x;
int y;
x = INCREMENTING_CONSTANT;
y = INCREMENTING_CONSTANT;

where x == 1; and y == 2

Note I don't want y = INCREMENTING_CONSTANT+1 type solutions.

Basically I want to use it as a compile time unique ID ( generally it wouldn't be used in code like the example but inside another macro)

like image 259
Keith Nicholas Avatar asked Mar 31 '10 22:03

Keith Nicholas


People also ask

Can we increment const value?

The increment operator obviously cannot applied for a const (ant) variable, because the const keyword prevents it to be changed after the initial definition. That's why the compiler error is issued.

How do you stop a loop from incrementing?

If so, it's an easy fix: just remove the incrementing statement that runs at the end of every loop iteration. That way you're left with only the one increment line in the if statement. By the way, do note that there's no point in using a for loop that only ever has one iteration.


1 Answers

If you just need something unique ID ish, can you use the __LINE__ preprocessor symbol? It's not what you're asking for, but it might work for your purposes.

like image 163
Stephen Canon Avatar answered Oct 12 '22 23:10

Stephen Canon