Marshall
Marshall

Reputation: 1362

What does this Define mean?

I'm taking a course in C and came across this #define. Reading up on it, it is that you define something. Example:

#define FAMILY 4

Then every time I set something equal to family or call family it is the value 4. But I also came across this:

#define EVER ;;
#define FAMILY 4

What does it mean if after ever there are two semicolons? Does it mean EVER = ";;"?

Upvotes: 4

Views: 1224

Answers (3)

nestedloop
nestedloop

Reputation: 2646

EVER will be equivalent to exactly this:

;;

This means that you could have:

#define EVER ;;

//..... 

for(EVER){printf("This will print forever");}

which will be equivalent to:

for(;;){printf("This will print forever");}

You should however exercise caution when using aliases for such structures, as your application gets bigger you could get weird and hard to debug issues if you mess some #define statement.

In my opinion, a classic while(true) might be healthier in the long run, though not as witty.

Upvotes: 12

glglgl
glglgl

Reputation: 91059

It is a funny way to replace an endless loop

for(;;) { ... }

with

for(EVER) { ... }

in order to make clear it is an endless loop.

Another option would be to do

#define forever while (1)

so we can do

forever { ... }

or even

do { ... } forever

Upvotes: 6

Some programmer dude
Some programmer dude

Reputation: 409206

The preprocessor is a step that runs before the actual compiler runs. The preprocessor does a simple search-replace of macros in the source, and passes it on to the compiler proper.

For example, with the EVER macro as defined in your question, you could use it as

for (EVER) { ... }

and the preprocessor will simply transform it into

for (;;) { ... }

which the compiler will see.

Upvotes: 5

Related Questions