typedef vs. #define

M

Mark

Hello

are there any other pros to using #defined new types, except the one
mentioned in C-FAQ 1.13? Quite often I see in chips SDKs things like these:

#ifndef int8
#define int8 char
#endif
....
#ifndef uint32
#define uint32 unsigned int
#endif

Looks quite ugly to me, I'd prefer 'typedef' these types, but perhaps I'm
missing something?
 
M

Malcolm McLean

are there any other pros to using #defined new types, except the one
mentioned in C-FAQ 1.13? Quite often I see in chips SDKs things like these:

Looks quite ugly to me, I'd prefer 'typedef' these types, but perhaps I'm
missing something?
For very short programs it doesn't really matter.
However #defines are word processing commands, typedefs alias types.
This means that you can do things like

#define int8 char
unsigned int8


The technical term for this is "preprocessor abuse". It can be handy.
Another thing you can do is
stringise the #defined type macro. This might be useful if writing
some sort of God-awful generic function, using strings as cheap and
cheerful C++ templates.
 
F

Fred

For very short programs it doesn't really matter.
However #defines are word processing commands, typedefs alias types.
This means that you can do things like

#define int8 char
unsigned int8

The technical term for this is "preprocessor abuse". It can be handy.
Another thing you can do is
stringise the #defined type macro. This might be useful if writing
some sort of God-awful generic function, using strings as cheap and
cheerful C++ templates.

Using the #define method can easily lead to errors.
Consider:
#define String char*
vs.
typedef String char*

followed by:
String a,b,c;

If you used the #define, bad things are likely to occur.
 
S

Seebs

Looks quite ugly to me, I'd prefer 'typedef' these types, but perhaps I'm
missing something?

The obvious thing would be the lack of a way to spell "iftypedef(uint32)".

-s
 
I

ImpalerCore

Hello

are there any other pros to using #defined new types, except the one
mentioned in C-FAQ 1.13? Quite often I see in chips SDKs things like these:

#ifndef int8
#define int8    char
#endif
...
#ifndef uint32
#define uint32   unsigned int
#endif

Looks quite ugly to me, I'd prefer 'typedef' these types, but perhaps I'm
missing something?

The typedef is the preferred implementation, but as Seebs said, not
having a convenient syntax for verifying that a typedef exists either
leads them to using #define or relying on an autoconf test to define a
HAVE_UINT32_T or maybe a HAVE_STDINT_H symbol. In my case, I have a
stdint wrapper that either includes the real stdint.h if HAVE_STDINT_H
is defined, or declare my own stdint subset (consisting mostly the
uintN_t types and the _C, _MIN, and _MAX macros).
 
M

Mark

Seebs said:
The obvious thing would be the lack of a way to spell
"iftypedef(uint32)".
That was mentioned in FAQ, I thought there could be other benefits too.
 
N

Nick Keighley


Hello, I've taken my pedantry medication this morning.

are there any other pros to using #defined new types,

technically, you can't create new types with a preprocessor macro.

[...] Quite often I see in chips SDKs things like these:

#ifndef int8
#define int8    char
#endif
...
#ifndef uint32
#define uint32   unsigned int
#endif

Looks quite ugly to me, I'd prefer 'typedef' these types, but perhaps I'm
missing something?

you can't create a new types witha typedef. typedef only creates a
type alias.


Consider:-
<code>

#define M_int int
typedef int T_int;

struct S_int1
{
int i;
};

struct S_int2
{
int i;
};

int main (void)
{
int *pi;
M_int *mpi;
T_int *tpi;
struct S_int1 *spi1;
struct S_int2 *spi2;

mpi = pi; /* 1 */
tpi = pi; /* 2 */

spi1 = spi2; /* 3 */

return 0;
}

</code>

the lines marked 1 and 2 don't give an error because in the first case
the copiler sees no difference and the second they are merely aliases
for each other. structs on the other hand *are* new types so you get a
type incompatibility warning (from VCC anyway).

Is this a required diagnostic? I'd hope so.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top