C99 exact width integers usage - informal survey

A short int isn't a guaranteed 16 bits any more than a 'regular' int is. It's only guaranteed to be no shorter than 16 bits, and no longer than 'int' for the same compiler.

And a long isn't guaranteed to be longer than an 'int' -- just no shorter than 32 bits, and no shorter than an 'int' on the same compiler.

If you want portability, you have to make sure that your code won't break when you exceed 0x7fff (not 100000) in an integer _or_ a short, and you won't be guaranteed that your particular implementation will provide a check unless you -- check.

I can't think of code that is less portable than code that won't even compile.

These I could maybe see as being useful -- but they also violate your condition on the use of 'int', that they may be bigger than expected.

Until some bozo defines them to make some otherwise perfectly portable code compile because some other character used int16 or int8 because they thought it would be cool.

I'm sorry. What I see here is even more reasons to avoid 'int_xx' types like the plague. I can see some value in 'int_least_xx' and 'int_fast_xx', but not much that I can't get from 'char', 'short', 'int' and 'long'.

--
Tim Wescott
Control systems and communications consulting
http://www.wescottdesign.com

Need to learn how to apply control theory in your embedded system?
"Applied Control Theory for Embedded Systems" by Tim Wescott
Elsevier/Newnes, http://www.wescottdesign.com/actfes/actfes.html
Reply to
Tim Wescott
Loading thread data ...

Then it is guaranteed in theory but in practice you have plenty of cases where an int is less than 16 bits, which makes sense for 8 bit microcontrolers for speed reasons. I agree though that you are right on the official definition however intricate the path that led to it was. >

[snip] >

Agreed

I prefer to know in advance if a variable can hold a value. How does your objection fit if the value is bigger than an int ?

Reply to
Lanarcam

Signed/unsigned chars are integer types and can be used in the same way as integers. What can be a problem is integer promotion.

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills  Staffs  England     /\/\/\/\/
/\/\/ chris@phaedsys.org      www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
Reply to
Chris H

============================== I agree with this post.

In general, I typically use exact width integers when:

1) interfacing to HW registers 2) interfacing to existing SW components that 'demand' a fixed size.

otherwise I just use native types directly or typedef a proper domain name (we don't use 8 bit CPUs).

The reason I tend to like stdint.h types is that I don't need to learn each persons pet naming convention like U16, UINT16, u16, etc.

"MISRA tool warnings about using native types directly."

I have never fully understood this rule since typedefs are just aliases in C not new types. Was this meant to be for documentation purposes? I usually just turn that warning off.

Reply to
Marco

... snip ...

And that check is the easiest thing in the world.

#include #if INT_MAX >= 100000 ... #endif

--
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: 
            Try the download section.
Reply to
CBFalconer

I don't know of any that would reduce the size of an int, although it is theoretically possible. The programmer could have declared uint_fast8_t, for example, assuming C99 . There are compilers, though, that assign an enum to a byte if that would contain all the contained enumeration constants.

In terms of optimizations for 8-bit processor targets, I want to see compilers that can optimize

unsigned int a; /* assume 16 bits */ unsigned char b,c; /* assume 8 bits */ ... a = b * c; /* and */ a = ((a >> 9) | 32) * (a & 0xff);

into native 8x8 => 16 bit multiplies.

--
Thad
Reply to
Thad Smith

At least GCC-4 does convert loop counter to fast-int type, as defined for the architecture. Sadly, on AVR it is still 16 bits.

Daniel.

Reply to
dsc

[Snipped]

I too prefer this over explicitly defining the size in the typedef. I have had to maintain code where things such as U8, U16 etc were used. Someone at some point needed a larger size for some of the values defined as U8. In stead of finding and replacing these instances with U16, they changed the typedef of U8 to be the same as U16. It was an obvious quick fix, which worked for the single case, but caused many problems down the road.

Another advantage is of course that if one has

MOTOR_CURRENT current; VOLTAGE voltage;

current = voltage;

Then the compiler would pick this up and either generate an error or warning, which would not happen with:

S16 current; S16 volage;

current = voltage;

Regards Anton Erasmus

Reply to
Anton Erasmus

That depends on your tool set. At least some (PC-Lint comes to mind) will provide errors when attempting to mismatch typedef'd types.

It's useful to know you've never assigned a VOLTAGE to a CURRENT (or more subtly a MVOLT to a VOLT).

The use of the stdint types to satisfy the MISRA requirement not to use native types directly strikes me as a bureaucratic response. It provides a way to satisfy the letter of the requirement while avoiding most of the benefits of following the requirement.

Robert

--
Posted via a free Usenet account from http://www.teranews.com
Reply to
Robert Adsett

On closer inspection on some of the compilers I have around, no :-( It does seem like an obvious optimisation to make - it is unfortunately much easier to think of useful optimisation techniques than to implement them!

Reply to
David Brown

Probably, but there are (otherwise quite good) compilers that will suboptimize a smaller variable declaration into the maximum int size. In discussions with the developer of the compiler, there seems to be a conflict between the C treatment of chars as ints and the "as if" part of expression evaluation. This leads to coercing all chars to ints (16, 32, or whatever bits) in expressions regardless of the desired result size.

Reply to
Everett M. Greene

The "as if" rules were designed to make it possible to implement code generators that produce results as if chars for example were cast to ints and then cast back.

The effect means that many optimizations can be implemented that are processor specific that reduce the computational intensity of an application.

w..

Reply to
Walter Banks

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.