473,806 Members | 2,319 Online
Bytes | Software Development & Data Engineering Community
+ 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 2382
po***********@g mail.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******@spamc op.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******@spamc op.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_Keit h) 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******@spamc op.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***********@g mail.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.or g> wrote:

Kenneth Brody <ke******@spamc op.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

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

Similar topics

19
6485
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 answer here? If a is converted to unsigned, then b>a. But, if b is converted to signed,then a>b. What's the correct coversion (what is the compiler supposed to do?)
3
31513
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 template <> struct to_unsigned<signed int> : public std::unary_function<signed int, unsigned int> { unsigned int operator()(signed int x) const { return x; } };
10
15668
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 standard doesn't even care what a normal char is (because signed and unsigned have equal behavior). For example if someone does this: unsigned char a = -2; /* or = 254 */
4
1042
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 etc and their implementation.the way compiler treats them and the scenarios where one
3
41952
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 0x%x\n",(unsigned char)b); }
5
7801
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 of bytes. For example, the .lib contains this function: int create(int id, int scale, unsigned char *image); In the wrapper DLL I have this function:
5
2854
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
11775
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg"); sizeof(reinterpret_cast<const char *>(p)); ----------------------------------------------------------------------- ---------------------------------------- the compiler tells me that "reinterpret_cast from type "const char * " to type "unsigned char *"...
8
2340
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 (*s1 == *s2) {
29
9999
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9597
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
10618
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
10110
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
9187
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...
0
5546
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
4329
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
2
3850
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
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.