definition of u_int32

R

raghu

i am new to c language i come across some definitions like unsigned int
long int32 i want to know why such assignments are mentioned and what
does it mean?
 
V

Vladimir Oka

raghu said:
i am new to c language i come across some definitions like unsigned int
long int32 i want to know why such assignments are mentioned and what
does it mean?

I guess you came across:

typedef unsigned long int u_int32;

It means that now you can declare variables like this:

u_int32 i;

and it will mean exactly the same as:

unsigned long int i;

As to why someone would want to do this, there are several answers: to
save typing
; to tell the reader that the variable is 32 bits and unsigned.

Both of these are IMO wrong, and here's why:

The `unsigned long` type may not be 32 bits on all
implementations/architectures, so you'd actually introduce subtle bugs
if you relied on the new type name alone. You've just made your program
not portable.

Saving on typing is no saving really. You only type once, but the code
is read many times, and by various people. For the readers, it may be
more useful, and possibly safer as I described above, to know exactly
what you're doing.

BTW, the C standard provides for a way to specify exact width. Have a
look at <stdint.h> header for your implementation. The type names are
of the form `intN_t` where `u` siginfies unsigendness, and N is the
width in number of bits.
 
F

Flash Gordon

Vladimir said:
I guess you came across:

typedef unsigned long int u_int32;

It means that now you can declare variables like this:

u_int32 i;

and it will mean exactly the same as:

unsigned long int i;

As to why someone would want to do this, there are several answers: to
save typing
; to tell the reader that the variable is 32 bits and unsigned.

Both of these are IMO wrong, and here's why:

The `unsigned long` type may not be 32 bits on all
implementations/architectures, so you'd actually introduce subtle bugs
if you relied on the new type name alone. You've just made your program
not portable.

This is not wrong if guarded by appropriate macros to either select the
correct type (if it exists) or ensure the typedef is correct.
Saving on typing is no saving really. You only type once, but the code
is read many times, and by various people. For the readers, it may be
more useful, and possibly safer as I described above, to know exactly
what you're doing.

Saving typing is not a valid reason, but if defined properly telling the
reader it is 32 bits is, assuming that really is what the programmer
intends.
BTW, the C standard provides for a way to specify exact width. Have a
look at <stdint.h> header for your implementation. The type names are
of the form `intN_t` where `u` siginfies unsigendness, and N is the
width in number of bits.


That was only added in C99, for some strange reason I can't find that
header anywhere on my MS VC++ install despite it being the latest version!

Unfortunately most C implementations are not compliant to C99 and some
are not even attempting to move towards C99.
 
P

pete

Flash Gordon wrote:
That was only added in C99, for some strange reason I can't find that
header anywhere on my MS VC++ install
despite it being the latest version!

Maybe it has something to do with
headers not being called "header files".
Unfortunately most C implementations are not compliant to C99 and some
are not even attempting to move towards C99.

Or maybe not.
 
F

Flash Gordon

pete said:
Maybe it has something to do with
headers not being called "header files".


Or maybe not.

Definitely not. Try to include it stdint.h a program and the program
will fail to compile. Unless you provide your own version, of course.
 

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
474,183
Messages
2,570,968
Members
47,517
Latest member
TashaLzw39

Latest Threads

Top