473,466 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Can I use unsigned char as int?

For instance, I'd like to use unsigned char as an 8-bit integer.
Can I use like

unsigned char a=0;
a++
Thanks a lot!

Nov 15 '05 #1
13 2348
po***********@gmail.com wrote on 01/08/05 :
For instance, I'd like to use unsigned char as an 8-bit integer.
Can I use like

unsigned char a=0;
a++


Technically, yes. The guaranteed minimum range is 0-255. But I don't
recommand this practice. char are not 'natural sized' and some extra
assembly code may be generated to deal with the reduced size... (extra
masquing, sign extension when signed etc.)

I recommend the use of int or unsigned int for a more efficient code.
However, small integers (like char) can be useful for arrays to reduce
the memory print, if the extra code is not a problem in terms of
performance. We often have to make a choice between execution
efficiency and memory size...

Another point. Defining a parameter as char is pointeless. It will
always be converted to an int and will use an int print in automatic
memory.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
Nov 15 '05 #2
Thanks a lot! I am cooperating with others.
other people have defined
#define Byte unsigned char

so I have to use it.

So is the following a better way?
Byte a;
unsigned short tmp;
if(tmp>256)
tmp = 0;
tmp ++
a = tmp;

Nov 15 '05 #3
Emmanuel Delahaye wrote:
[...]
Another point. Defining a parameter as char is pointeless. It will
always be converted to an int and will use an int print in automatic
memory.


Is this strictly true?

Given the following:

void foo(char c)
{
... do stuff with 'c' ...
}

void myfunc(void)
{
foo('a');
}

Assuming 32-bit ints and the ASCII character set, must the compiler
generate code that passes 0x00000061 to foo?

I have seen compilers which will load 0x61 into the low byte of a
register and push that register onto the stack, leaving the rest of
the register alone. Yes, it did push a 32-bit value, but the top
24 bits were in an undetermined state.

Were these compilers broken, as far as the standard goes?

And, strictly speaking, would it be legal for a C compiler to generate
code which pushed a 16-bit value on the stack on a system with 32-bit
integers?
--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Nov 15 '05 #4
"Kenneth Brody" <ke******@spamcop.net> wrote in message
news:42***************@spamcop.net...
....
Assuming 32-bit ints and the ASCII character set, must the compiler
generate code that passes 0x00000061 to foo?

I have seen compilers which will load 0x61 into the low byte of a
register and push that register onto the stack, leaving the rest of
the register alone. Yes, it did push a 32-bit value, but the top
24 bits were in an undetermined state.

Were these compilers broken, as far as the standard goes?
Probably not. I think Emmanuel only meant that physically the char value
still occupies as much on stack (when passed as an argument) as an int. Just
that.
And, strictly speaking, would it be legal for a C compiler to generate
code which pushed a 16-bit value on the stack on a system with 32-bit
integers?


Well, if it supports that 16-bit type (e.g. short) and can address a 16-bit
memory cell, why not?

Alex
Nov 15 '05 #5
Kenneth Brody <ke******@spamcop.net> writes:
Emmanuel Delahaye wrote:
[...]
Another point. Defining a parameter as char is pointeless. It will
always be converted to an int and will use an int print in automatic
memory.
Is this strictly true?


No.
Given the following:

void foo(char c)
{
... do stuff with 'c' ...
}

void myfunc(void)
{
foo('a');
}

Assuming 32-bit ints and the ASCII character set, must the compiler
generate code that passes 0x00000061 to foo?
No, if there's a mechanism for passing a single byte, it can be used
here. (This isn't the best example, since character constants are of
type int; replacing foo('a') with foo(obj), where obj is a char
object, would show what's going on more clearly.)

If there's no prototype in scope, or if the called function has a
prototype with "..." (such as printf), a char will be promoted to int,
but that doesn't apply in this case.
I have seen compilers which will load 0x61 into the low byte of a
register and push that register onto the stack, leaving the rest of
the register alone. Yes, it did push a 32-bit value, but the top
24 bits were in an undetermined state.

Were these compilers broken, as far as the standard goes?
There's no problem as long as the generated code behaves properly.
And, strictly speaking, would it be legal for a C compiler to generate
code which pushed a 16-bit value on the stack on a system with 32-bit
integers?


Yes.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #6
On Mon, 01 Aug 2005 16:30:30 -0400, Kenneth Brody
<ke******@spamcop.net> wrote:
Emmanuel Delahaye wrote:
[...]
Another point. Defining a parameter as char is pointeless. It will
always be converted to an int and will use an int print in automatic
memory.
Is this strictly true?


Only if there is no prototype or it is a variadic function (parameter
list ends with ... and those parameters are accessed using va_arg). So
for instance passing a char to printf() will indeed be promoted to an
int, but...
Given the following:

void foo(char c)
{
... do stuff with 'c' ...
}

void myfunc(void)
{
foo('a');
}
.... in this case foo() has a prototype in scope so it will be passed as
a char. Which may...
I have seen compilers which will load 0x61 into the low byte of a
register and push that register onto the stack, leaving the rest of
the register alone. Yes, it did push a 32-bit value, but the top
24 bits were in an undetermined state.
.... do that, or it might push a char onto the stack on some machines
which allow that, or it might pass the value in an 8 bit register, or
whatever the implementers felt was best. A compiler could even pass all
parameters as pointers and do the appropriate dereferencing in the
function as long as the code acts "as if" the parameters are passed by
value.
Were these compilers broken, as far as the standard goes?
No.
And, strictly speaking, would it be legal for a C compiler to generate
code which pushed a 16-bit value on the stack on a system with 32-bit
integers?


Yup. If the machine architecture allows it (it doesn't cause an
alignment problem) that's fine. For that matter, if you had a function
with prototype

void func(char a, char b, char c, char d);

the compiler could pack all 4 chars into a single register and pass that
in or push them all on the stack. Or it could allocate space on the
stack for all of the parameters and then write the values into that
space. It doesn't matter, as long as the calling system matches the
called code (which might be specified as a system calling convention)
the compiler can do whatever it likes.

Chris C
Nov 15 '05 #7
po***********@gmail.com wrote:
Thanks a lot! I am cooperating with others.
other people have defined
#define Byte unsigned char

so I have to use it.

So is the following a better way?
Byte a;
unsigned short tmp;
if(tmp>256)
tmp = 0;
tmp ++
a = tmp;


Cooperation need not mean subordination. Speak Out. #define is not the
right way to do this. Consider..

typedef unsigned char Byte;
Byte a;

unsigned short tmp; /* Are you missing the assignment? */
if (tmp > 256) /* The value of tmp is indeterminate (who knows) */
tmp = 0;
tmp++; /* What can you expect here? */
a = tmp;

I give up. What was the question?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #8


Kenneth Brody wrote:
Emmanuel Delahaye wrote:
[...]
Another point. Defining a parameter as char is pointeless. It will
always be converted to an int and will use an int print in automatic
memory.


Is this strictly true?

Given the following:

void foo(char c)
{
... do stuff with 'c' ...
}

void myfunc(void)
{
foo('a');
}

Assuming 32-bit ints and the ASCII character set, must the compiler
generate code that passes 0x00000061 to foo?

I have seen compilers which will load 0x61 into the low byte of a
register and push that register onto the stack, leaving the rest of
the register alone. Yes, it did push a 32-bit value, but the top
24 bits were in an undetermined state.

Were these compilers broken, as far as the standard goes?

And, strictly speaking, would it be legal for a C compiler to generate
code which pushed a 16-bit value on the stack on a system with 32-bit
integers?

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>


The Poster did not say, but why assume 32 bits. If he has an 8 bit cpu He should
use the smallest variable possible, especially is RAM is limited. For a 16 bit
X86 still a savings. For a 32 bit cpu most likely no help, and for some maybe
worse.
Nov 15 '05 #9
On Mon, 01 Aug 2005 21:23:11 GMT,
Keith Thompson <ks***@mib.org> wrote:

Kenneth Brody <ke******@spamcop.net> writes:
Emmanuel Delahaye wrote:
[...]
Another point. Defining a parameter as char is pointeless. It will
always be converted to an int and will use an int print in automatic
memory.


Is this strictly true?


No.


If compilers do it anyway (pass char values as int) it can have following
reasons:
- It is easier to keep the stack varables alligned if you pass only
32bit and 64bit items.
- Functions are still callable from old K&R style written programs,
that is, no prototype.
- Similarly, functions written in K&R style can still be called from
programs written using ANSI prototypes.

Villy
Nov 15 '05 #10
On Mon, 01 Aug 2005 13:28:01 -0700, poison.summer wrote:
Thanks a lot! I am cooperating with others.
other people have defined
#define Byte unsigned char

so I have to use it.

So is the following a better way?
A better way for what?
Byte a;
unsigned short tmp;
How is tmp set?
if(tmp>256)
tmp = 0;
tmp ++
a = tmp;


If tmp starts as 255 or 256 and UCHAR_MAX is 255 the final value of tmp
won't fit in a. What is this code supposed to do? In terms of code
efficiency you're probably better off using unsigned int than unsigned
short.

Lawrence
Nov 15 '05 #11
Neil Kurzman wrote:

Kenneth Brody wrote: [...]
void foo(char c)
{
... do stuff with 'c' ...
}

void myfunc(void)
{
foo('a');
}

Assuming 32-bit ints and the ASCII character set, must the compiler
generate code that passes 0x00000061 to foo?

[...] The Poster did not say, but why assume 32 bits. If he has an 8 bit cpu
He should use the smallest variable possible, especially is RAM is
limited. For a 16 bit X86 still a savings. For a 32 bit cpu most
likely no help, and for some maybe worse.


If I hadn't listed those assumptions, someone would have (rightfully)
pointed out that there are non-32-bit systems, and non-ASCII systems,
where 'a' is not 0x00000061.

Perhaps I should have said "given a system with 32-bit integers and
the ASCII character set..."?

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Nov 15 '05 #12
Lawrence Kirby wrote:

On Mon, 01 Aug 2005 13:28:01 -0700, poison.summer wrote:
Thanks a lot! I am cooperating with others.
other people have defined
#define Byte unsigned char

so I have to use it.

So is the following a better way?


A better way for what?
Byte a;
unsigned short tmp;


How is tmp set?
if(tmp>256)
tmp = 0;
tmp ++
a = tmp;


If tmp starts as 255 or 256 and
UCHAR_MAX is 255 the final value of tmp
won't fit in a. What is this code supposed to do? In terms of code
efficiency you're probably better off using unsigned int than unsigned
short.


I'm guessing that he wants the count to go up from 0
and then roll over after 255,
but I'm assuming that he's not expressing himself well.

/* BEGIN new.c */

#include <stdio.h>

typedef unsigned char Byte;

int main(void)
{
Byte a;
unsigned count, tmp;

tmp = a = 0;
for (count = 3; count != 0; --count) {
do {
++tmp;
tmp &= 0xff;
a = (Byte)tmp;
if (a == 255) {
printf("%u ", (unsigned)a);
}
if (a == 256) {
printf("%u ", (unsigned)a);
}
} while (a != 0);

}
putchar('\n');
return 0;
}

/* END new.c */
--
pete
Nov 15 '05 #13
pete wrote:

Lawrence Kirby wrote:

On Mon, 01 Aug 2005 13:28:01 -0700, poison.summer wrote:
Thanks a lot! I am cooperating with others.
other people have defined
#define Byte unsigned char

so I have to use it.

So is the following a better way?


A better way for what?
Byte a;
unsigned short tmp;


How is tmp set?
if(tmp>256)
tmp = 0;
tmp ++
a = tmp;


If tmp starts as 255 or 256 and
UCHAR_MAX is 255 the final value of tmp
won't fit in a. What is this code supposed to do? In terms of code
efficiency you're probably better
off using unsigned int than unsigned short.


I'm guessing that he wants the count to go up from 0
and then roll over after 255,
but I'm assuming that he's not expressing himself well.

/* BEGIN new.c */

#include <stdio.h>

typedef unsigned char Byte;

int main(void)
{
Byte a;
unsigned count, tmp;

tmp = a = 0;
for (count = 3; count != 0; --count) {
do {
++tmp;
tmp &= 0xff;
a = (Byte)tmp;


/* I think this way is more better */

++tmp;
a = (Byte)(tmp & 0xff);

--
pete
Nov 15 '05 #14

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

Similar topics

19
by: MiniDisc_2k2 | last post by:
Okay, here's a question about the standard. What does it say about unsigned/signed mismatches in a comparison statement: char a = 3; unsigned char b = 255; if (a<b) Now what's the real...
3
by: Siemel Naran | last post by:
Hi. Is there a way to convert the type signed int to the type unsigned int, char to unsigned char, signed char to unsigned char, and so on for all the fundamental integer types? Something like ...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
4
by: ravinderthakur | last post by:
hi all experts, can anybody explain me the difference between the unsigned char and char in c/c++ langugage. specifically how does this affects the c library fucntion such as strcat,strtok...
3
by: QQ | last post by:
Hello, Here is my simple program int main() { unsigned char a =0x81; char b = 0x81; printf("unsigned char = 0x%x(%d), char = 0x%x(%d)\n",a,a,b,b); printf("cast char to unsigned...
5
by: Stephen Cawood | last post by:
I'm trying to use a C++ .lib from C# (I tried the Interop group will no results). I have a working wrapper DLL (I can get back simple things like int), but I'm having issues dealing with an array...
5
by: ryanlee101 | last post by:
I am getting a exception error when I complie my code. The error is: - imageData 0x00000000 <Bad Ptr> type unsigned char * I think it is from when I declare some of my char variables
26
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");...
8
by: Steven | last post by:
Hello, everyone! I find a version of strcpy(), I don't know why it return the unsigned char value. Can I change it into return *s1-*s2? int strcmp(const char *s1, const char *s2) { while...
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.