473,224 Members | 1,435 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

Operational and functional differences - #define vs typedef ?

Are there differences in terms of functionality
of,

#define
and
typedef ?

By the above I mean any instance when the outcome
obtained by running two versions (1) and (2) below
of C codes are different ?

For example,

(1) In one C code, it has:

#define u_ unsigned
#define uc_ unsigned char

(2) The same C code is re-writtened to:

typedef unsigned u_;
typdef unsigned char uc_;

TIA.

O Plameras

Dec 15 '05 #1
10 7467
O Plameras <os****@acay.com.au> writes:
Are there differences in terms of functionality
of,

#define
and
typedef ?

By the above I mean any instance when the outcome
obtained by running two versions (1) and (2) below
of C codes are different ?

For example,

(1) In one C code, it has:

#define u_ unsigned
#define uc_ unsigned char

(2) The same C code is re-writtened to:

typedef unsigned u_;
typdef unsigned char uc_;


#define creates a macro. It's expanded literally at each point where
it's used. Most syntax checking and parsing occurs *after* macros are
expanded.

A typedef creates an alias for a type. Macros and typedefs can be
used similarly, but they're really completely different things.

For example, given:
#define u_ unsigned
you can then declare
_u long x;
because the "_u" expands to "unsigned" before the type name
"unsigned long" is parsed.

In general, typedefs are better than macros for defining type aliases,
because that's what they're designed for, and because the syntax of
type declarations is such that a macro isn't always going to work.

In this particular case, I'd recommend using neither macros nor
typedefs. Someone reading your code is going to know what "unsigned"
and "unsigned char" mean. He won't know what "u_" and "uc_" mean
unless he looks up your typedef.

The names "unsigned" and "unsigned char" are perfectly clear; there's
no benefit in inventing other names for them. (Saving keystrokes
isn't much of a benefit, especially when it makes your code more
difficult to understand.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 15 '05 #2

Keith Thompson wrote:
In this particular case, I'd recommend using neither macros nor
typedefs. Someone reading your code is going to know what "unsigned"
and "unsigned char" mean. He won't know what "u_" and "uc_" mean
unless he looks up your typedef.


How do you feel about using typedefs to ensure consistency across
platforms,
as in the case of establishing int32_t and int64_t types? (Naturally,
that requires
putting your typedef definitions within preprocessor conditionals that
ascertain
the nature of the platform...)

-bluejack

Dec 15 '05 #3
O Plameras wrote:

Are there differences in terms of functionality
of,

#define
and
typedef ?


/* BEGIN new.c */

#define VOID_POINTER void *

typedef void * void_pointer;

int main(void)
{
void_pointer a, b;
VOID_POINTER c, d; /* This line won't compile */

return 0;
}

/* END new.c */

--
pete
Dec 15 '05 #4
"bluejack" <bl******@gmail.com> writes:
Keith Thompson wrote:
In this particular case, I'd recommend using neither macros nor
typedefs. Someone reading your code is going to know what "unsigned"
and "unsigned char" mean. He won't know what "u_" and "uc_" mean
unless he looks up your typedef.


How do you feel about using typedefs to ensure consistency across
platforms, as in the case of establishing int32_t and int64_t types?
(Naturally, that requires putting your typedef definitions within
preprocessor conditionals that ascertain the nature of the
platform...)


That's a great idea -- which is why it's part of the C99 standard's
<stdint.h> header.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 15 '05 #5
O Plameras wrote:

Are there differences in terms of functionality
of,

#define
and
typedef ?

By the above I mean any instance when the outcome
obtained by running two versions (1) and (2) below
of C codes are different ?

For example,

(1) In one C code, it has:

#define u_ unsigned
#define uc_ unsigned char

(2) The same C code is re-writtened to:

typedef unsigned u_;
typdef unsigned char uc_;


Given your "u_" and "uc_" examples, your #define would allow:

u_ long l; /* unsigned long */

whereas the typedef wouldn't.

On the other hand, consider:

#define PCHAR char *
and
typedef char * PCHAR;

and then see what happens with:

PCHAR pt1, pt2;

There are also times when #define simply can't be used:

typedef void (*VoidVoidFuncPtr)(void);
If you want to define a type, use typedef.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Dec 16 '05 #6
On Thu, 15 Dec 2005 15:30:12 -0800, Keith Thompson <ks***@mib.org> wrote:
That's a great idea -- which is why it's part of the C99 standard's
<stdint.h> header.


Cool. I haven't researched all the differences between old ansi/k&r C's
(which is what I use, mostly) and C99, but that's a nice thing to know,
and a very handy feature.

(Also, I switched off google's cruddy web interface. Correct quoting
ensues.)

-bluejack

/* Work as though you live in the younger days of a better nation */
Dec 16 '05 #7
"Blunt Jackson" <bl******@no.diatom.spam.com> wrote:
On Thu, 15 Dec 2005 15:30:12 -0800, Keith Thompson <ks***@mib.org> wrote:
That's a great idea -- which is why it's part of the C99 standard's
<stdint.h> header.
Cool. I haven't researched all the differences between old ansi/k&r C's
(which is what I use, mostly)


Note that those are two separate versions of C. First there was what is
called K&R C, which was the C that Kernighan and Ritchie described in
the _first_ edition of "The C Programming Language"; and then there was
ANSI C89, which is (more or less) what is described in K&R _2_. (And
ANSI C89 was adopted by ISO, so it's also ISO C90.)
Lastly, there was (is now) ISO C99, which was also ratified by ANSI
(being participant in ISO), so that's also ANSI C99.

The differences between K&R C and C89 are larger, IYAM, than between
both ISO Cs. For example, ANSI C89 was the one that introduced
prototypes; they weren't in K&R C.
(Also, I switched off google's cruddy web interface. Correct quoting
ensues.)


And there was much rejoicing.

Richard
Dec 16 '05 #8
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"Blunt Jackson" <bl******@no.diatom.spam.com> wrote:
On Thu, 15 Dec 2005 15:30:12 -0800, Keith Thompson <ks***@mib.org> wrote:
> That's a great idea -- which is why it's part of the C99 standard's
> <stdint.h> header.


Cool. I haven't researched all the differences between old ansi/k&r C's
(which is what I use, mostly)


Note that those are two separate versions of C.


For sufficiently large values of "two".

[description of three versions of C snipped]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 16 '05 #9
Keith Thompson <ks***@mib.org> wrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"Blunt Jackson" <bl******@no.diatom.spam.com> wrote:
On Thu, 15 Dec 2005 15:30:12 -0800, Keith Thompson <ks***@mib.org> wrote:

> That's a great idea -- which is why it's part of the C99 standard's
> <stdint.h> header.

Cool. I haven't researched all the differences between old ansi/k&r C's
(which is what I use, mostly)


Note that those are two separate versions of C.


For sufficiently large values of "two".


No, the _old_ ANSI and K&R C are two versions. The _new_ ISO C99 is the
third, which Blunt Jackson mentioned separately.

Richard
Dec 19 '05 #10
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Keith Thompson <ks***@mib.org> wrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
> "Blunt Jackson" <bl******@no.diatom.spam.com> wrote:
>> On Thu, 15 Dec 2005 15:30:12 -0800, Keith Thompson <ks***@mib.org> wrote:
>> > That's a great idea -- which is why it's part of the C99 standard's
>> > <stdint.h> header.
>>
>> Cool. I haven't researched all the differences between old ansi/k&r C's
>> (which is what I use, mostly)
>
> Note that those are two separate versions of C.


For sufficiently large values of "two".


No, the _old_ ANSI and K&R C are two versions. The _new_ ISO C99 is the
third, which Blunt Jackson mentioned separately.


Sorry, I read "those are two separate versions" as "there are two
separate versions".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 19 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then...
10
by: lallous | last post by:
Hello I noticed that when I use #define inside a namespace then this #define only works when prefixed with the namespace name. Is this behaviour standard or just compiler specific? I use...
8
by: beza1e1 | last post by:
I see myself shifting more and more over to the functional kind of coding. Could be related to the Haskell, we had to learn in CS. Now i was wondering, how other people use Python? With...
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
3
by: robin liu | last post by:
What's the difference between these two declarations ? 1) typedef void (*pf)(void); 2) typedef void f(void); the first declaration is define a function pointer, what is the second ? define a...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
71
by: David T. Ashley | last post by:
Where is the best place to define TRUE and FALSE? Are they in any of the standard include files, ever? Do any standards apply? What I've traditionally done is something like: #ifndef...
23
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
6
by: wongjoekmeu | last post by:
I need to rewrite some typedef to #define. For instance I rewrote typedef void* handle to #define handle void* But I saw for instance another typedef in my code which I don't understand,...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.