473,782 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about char

I know that char will always be one byte, and that the size of a byte
might not be 8-bits on some computers.

Does that mean that an unsigned char might be able to hold numbers
larger than 255 on some computers?

I need to know as the program I've written relies on unsigned char to
act as an 8-bit byte, and I need any unsigned char holding 255 to
overflow to 0 when the ++ operator is applied.

Dec 10 '05 #1
9 1631
an************* *@googlemail.co m wrote:
I know that char will always be one byte, and that the size of a byte
might not be 8-bits on some computers.

Does that mean that an unsigned char might be able to hold numbers
larger than 255 on some computers?

I need to know as the program I've written relies on unsigned char to
act as an 8-bit byte, and I need any unsigned char holding 255 to
overflow to 0 when the ++ operator is applied.


the char type is defined to be large enough to hold one character of
the implementation' s basic character set (3.9.1.1). this may be
anything from 7 to 9 or even more bits in a single byte on exotic (old)
architectures.

as 8bit bytes are common on modern architectures you may probably
safely ignore the problem, but for real portability you would have to
use a bigger type and manually overflow at 255.

-- peter

Dec 10 '05 #2
an************* *@googlemail.co m wrote:
I know that char will always be one byte, and that the size of a byte
might not be 8-bits on some computers.

Does that mean that an unsigned char might be able to hold numbers
larger than 255 on some computers?

I need to know as the program I've written relies on unsigned char to
act as an 8-bit byte, and I need any unsigned char holding 255 to
overflow to 0 when the ++ operator is applied.

An unsigned char can be larger than 8 bits. So if you want it to be
portable, you can use the standard C99 typedef uint8_t (defined in
stdint.h). If you don't want to limit yourself to C99 compilers only,
you can use a typedef like this:

typedef unsigned char Uint8;

It might be even better if you replace 'unsigned char' with a fixed-size
type that is specific to your compiler. If you then compile on another
compiler, you will have to replace that type with a similar type
available on the new compiler, in order to make it compile. That would
lower the chance of your Uint8 getting the wrong size without anyone
noticing.

You could also use the C99 uint8_t, with a fallback for older compilers,
something like this:

#if defined(__STDC_ VERSION__) || && (__STDC_VERSION __ >= 199901L)
#include <stdint.h>
#else
typedef unsigned char uint8_t;
#endif

But in most cases, just using an int and resetting it to 0 when it
reaches 255 would be the cleanest solution. You shouldn't rely on
'tricks' like numbers wrapping around if you don't have to.
Dec 10 '05 #3
Tydr Schnubbis wrote:
an************* *@googlemail.co m wrote:
I know that char will always be one byte, and that the size of a byte
might not be 8-bits on some computers.

Does that mean that an unsigned char might be able to hold numbers
larger than 255 on some computers?

I need to know as the program I've written relies on unsigned char to
act as an 8-bit byte, and I need any unsigned char holding 255 to
overflow to 0 when the ++ operator is applied.

An unsigned char can be larger than 8 bits. So if you want it to be
portable, you can use the standard C99 typedef uint8_t (defined in
stdint.h). If you don't want to limit yourself to C99 compilers only,
you can use a typedef like this:

typedef unsigned char Uint8;

It might be even better if you replace 'unsigned char' with a fixed-size
type that is specific to your compiler. If you then compile on another
compiler, you will have to replace that type with a similar type
available on the new compiler, in order to make it compile. That would
lower the chance of your Uint8 getting the wrong size without anyone
noticing.

You could also use the C99 uint8_t, with a fallback for older compilers,
something like this:

#if defined(__STDC_ VERSION__) || && (__STDC_VERSION __ >= 199901L)
#include <stdint.h>
#else
typedef unsigned char uint8_t;
#endif

But in most cases, just using an int and resetting it to 0 when it
reaches 255 would be the cleanest solution. You shouldn't rely on
'tricks' like numbers wrapping around if you don't have to.


You obviously can't use C99-specific features portably in C++, so
disregard that part. The rest still applies. Sorry folks.
Dec 10 '05 #4
On 10 Dec 2005 12:15:34 -0800, "an************ **@googlemail.c om"
<an************ **@googlemail.c om> wrote:
I know that char will always be one byte, and that the size of a byte
might not be 8-bits on some computers.

Does that mean that an unsigned char might be able to hold numbers
larger than 255 on some computers?

I need to know as the program I've written relies on unsigned char to
act as an 8-bit byte, and I need any unsigned char holding 255 to
overflow to 0 when the ++ operator is applied.


In addition to what the others have said, there is a constant CHAR_BIT
defined in <limits.h> which you can use to determine how many bits one
char will hold.

--
Bob Hairgrove
No**********@Ho me.com
Dec 10 '05 #5

I've never fully understood the issue surrounding the use of char with
regards to trap representation. I've perused the standard but I'm not
sure if I solid handle on so called trap representation. In any event,
there's seem to be an argument on both sides with some purists saying
it can and others saying it cant.
That said, is it safe to state that if a computer has an 8-bit signed
char with 255 admissible values (-127 to 127), could the potential for
'trap representation' arise here or ..?

Dec 11 '05 #6
On 10 Dec 2005 12:33:40 -0800, "peter steiner" <pn*******@gmai l.com>
wrote in comp.lang.c++:
an************* *@googlemail.co m wrote:
I know that char will always be one byte, and that the size of a byte
might not be 8-bits on some computers.

Does that mean that an unsigned char might be able to hold numbers
larger than 255 on some computers?

I need to know as the program I've written relies on unsigned char to
act as an 8-bit byte, and I need any unsigned char holding 255 to
overflow to 0 when the ++ operator is applied.
the char type is defined to be large enough to hold one character of
the implementation' s basic character set (3.9.1.1). this may be
anything from 7 to 9 or even more bits in a single byte on exotic (old)
architectures.


The C++ language standard requires that the char types contain at
least 8 bits. 7-bit chars are not compatible with the C or C++
standard.
as 8bit bytes are common on modern architectures you may probably
safely ignore the problem, but for real portability you would have to
use a bigger type and manually overflow at 255.


Some of the most modern architectures are digital signal processors,
and many of them have 16 or 32 bit chars in their C and C++
implementations . So apparently the only "modern architectures" you
know about are used in desk top type computers.

--
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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 11 '05 #7
Jack Klein wrote:
On 10 Dec 2005 12:33:40 -0800, "peter steiner" <pn*******@gmai l.com>
wrote in comp.lang.c++:
an************* *@googlemail.co m wrote:
I know that char will always be one byte, and that the size of a byte
might not be 8-bits on some computers.

Does that mean that an unsigned char might be able to hold numbers
larger than 255 on some computers?

I need to know as the program I've written relies on unsigned char to
act as an 8-bit byte, and I need any unsigned char holding 255 to
overflow to 0 when the ++ operator is applied.
the char type is defined to be large enough to hold one character of
the implementation' s basic character set (3.9.1.1). this may be
anything from 7 to 9 or even more bits in a single byte on exotic (old)
architectures.


The C++ language standard requires that the char types contain at
least 8 bits. 7-bit chars are not compatible with the C or C++
standard.


where does it say that? i have been unable to find any more concrete
definition than the one paraphrased above.
as 8bit bytes are common on modern architectures you may probably
safely ignore the problem, but for real portability you would have to
use a bigger type and manually overflow at 255.
Some of the most modern architectures are digital signal processors,
and many of them have 16 or 32 bit chars in their C and C++
implementations .


that is the reason why i recommended to use another solution for truly
portable code. i think that developers targeting embedded platforms
know about that fact...
So apparently the only "modern architectures" you
know about are used in desk top type computers.


?

-- peter

Dec 11 '05 #8

fo*******@hotma il.com wrote:
I've never fully understood the issue surrounding the use of char with
regards to trap representation. I've perused the standard but I'm not
sure if I solid handle on so called trap representation. In any event,
there's seem to be an argument on both sides with some purists saying
it can and others saying it cant.
That said, is it safe to state that if a computer has an 8-bit signed
char with 255 admissible values (-127 to 127), could the potential for
'trap representation' arise here or ..?


the same standard clause (3.9.1.1) state that all bits in the object
representation participate in the value representation and that all
possible value bit pattern combinations represent numbers. in plain
english that means that all bits stored in the in-memory representation
of a char type are part of the number it represents, thus there is no
potential trap representation.

the way i always understood that it is exclusive to the char type that
it avoids this very problem.

-- peter

Dec 11 '05 #9
peter steiner wrote:

fo*******@hotma il.com wrote:
I've never fully understood the issue surrounding the use of char with
regards to trap representation. I've perused the standard but I'm not
sure if I solid handle on so called trap representation. In any event,
there's seem to be an argument on both sides with some purists saying
it can and others saying it cant.
That said, is it safe to state that if a computer has an 8-bit signed
char with 255 admissible values (-127 to 127), could the potential for
'trap representation' arise here or ..?
the same standard clause (3.9.1.1) state that all bits in the object
representation participate in the value representation and that all
possible value bit pattern combinations represent numbers.


Well, the wording in my copy of the standard is a little different:

[...] For character types, all bits of the object representation
participate in the value representation. For unsigned character types, all
possible bit patterns of the value representation represent numbers. [...]

As you can see, the guarantee that all bit patterns represent values is
restricted to unsigned char. Thus, the previous sentence just rules out
something like, e.g., a redundant parity bit in the representation or
padding. It does not rule out trap values.

in plain
english that means that all bits stored in the in-memory representation
of a char type are part of the number it represents, thus there is no
potential trap representation.
Nope, see above.

the way i always understood that it is exclusive to the char type that
it avoids this very problem.


Correct: the standard goes on to say:

These requirements do not hold for other types.
Kai-Uwe Bux

Dec 11 '05 #10

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

Similar topics

4
2806
by: Asif | last post by:
Hi there, I have been trying to understand the behaviour of char (*pfn)(null) for a couple of days. can some body help me understand the behaviour of char (*pfn)(null) in Visual C++ environment? The question is why this is legal char *ptr; char (*pfn)(null); ptr = pfn
7
5076
by: CoolPint | last post by:
While I was testing my understanding of Functioin Template features by playing with simple function templates, I got into a problem which I cannot understand. I would be very grateful if someone offered me a kind explanation. TIA Function template and specialization below works fine: template <typename T> T maximum (T a, T b) {
18
8258
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include <iostream> #include <fstream> #include <cstdlib> #include <string>
17
607
by: ThazKool | last post by:
Why is it that calling sizeof on a class that contains only one char& returns the number 4? Should it have a 0 size?
26
1840
by: Materialised | last post by:
Hi everyone, I seen the post by Rob Morris, and thought that I would double check that I was using pointers in the correct way. So I written the following string functions to test. I know soem can be iumplimented using the standard libary, but I just wanted to test writing my own functions. They work ok, but I would like some feed back on any issues you can see with them etc #include <stdio.h> #include <stdlib.h>
14
3075
by: mluvw47 | last post by:
Know the answers to the following? Line Contains 50 char *b, q, *r; 200 b = getbuf (); 201 q = *b; 212 R = anotherfunction (b); 213-2003 /* we want to use 'q' and 'r' here */ 2000 char * getbuf ()
2
1306
by: akshayrao | last post by:
could someone point me in the right direction regarding this question?? http://groups.google.com/group/programpara/browse_thread/thread/75b58c897f890930 thanks.
9
2269
by: happyvalley | last post by:
I just wonder how to pass arguments to this function with a char** void oldmain(int argv, char**argc) { ........ } void main(void) { int argv;
25
2266
by: Why Tea | last post by:
Thanks to those who have answered my original question. I thought I understood the answer and set out to write some code to prove my understanding. The code was written without any error checking. --- #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct {
5
1798
by: =?Utf-8?B?SmVzc2ljYQ==?= | last post by:
Hello, I have a pInvoke question. This is the C function that is exported from one of the C dll, extern __declspec(dllexport) IM_RET_CODE ST_import (IM_MODE mode, char *filename, ST_TYPES **st_type, ST_ERROR **st_error);
0
9479
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
10311
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
10146
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
9942
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...
1
7492
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
6733
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
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...
1
4043
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
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.