473,544 Members | 1,334 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Standard integer types vs <stdint.h> types

char and unsigned char have specific purposes: char is useful for
representing characters of the basic execution character set and
unsigned char is useful for representing the values of individual
bytes. The remainder of the standard integer types are general
purpose. Their only requirement is to satisfy a minimum range of
values, and also int "has the natural size suggested by the
architecture of the execution environment". What are the reasons for
using these types instead of the [u]int_fastN_t types of <stdint.h>?

If I want just a signed integer with at least 16 width, then why
choose int instead of int_fast16_t? int_fast16_t is the "fastest"
signed integer type that has at least 16 width, while int is simply a
signed integer type that has at least 16 width. It seems that choosing
int_fast16_t is at least as good as choosing int. This argument can be
made for N=8,16,32,64, and for the corresponding unsigned types.
<stdint.halso offers [u]int_leastN_t types, which are useful when
keeping a small storage size is the greatest concern.

The only benefit of using the standard integer types I can see is that
their ranges may expand as the C standard progresses, so code that
uses them might stay useful over time. For example fseek of <stdio.h>
uses a long for its offset parameter, so if the range of long grows
then fseek will automatically offer a wider range of offsets.

It's also interesting to note (or maybe not) that in the Windows
world, the idea of long being general purpose has somewhat been
destroyed and long has become a type that must have exactly 32 bits.
Jan 18 '08
130 6637

"Flash Gordon" <sp**@flash-gordon.me.ukwro te in message
See above, the types you want have been supported for a long time. If you
don't like the spelling you can typedef them to something else.
bool breaks libraries.

If ANSI decides that "s64" shall be the name of a new type, that's fine. If
I define it myself, either the whole world adopts my convention, which is to
say I have usurrped ANSI / ISO, or it is a nuisance to everybody trying to
call or read my code.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jan 18 '08 #21
Flash Gordon wrote:
>
There *are* times when you need to know, and there are times when you
need to know a type has at least a specific range, but a lot of the time
you do not care if it is larger.
Rarely.

If I write

int a = 45000;

that will not work in 16 bit implementations .

You HAVE to know the bitsize to know how much data
you can put into an integer!

Potentially, all assignments are affected unless
you never use integer data bigger than 32767.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Jan 18 '08 #22
Richard Heathfield wrote:
Bart C said:
>How can one program without knowing the bitsize of one's datatypes?

We know the minimum value range of our data types - why would we need to
know more than that?
>But I've probably spent too many years low-level programming.

Okay, that's one reason. Any more? Huh? Huh? :-)
Consider

int i = 38700;

If you do not care about int size you will never know that
this will not work in 16 bit implementations , or (worst)
i will be initialized to something completely different.

Of course YOU never use data bigger than 32767, as everyone knows.

:-)
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Jan 18 '08 #23
jacob navia said:
Flash Gordon wrote:
>>
There *are* times when you need to know, and there are times when you
need to know a type has at least a specific range, but a lot of the time
you do not care if it is larger.

Rarely.

If I write

int a = 45000;

that will not work in 16 bit implementations .
That is not a counter-example to Flash's point. If you need an integer type
that can store 45000, then "you need to know a type has at least a
specific range". Since the least range for int is -32767 to +32767, you
know that this won't do in portable code, so you move up to long int:

long int a = 45000;

This will work just fine in 16 bit implementations , no matter how big a
long int is, because it *must* have at least 32 bits.
You HAVE to know the bitsize to know how much data
you can put into an integer!
No, you have to know whether the value you wish to store will fit into the
range for a given type. If so, it doesn't matter how many /more/ bits it
has. And if not, it still doesn't matter how many bits it has - if your
value doesn't fit, it doesn't fit.
Potentially, all assignments are affected unless
you never use integer data bigger than 32767.
Right, which is why we have this thing called long int.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jan 18 '08 #24
jacob navia said:

<snip>
Consider

int i = 38700;
Why? It's not guaranteed to work. Instead, consider:

long int i = 38700;

which *is* guaranteed to work.
If you do not care about int size you will never know that
this will not work in 16 bit implementations , or (worst)
i will be initialized to something completely different.
I don't know whose point you're arguing against, but the above response
does not address any statement I have made.
Of course YOU never use data bigger than 32767, as everyone knows.
Not so. But when I do, I use long int. And if I need values higher than
2147483647 and know they can't be negative, I'll use unsigned long int.
And if I need values higher than 4294967295, I'll use my bignum library.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jan 18 '08 #25
Malcolm McLean wrote, On 18/01/08 15:21:
>
"Flash Gordon" <sp**@flash-gordon.me.ukwro te in message
>See above, the types you want have been supported for a long time. If
you don't like the spelling you can typedef them to something else.
bool breaks libraries.
So don't use it. Use _Bool if you want instead. There is not requirement
for you to #include <stdbool.hto use the new boolean type.
If ANSI decides that "s64" shall be the name of a new type, that's fine.
If I define it myself, either the whole world adopts my convention,
which is to say I have usurrped ANSI / ISO, or it is a nuisance to
everybody trying to call or read my code.
Misusing int as you are want to do causes problems.

Either get yourself on the standard committee and try and get the
language changed
Or find a language that is defined as you want and use that
Or write your own language and use that
Or put up with the language as it is defined.

In any case, by your definition of what is wrong it was wrong in the
1989 standard so stop complaining that these things are rapidly
destroying the language, since that is patently false.
--
Flash Gordon
Jan 18 '08 #26

"jacob navia" <ja***@nospam.c omwrote in message
>
If I write

int a = 45000;

that will not work in 16 bit implementations .

You HAVE to know the bitsize to know how much data
you can put into an integer!
The question is, why do you want an integer equal to 45000?
We can thinks of lots of possible reasons, but a good one might be that you
have 45000 customer records you wish to read into memory.
Which means that you need 45000 * N bytes of contiguous storage, which most
machines will happily provide for fairly reasonable values of N. But not 16
bit machines.
As long as int is the same as a pointer and memory space is flat, everything
is fine.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Jan 18 '08 #27
eu*****@gmail.c om wrote:
>
.... snip ...
>
For portable code, this can become detrimental to efficiency. If I
want a fast type with at least 32-bit range, C90 says I should
choose long. This might end up being a good choice for one
compiler, but on another compiler where long has another de-facto
purpose for long that causes long to be significantly less
efficient than another available at-least-32-bit type, then half
of the intent behind my choice of long has been ruined.

If you argue against the preceding paragraph by saying "you should
not be so concerned about efficiency", then I think your reasoning
is a very short step away from concluding that we can discard our
worries about type ranges and efficiency and simply use only
intmax_t and uintmax_t everywhere. Surely this is not the level of
abstraction that C is intended for.
I think you have exagerrated the problem. In general there will
only be one beast where you have to be critical of the exact size.
You should set up your own type, and alias it to the appropriate
standard type by using limits.h and conditional defines. Then
write your code in terms of that type. Note that this also allows
for use of other than the multiple of 8 sizes.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Jan 18 '08 #28
Bart C wrote:
"Richard Heathfield" <rj*@see.sig.in validwrote:
>Bart C said:
>>How can one program without knowing the bitsize of one's datatypes?

We know the minimum value range of our data types - why would we
need to know more than that?

For ... performance?

If I know I need, say, 32-bits, but not 64-bits which would be an
overkill, what do I use? int could be only 16. long int is at
least 32 but could be 64.

I would have to choose long int but at the risk of being
inefficient (I might need a few hundred million of them).
The basic problem here is historic. C was organized to allow for
16 bit processors, which lead to the 16 bit minimum size for ints.
Without that the minimum sizes for short, int, long could have been
16, 32, 64, and long long would never have arisen. We can't change
the minima without breaking all sorts of programs.

At least the situation is such that there can be no more than one
trap value for 16 bit ints. :-)

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Jan 18 '08 #29
Malcolm McLean wrote:
I think we are creating a mess with all these integer types and
conventions on how they should be used.
That's probably inevitable with C having to target every conceivable
hardware and having to maintain compatibility with every line of code ever
written.

My main interest is PCs which are typically 32/64 bits now, and will be at
least that for the foreseeable future. So I don't really care about anything
other than 8,16,32,64 bits. (And it can't be a coincidence that these sizes
figure heavily in stdint.h.).
However generally I want an integer which can index or count an
array, and is fast, and is signed, because intermediate calculations
might go below zero.
This is usually semi-achievable. There will be a fast type the same
width as the address bus, the one bit we can ignore as we are
unlikely to want a char array taking up half memory (we can always
resort to "unsigned" for that special situation), but it might not be
the fastest type, and most arrays will in fact be a lot smaller than
the largest possible array.
The number of (physical) address bits appearing on the cpu pinouts is
unlikely to be a full 64 bits. Even virtual address spaces are a lot less
than 64-bits I would guess.

(64-bits addresses a *lot* of memory. It might just be enough to address all
the memory in every desktop PC in the world.)

If your task-space is 4GB or less than your array indexes/indices, size-t's
and so on don't need to be more than 32-bit unsigned. But your file-system
is likely to need 64-bits.

(Actually 48-bits will probably suffice for most purposes but that is too
odd a figure.)

--
Bart


Jan 18 '08 #30

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

Similar topics

4
1628
by: hostmaster | last post by:
Hi, I'm quite new to Python and I am reading this book called 'Core Python Programming' by Wesley J. Chun. I think that this is not a new book but I believe some of the points are still valid. There is this part in the book where it says: "In Python, standard types are not classes, so creating integers and strings does not involve...
6
1484
by: cppsks | last post by:
What would be the rational for using const with standard types? const int & OR const unsigned long & Thanks.
5
4171
by: Boni | last post by:
Dear all, how do I convert a .NET integer into System.Uint32 (needed to call external function) Thanks, Boni
11
2648
by: Frederick Gotham | last post by:
I'd like to discuss the use of signed integers types where unsigned integers types would suffice. A common example would be: #include <cassert> #include <cstddef> int CountOccurrences(unsigned char const val, unsigned char const p,
2
5112
by: Frederick Gotham | last post by:
I just want to clarify my understanding of arithmetic and comparison between two different integer types. Phase (1): Integer Promotion ---------- All of the following types always get promoted to "signed int": signed char
9
1566
by: Roman Mashak | last post by:
Hello, All! I often come across in open-source software definitions of standard types with new names, for example: typedef char SYS_CHAR; typedef unsigned short SYS_UINT16; .... Why not to use types specified in C99 or Posix standards? I believe that's
22
3262
by: David T. Ashley | last post by:
I'm writing code which will compile on multiple Windows and multiple *nix platforms. Some will have 64-bit integers, I believe. What is my best option to define platform-independent types such as UINT32 or UINT64? I'm not sure how to sort out the various platforms and integer sizes, and some of it may even vary based on compiler options. ...
3
3353
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt, &gt ,&ltCompany&gt to display '<', '>' and '<Company>' respectively. But is there any freeware code available which could implement the above...
159
6132
by: Bob Timpkinson | last post by:
Hi, I have a 32-bit machine... Is there anyway I can get gcc to use the following integer sizes? char: 8 bits short: 16 bits int: 32 bits long: 64 bits long long: 128 bits
0
7429
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7777
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5916
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4922
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3420
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3414
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1844
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
990
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.