473,288 Members | 1,745 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,288 software developers and data experts.

c++ integer data types

I wrote a short page as a quick reference to c++ integer data types.
Any feedback welcome: http://www.somacon.com/blog/page11.php
Jul 22 '05 #1
13 3639
Shailesh Humbad wrote:

I wrote a short page as a quick reference to c++ integer data types. Any
feedback welcome: http://www.somacon.com/blog/page11.php


"The size of a char is guaranteed to be at least eight bits. The actual
size is an unspecified, system-dependent unit that can represent the
implementation's character set."
Wrong! The size of char is *always* 1 byte, which is not always 8 bits.
That is 1 byte is not always of 8 bits (but most times it is).

We use char, signed char and unsigned char, not only to deal with
characters, but also to deal with bytes.
For example any object of C++ can be considered (and read) as a sequence
of unsigned chars safely.
Any POD object can be read as a sequence of plain chars too. And you can
copy it to an unsigned char/plain char array of the same size and create
a (shallow) copy of the first object.
-------------------------------------------------------------------------
"A new type has been introduced, long long, that is guaranteed to be at
least 64 bits. See limits.h to get the ranges on your system."

Wrong, long long is not part of C++98 and also you omitted <limits>. So
the above would be better this way:
"Use numeric_limits in <limits> (or the C subset <climits> constants) to
get the ranges of all built in types in your system".

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #2
* Ioannis Vranos:
Shailesh Humbad wrote:

I wrote a short page as a quick reference to c++ integer data types. Any
feedback welcome: http://www.somacon.com/blog/page11.php


"The size of a char is guaranteed to be at least eight bits. The actual
size is an unspecified, system-dependent unit that can represent the
implementation's character set."

Wrong! The size of char is *always* 1 byte, which is not always 8 bits.
That is 1 byte is not always of 8 bits (but most times it is).


I don't see anything _wrong_ in the quoted passage; it doesn't mention
bytes, and since C++ adopts CHAR_MIN and CHAR_MAX from C it (frantic
handwaiving to distract academics) inherits C's guarantee of >= 8 bits.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #3
Alf P. Steinbach wrote:
I don't see anything _wrong_ in the quoted passage; it doesn't mention
bytes, and since C++ adopts CHAR_MIN and CHAR_MAX from C it (frantic
handwaiving to distract academics) inherits C's guarantee of >= 8 bits.

Yes. Perhaps he should replace the word "size" with the word "width" or
"bit-size".

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #4
On Sat, 27 Nov 2004 07:52:30 GMT, Shailesh Humbad
<no*****@nowhere.com> wrote in comp.lang.c++:
I wrote a short page as a quick reference to c++ integer data types.
Any feedback welcome: http://www.somacon.com/blog/page11.php


First, you confuse C99's <stdint.h> and <inttypes.h> headers, although
I admit the names are not terribly intuitive.

It is <stdint.h> that provides for various integer types.
<inttypes.h> provides macros for performing formatted character and
wide character input/output operations on the types.

I find your list of "Integer Data Types" likely to be very confusing
for someone not already familiar with the types. You list "short int"
and "long int" on the same line with int, when they are different
types. Then your list of synonyms is incomplete.

Finally, your recommendation is seriously flawed as far as I'm
concerned. The whole point of <stdint.h> is to allow writing code
that is portable to different implementations with different widths
for the integer types. Note that it is possible to write one's own
<stdint.h> for any conforming C++ compiler, with the possible omission
of the 64 bit types.

This is of more than academic concern to some of us, especially those
who work in multiple and less-common environments, such as digital
signal processors and embedded systems of various types.

About a year ago I wrote code to deal with data from a CAN bus
interface. The hardware level driver code is of course off-topic
here, but the code that packs data into packets to transmit, and
unpacks data from received packets to parse, was 100% standard.

The CAN packet in memory consisted of 128 bits in contiguous bytes.
The data in the middle 64 bits represented data that could be any
combination of 8-bit, 16-bit, and 32-bit values.

The processors on each end of the link had very different hardware
restrictions and integer types.

The master was an ARM microcontroller that requires alignment of
16-bit data to even addresses and 32-bit data to addresses evenly
divisible by four. The slave was a DSP with 16-bit bytes that can't
address 8-bit values in memory at all, and required 32-bit data to be
aligned to even addresses.

The ARM compiler came with a <stdint.h> header, the DSP compiler did
not so I wrote my own. Then I wrote packetizing, depacketizing, and
parsing routines using that <stdint.h> that compiled and executed
unchanged on both sides.

You might want to take a look at my page:
http://www.jk-technology.com/c/inttypes.html

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #5
Jack Klein wrote:
You might want to take a look at my page:
http://www.jk-technology.com/c/inttypes.html

Very interesting information, however regarding that page I think that
placing C and C++ information together can be very confusing since they
are two different languages with competing features (like Complex types)
and different characteristics (like const meaning) currently, and I
guess will be even more in the future.
In any case, only confusion can arise to a C or C++ newcomer when
reading this kind of mixed language information.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #6
Jack Klein wrote:
On Sat, 27 Nov 2004 07:52:30 GMT, Shailesh Humbad
<no*****@nowhere.com> wrote in comp.lang.c++:

I wrote a short page as a quick reference to c++ integer data types.
Any feedback welcome: http://www.somacon.com/blog/page11.php

First, you confuse C99's <stdint.h> and <inttypes.h> headers, although
I admit the names are not terribly intuitive.

It is <stdint.h> that provides for various integer types.
<inttypes.h> provides macros for performing formatted character and
wide character input/output operations on the types.

I find your list of "Integer Data Types" likely to be very confusing
for someone not already familiar with the types. You list "short int"
and "long int" on the same line with int, when they are different
types. Then your list of synonyms is incomplete.

Finally, your recommendation is seriously flawed as far as I'm
concerned. The whole point of <stdint.h> is to allow writing code
that is portable to different implementations with different widths
for the integer types. Note that it is possible to write one's own
<stdint.h> for any conforming C++ compiler, with the possible omission
of the 64 bit types.

This is of more than academic concern to some of us, especially those
who work in multiple and less-common environments, such as digital
signal processors and embedded systems of various types.

About a year ago I wrote code to deal with data from a CAN bus
interface. The hardware level driver code is of course off-topic
here, but the code that packs data into packets to transmit, and
unpacks data from received packets to parse, was 100% standard.

The CAN packet in memory consisted of 128 bits in contiguous bytes.
The data in the middle 64 bits represented data that could be any
combination of 8-bit, 16-bit, and 32-bit values.

The processors on each end of the link had very different hardware
restrictions and integer types.

The master was an ARM microcontroller that requires alignment of
16-bit data to even addresses and 32-bit data to addresses evenly
divisible by four. The slave was a DSP with 16-bit bytes that can't
address 8-bit values in memory at all, and required 32-bit data to be
aligned to even addresses.

The ARM compiler came with a <stdint.h> header, the DSP compiler did
not so I wrote my own. Then I wrote packetizing, depacketizing, and
parsing routines using that <stdint.h> that compiled and executed
unchanged on both sides.

You might want to take a look at my page:
http://www.jk-technology.com/c/inttypes.html


Hi Jack,

I'm glad you replied. I had skimmed through your page and found the
facts about odd/even alignment and addressing quite interesting . I
didn't claim my page was right or even good. It's just my effort to
understand what's going on. If even experts are conflicting with each
other on this topic, then forgive me for being hopeless. I will have to
look into the synonym issues further. I'll also have to use "width" or
"bit-size" where I'm using "size". You're right about the header files.
Unfortunately, neither stdint.h nor inttypes.h are included in MSVC
2003, so I haven't been exposed to either. At--

http://www.opengroup.org/onlinepubs/.../stdint.h.html

--it says "The <stdint.h> header is a subset of the <inttypes.h> header
more suitable for use in freestanding environments, which might not
support the formatted I/O functions. In some environments, if the
formatted conversion support is not wanted, using this header instead of
the <inttypes.h> header avoids defining such a large number of macros."

As for the recommendation to only use int, double, char, and bool until
otherwise required, you'll have to take that up with Bjarne Stroustrup.
I based the recommendation, and most of my other information, on what
it says in Section 4.1.1 of his book, _The C++ Programming Language_.

"For most applications, one could simply use bool for logical values,
char for characters, int for integer values, and double for
floating-point values. The remaining fundamental types are variations
for optimizations and special needs that are best ignored until such
needs arise. They must be known, however, to read old C and C++ code."

BTW, I'm writing a protocol that runs over TCP/IP. A major assumption
of TCP is that a byte is always 8-bits. ("... we constrain the length
of a segment to an integral number of 8-bit bytes." --
http://cs.mills.edu/180/reading/CK74.pdf -- "A Protocol for Packet
Network Intercommunication" -- Don't you love the Internet?) Therefore,
in order to define my protocol in C++ and increase its cross-platform
compilability, I need certainty regarding bit-widths. Would you
recommend using stdint.h for this purpose, and if so, where can I get it
from?

Thanks,
Shailesh
Jul 22 '05 #7
Shailesh Humbad wrote:
Unfortunately, neither stdint.h nor inttypes.h are included in MSVC
2003, so I haven't been exposed to either.

These are also not part of the C++98 standard, and VC++2003 supports
C++98 and C90 (and I guess it will never support C99).
BTW, I'm writing a protocol that runs over TCP/IP. A major assumption
of TCP is that a byte is always 8-bits. ("... we constrain the length
of a segment to an integral number of 8-bit bytes." --
http://cs.mills.edu/180/reading/CK74.pdf -- "A Protocol for Packet
Network Intercommunication" -- Don't you love the Internet?) Therefore,
in order to define my protocol in C++ and increase its cross-platform
compilability, I need certainty regarding bit-widths. Would you
recommend using stdint.h for this purpose, and if so, where can I get it
from?

stdint.h is not part of C++. To get the number of bits of a byte, for
any type you may use numeric_limits defined in <limits>.

More precisely, the number of bits of a byte in a given implementation is:
std::numeric_limits<unsigned char>::digits

unsigned char and char are guaranteed to have no padding bits in C++ (in
C99 char may have, but lets forget C here).

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #8
Ioannis Vranos wrote:

To get the number of bits of a byte, for

any implementation
you may use numeric_limits defined in <limits>.

More precisely, the number of bits of a byte in a given implementation is:
std::numeric_limits<unsigned char>::digits

unsigned char and char are guaranteed to have no padding bits in C++ (in
C99 char may have, but lets forget C here).


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #9
Ioannis Vranos wrote:
Shailesh Humbad wrote:
Unfortunately, neither stdint.h nor inttypes.h are included in MSVC
2003, so I haven't been exposed to either.


These are also not part of the C++98 standard, and VC++2003 supports
C++98 and C90 (and I guess it will never support C99).
BTW, I'm writing a protocol that runs over TCP/IP. A major assumption
of TCP is that a byte is always 8-bits. ("... we constrain the length
of a segment to an integral number of 8-bit bytes." --
http://cs.mills.edu/180/reading/CK74.pdf -- "A Protocol for Packet
Network Intercommunication" -- Don't you love the Internet?)
Therefore, in order to define my protocol in C++ and increase its
cross-platform compilability, I need certainty regarding bit-widths.
Would you recommend using stdint.h for this purpose, and if so, where
can I get it from?


stdint.h is not part of C++. To get the number of bits of a byte, for
any type you may use numeric_limits defined in <limits>.

More precisely, the number of bits of a byte in a given implementation is:
std::numeric_limits<unsigned char>::digits

unsigned char and char are guaranteed to have no padding bits in C++ (in
C99 char may have, but lets forget C here).


That gives me the number of bits of a byte at run time, but I need
certainty of it at compile time. Maybe I will try <boost/cstdint.hpp>.
I think that would be better than what I'm doing now, which is to use
the Microsoft-specific unsigned __int8 type.
Jul 22 '05 #10
Shailesh Humbad wrote:
Ioannis Vranos wrote:

stdint.h is not part of C++. To get the number of
bits of a byte, for any type you may use numeric_limits
defined in <limits>.

More precisely, the number of bits of a byte in a given
implementation is:

std::numeric_limits<unsigned char>::digits

unsigned char and char are guaranteed to have no padding
bits in C++ (in C99 char may have, but lets forget C here).


That gives me the number of bits of a byte at run time, but
I need certainty of it at compile time.


std::numeric_limits<unsigned char>::digits is surely - as a static
const member - known at compile-time? My compiler (gcc 3.3.3 Cygwin)
evidently thinks so:

<code>

#include <iostream>
#include <limits>

int main()
{
std::cout << "Bits per char = " << std::numeric_limits<unsigned
char>::digits;

int array[std::numeric_limits<unsigned char>::digits];
}

</code>

Output:

Bits per char = 8

Regards,

--
Lionel B

Jul 22 '05 #11
Lionel B wrote:
std::numeric_limits<unsigned char>::digits is surely - as a static
const member - known at compile-time? My compiler (gcc 3.3.3 Cygwin)
evidently thinks so:

<code>

#include <iostream>
#include <limits>

int main()
{
std::cout << "Bits per char = " << std::numeric_limits<unsigned
char>::digits;

int array[std::numeric_limits<unsigned char>::digits];
}

</code>

Output:

Bits per char = 8

You are right. However I couldn't make it work in #if statements in my
compilers, but he can also use the C90-subset CHAR_BIT defined in <climits>:
#include <iostream>
#include <climits>

int main()
{
#if CHAR_BIT == 8

std::cout<<"Byte 8 bits\n";

#else
std::cout<<"Byte not 8 bits!\n";

#endif
}


CHAR_BIT provides the bits of a byte in both C90 and C++98, but not in
C99, since in the later a char is allowed to have padding bits.

However, why should we mention C99 all the time in here? :-)

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #12
Ioannis Vranos wrote:
CHAR_BIT provides the bits of a byte in both C90 and C++98, but not in
C99, since in the later a char is allowed to have padding bits.

However, why should we mention C99 all the time in here? :-)

Actually it may work the same in C99 too, since in C99 is also mentioned
that it provides the number of bits of a byte.

So in summary regarding C++98, as far as I know, we can get the bits of
a byte by using numeric_limits<unsigned char>::digits (defined in
<limits>) and CHAR_BIT (defined in <climits> and <limits.h>).
Important: Since numeric_limits<T>::digits provides the bits without the
sign bit, numeric_limits<char>::digits does not provide the bits of a byte.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #13
Ioannis Vranos wrote:
Actually it may work the same in C99 too, since in C99 is also mentioned
that it provides the number of bits of a byte.

So in summary regarding C++98, as far as I know, we can get the bits of
a byte by using numeric_limits<unsigned char>::digits (defined in
<limits>) and CHAR_BIT (defined in <climits> and <limits.h>).
Important: Since numeric_limits<T>::digits provides the bits without the
sign bit, numeric_limits<char>::digits does not provide the bits of a byte.

.... when the char is implemented as signed.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #14

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

Similar topics

5
by: Charlie | last post by:
Hi, The description of Python always mentions "very high level dynamic data types". Now, I can't seem to find any examples of these (nothing described with this term anyway). Is this simply...
3
by: Shawn Berg | last post by:
Is the Integer data type deprecated? I am not sure what Integer data type to use in some of my classes. According to the URL below, if I am reading it correctly, it seems as though I should be...
1
by: J. Campbell | last post by:
I have a feeling that I'm doing things all ass-backwards (again ;-), and would like some advice. What I want to do is: put some data to memory and then access that memory space as an array of...
1
by: John Smith | last post by:
How do I use two different data types with a conditional operator ? I want to cout either a character or an integer depending on a certain condition. cout << ((IsThisTrue? char:integer) The...
11
by: theshowmecanuck | last post by:
As a matter of academic interest only, is there a way to programmatically list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to...
11
by: mesut demir | last post by:
Hi All, When I create fields (in files) I need assign a data type like char, varchar, money etc. I have some questions about the data types when you create fields in a file. What is the...
4
by: michael sorens | last post by:
I have successfully bound an XmlDocument to a DataGridView but all fields seem to be strings. I want to retrofit appropriate datatypes on some of the fields. Let me take this in 2 parts. Part...
8
MMcCarthy
by: MMcCarthy | last post by:
Type MemSize RetVal of VarType() Declaration Char Conversion Boolean 2b vbBoolean(11) CBool() Byte 1b vbByte(17) ...
4
by: Fester Bestertester | last post by:
Greetings, I'm still using MS Access 2K3 and DAO 3.6 objects (I know, I know, I'm a dinosaur)... I'm traversing through the fields of a table to retrieve their data types, thus, Dim...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.