Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are #define and typedef operands inverted?

The following defines A to be replaced by B:

#define A B

Whereas this defines A to be an alias for the type B:

typedef B A;

Why ? Isn't this incoherent ?

like image 832
fouronnes Avatar asked Feb 27 '11 20:02

fouronnes


People also ask

Why is MGK called MGK?

George Kelly Barnes (July 18, 1895 – July 18, 1954), better known by his pseudonym "Machine Gun Kelly", was an American gangster from Memphis, Tennessee, active during the Prohibition era. His nickname came from his favorite weapon, a Thompson submachine gun.

Why is MGK famous?

Colson Baker (born April 22, 1990), known professionally as Machine Gun Kelly (MGK), is an American rapper, singer, songwriter, and actor. He is noted for his genre duality across alternative rock with hip hop. Houston, Texas, U.S. Cleveland, Ohio, U.S.


1 Answers

Simply put: consider the following variable declarations:

// declare a variable called "myInt" with type int
int myInt;
// declare a variable called "myDouble" with type double
double myDouble;
// declare a variable called "myLong" with type long
long myLong;
// declare a variable called "myFunc" with type pointer to function
void (*myFunc)(char*);

Then the typedefs make perfect sense:

// declare a type alias called "myInt" with type int
typedef int myInt;
// declare a type alias called "myDouble" with type double
typedef double myDouble;
// declare a type alias called "myLong" with type long
typedef long myLong;
// declare a type alias called "myFunc" with type pointer to function
typedef void (*myFunc)(char*);

Macros, on the other hand, can take on a function-style syntax:

#define A(B, C) B, C

A(1, 2) // expands out to 1, 2

So for macros, the "definition" coming after the "name" makes more sense.

(This applies to C++ too, by the way.)

like image 116
In silico Avatar answered Sep 27 '22 22:09

In silico