473,698 Members | 2,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stdint types and conversion...

Hello all,

Conversion macros along the name of INT8_C, INT16_C etc, are defined in
stdint.h to convert their argument into suitable representations for
their corresponding types, i.e. int8_t, int16_t etc.

My questions are:
1. Should the conversion macros be used even when assigning from a
variable of the same type?

2. Why are no such macros provided for int_fast8_t etc. as well as
int_least8_t etc? Can they be assigned values from other types provided
there are no size mismatches and expect the correct thing to happen?

Thanks.

Mar 25 '06 #1
11 3160
santosh wrote:
Hello all,

Conversion macros along the name of INT8_C, INT16_C etc, are defined in
stdint.h to convert their argument into suitable representations for
their corresponding types, i.e. int8_t, int16_t etc.
These are not *c*onversion macros, they are used to declare *c*onstants of
the appropriate types. "Conversion " implies something is being converted,
which is not the case; the constant will have the appropriate type. Your
other questions follow from this misconception.
My questions are:
1. Should the conversion macros be used even when assigning from a
variable of the same type?
No. In fact, you must *not* use them:

"The argument in any instance of these macros shall be a decimal, octal, or
hexadecimal constant (as defined in 6.4.4.1) with a value that does not
exceed the limits for the corresponding type." [7.8.14, paragraph 2].

A variable of any kind is not (required to be) a constant.
2. Why are no such macros provided for int_fast8_t etc. as well as
int_least8_t etc? Can they be assigned values from other types provided
there are no size mismatches and expect the correct thing to happen?

No. The usual rules apply: you need a cast for values that might not fit,
and it's your responsibility to ensure they fit. (A gross oversimplificat ion
of the actual rules, of course, see any good book on C about implicit and
explicit conversion.)

S.
Mar 25 '06 #2
Skarmander wrote:
santosh wrote:
Hello all,

Conversion macros along the name of INT8_C, INT16_C etc, are defined in
stdint.h to convert their argument into suitable representations for
their corresponding types, i.e. int8_t, int16_t etc.

These are not *c*onversion macros, they are used to declare *c*onstants of
the appropriate types. "Conversion " implies something is being converted,
which is not the case; the constant will have the appropriate type. Your
other questions follow from this misconception.


If I understand correctly the macros are to be used when assigning
literal constant values to variables of their respective types?
Something like:

....
int8_t i8 = INT8_C(-112);
....
My questions are:
1. Should the conversion macros be used even when assigning from a
variable of the same type?

No. In fact, you must *not* use them:

"The argument in any instance of these macros shall be a decimal, octal, or
hexadecimal constant (as defined in 6.4.4.1) with a value that does not
exceed the limits for the corresponding type." [7.8.14, paragraph 2].

A variable of any kind is not (required to be) a constant.
2. Why are no such macros provided for int_fast8_t etc. as well as
int_least8_t etc? Can they be assigned values from other types provided
there are no size mismatches and expect the correct thing to happen?

No. The usual rules apply: you need a cast for values that might not fit,
and it's your responsibility to ensure they fit. (A gross oversimplificat ion
of the actual rules, of course, see any good book on C about implicit and
explicit conversion.)

S.


Thanks for the clarification.

Mar 25 '06 #3
On 25 Mar 2006 04:30:08 -0800, "santosh" <sa*********@gm ail.com>
wrote:
Hello all,

Conversion macros along the name of INT8_C, INT16_C etc, are defined in
stdint.h to convert their argument into suitable representations for
their corresponding types, i.e. int8_t, int16_t etc.
My copy of n1124 does not show any such macros. Where did you find
them? Why would they be needed? You define a variable of the desired
type with a simple
int8_t x;
and you can convert an expression to the desired type with a simple
cast
x = (int8_t)<expres sion>

My questions are:
1. Should the conversion macros be used even when assigning from a
variable of the same type?

2. Why are no such macros provided for int_fast8_t etc. as well as
int_least8_t etc? Can they be assigned values from other types provided
there are no size mismatches and expect the correct thing to happen?

Thanks.

Remove del for email
Mar 25 '06 #4
santosh wrote:
Skarmander wrote:
santosh wrote:
Hello all,

Conversion macros along the name of INT8_C, INT16_C etc, are defined in
stdint.h to convert their argument into suitable representations for
their corresponding types, i.e. int8_t, int16_t etc.

These are not *c*onversion macros, they are used to declare *c*onstants of
the appropriate types. "Conversion " implies something is being converted,
which is not the case; the constant will have the appropriate type. Your
other questions follow from this misconception.


If I understand correctly the macros are to be used when assigning
literal constant values to variables of their respective types?
Something like:

...
int8_t i8 = INT8_C(-112);
...

"The macro INT/N/_C(value) shall expand to an integer constant expression
corresponding to the type int_least/N/_t. [..] For example, if
uint_least64_t is a name for the type unsigned long long int, then
UINT64_C(0x123) might expand to the integer constant 0x123ULL."

The _C macros are necessary because you do not know what types are used to
implement the intN_least_t types, so you would not know how to write down
the constant.

For the exact-width integer types (intN_t) these macros are not necessary,
because you can always write down the appropriate constant: for 8 and 16
bits an integer constant will do, for 32 bits a long constant, and for 64
bits a long long constant (of course, the constant has to fit the range of
the integer).

S.
Mar 25 '06 #5
Barry Schwarz wrote:
On 25 Mar 2006 04:30:08 -0800, "santosh" <sa*********@gm ail.com>
wrote:
Hello all,

Conversion macros along the name of INT8_C, INT16_C etc, are defined in
stdint.h to convert their argument into suitable representations for
their corresponding types, i.e. int8_t, int16_t etc.
My copy of n1124 does not show any such macros. Where did you find
them?


Section 7.8.14. Your search function will not help you here, since these
macros are not mentioned by name.
Why would they be needed?
I provided the answer upthread: you do not necessarily know how to write
down constants of the appropriate type.
You define a variable of the desired
type with a simple
int8_t x;
and you can convert an expression to the desired type with a simple
cast
x = (int8_t)<expres sion>

This is true, but not relevant to the macros under discussion.

S.
Mar 25 '06 #6
Barry Schwarz wrote:
On 25 Mar 2006 04:30:08 -0800, "santosh" <sa*********@gm ail.com>
wrote:
Hello all,

Conversion macros along the name of INT8_C, INT16_C etc, are defined in
stdint.h to convert their argument into suitable representations for
their corresponding types, i.e. int8_t, int16_t etc.
My copy of n1124 does not show any such macros. Where did you find
them? Why would they be needed?


The are mentioned in the stdint.h file on P.J. Plauger's Dinkumware
site, (C99 Standard Library Reference).

http://www.dinkumware.com/manuals/re...&h=stdint.html
You define a variable of the desired
type with a simple
int8_t x;
and you can convert an expression to the desired type with a simple
cast
x = (int8_t)<expres sion>


Thanks for the explanation.

Mar 25 '06 #7
Skarmander wrote:
santosh wrote:
Skarmander wrote:
santosh wrote:
Hello all,

Conversion macros along the name of INT8_C, INT16_C etc, are defined in
stdint.h to convert their argument into suitable representations for
their corresponding types, i.e. int8_t, int16_t etc.

These are not *c*onversion macros, they are used to declare
*c*onstants of
the appropriate types. "Conversion " implies something is being
converted,
which is not the case; the constant will have the appropriate type. Your
other questions follow from this misconception.


If I understand correctly the macros are to be used when assigning
literal constant values to variables of their respective types?
Something like:

...
int8_t i8 = INT8_C(-112);
...

"The macro INT/N/_C(value) shall expand to an integer constant
expression corresponding to the type int_least/N/_t. [..] For example,
if uint_least64_t is a name for the type unsigned long long int, then
UINT64_C(0x123) might expand to the integer constant 0x123ULL."

The _C macros are necessary because you do not know what types are used
to implement the intN_least_t types, so you would not know how to write
down the constant.

For the exact-width integer types (intN_t) these macros are not
necessary, because you can always write down the appropriate constant:
for 8 and 16 bits an integer constant will do, for 32 bits a long
constant, and for 64 bits a long long constant (of course, the constant
has to fit the range of the integer).


Well, the above is obviously faulty. Poking holes in it is left as an
exercise to the reader. At this stage I don't trust myself enough anymore to
do it.

S.

Mar 25 '06 #8
Skarmander wrote:
santosh wrote:
If I understand correctly the macros are to be used when assigning
literal constant values to variables of their respective types?
Something like:

...
int8_t i8 = INT8_C(-112);
...

"The macro INT/N/_C(value) shall expand to an integer constant expression
corresponding to the type int_least/N/_t. [..] For example, if
uint_least64_t is a name for the type unsigned long long int, then
UINT64_C(0x123) might expand to the integer constant 0x123ULL."

The _C macros are necessary because you do not know what types are used to
implement the intN_least_t types, so you would not know how to write down
the constant.

For the exact-width integer types (intN_t) these macros are not necessary,
because you can always write down the appropriate constant: for 8 and 16
bits an integer constant will do, for 32 bits a long constant, and for 64
bits a long long constant (of course, the constant has to fit the range of
the integer).

S.


Thanks muchly for the explanation. I understand now.

Mar 25 '06 #9
On Sat, 25 Mar 2006 18:08:08 +0100, Skarmander
<in*****@dontma ilme.com> wrote in comp.lang.c:
santosh wrote:
Hello all,

Conversion macros along the name of INT8_C, INT16_C etc, are defined in
stdint.h to convert their argument into suitable representations for
their corresponding types, i.e. int8_t, int16_t etc.

These are not *c*onversion macros, they are used to declare *c*onstants of
the appropriate types. "Conversion " implies something is being converted,
which is not the case; the constant will have the appropriate type. Your
other questions follow from this misconception.
My questions are:
1. Should the conversion macros be used even when assigning from a
variable of the same type?

No. In fact, you must *not* use them:

"The argument in any instance of these macros shall be a decimal, octal, or
hexadecimal constant (as defined in 6.4.4.1) with a value that does not
exceed the limits for the corresponding type." [7.8.14, paragraph 2].

A variable of any kind is not (required to be) a constant.
2. Why are no such macros provided for int_fast8_t etc. as well as
int_least8_t etc? Can they be assigned values from other types provided
there are no size mismatches and expect the correct thing to happen?

No. The usual rules apply: you need a cast for values that might not fit,
and it's your responsibility to ensure they fit. (A gross oversimplificat ion
of the actual rules, of course, see any good book on C about implicit and
explicit conversion.)


I'm not sure what you mean by your last paragraph. When performing
assignment among arithmetic types, there are always automatic implicit
conversions when the types are different.

In all the following cases:

-- assigning from a floating point type of greater rank to one of
lesser rank

-- assigning from a floating point type to any integer type

-- assigning from an integer type to a floating point type

....if the value of the original is outside the range of values
representable in the destination type, the result is undefined. A
cast, that is an explicit conversion, does not change this. If the
behavior of the conversion is undefined without the cast, it is just
as undefined with it.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Mar 25 '06 #10

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

Similar topics

6
2950
by: J. Campbell | last post by:
I'm wondering if there are any compelling reasons NOT to use the types defined in stdint.h in c-programms? Please refer to this thread: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=3nzzb.3886%24xm.135697%40nasal.pacific.net.au Thanks
8
4390
by: phil-news-nospam | last post by:
I have some code where I am using certain literal values cast to stdint types like uint32_t, uint64_t, etc. In gcc versions below 3.3 it's working OK. Here's an example: (uint64_t) 0x5555555555555555U In gcc 3.3.1, it doesn't like this, saying that the literal is out of range for "unsigned long" (well, yeah, that would be true). What is the proper way to write a literal when you want the type to be of a specific stdint type, as
4
2340
by: copx | last post by:
I wonder how portable the exact width types defined by stdint.h are. I guess target platforms are not required to actually have all those types, right? What happens in that case?
130
6759
by: euler70 | last post by:
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...
0
8611
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8876
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7741
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4372
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2341
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.