473,883 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is better? char or int?

What is better for holding small numbers in a program?

I know that char uses less memory, but, with 32 or 64 bits systems,
there are advantages (such as processing time) in using int instead of
char for small numbers?

Luis

Feb 16 '07
58 4909
Ian Collins wrote:
Harald van Dijk wrote:
Ian Collins wrote:
>Harald van Dijk wrote:
Richard Bos wrote:
"Malcolm McLean" <re*******@btin ternet.comwrote :
However if you have a large array of values, then it is reasonable to use
char to save space. An obvious case is 24-bit colour images. Using ints to
store every channel would just throw memory away.

OTOH, in that case I would never use either int _or_ char, but always
unsigned char (and if I had C99, uint8_t).

Why uint8_t? What benefits does it provide over unsigned char?

Less typing?
typedef unsigned char uchar;

(I'm dealing with some code that does this right now.)
At least uint8_t is standardised.
It wasn't a completely serious reply. I don't think code should use
such typedefs.

Feb 19 '07 #21
On Fri, 16 Feb 2007 14:12:03 -0800, Ben Pfaff wrote:
"Dave Hansen" <id**@hotmail.c omwrites:
>Never use plain char. For anything. I'm serious.

What do you make your strings out of?
Either (const char) or (unsigned char), depending on whether they're there
to sit pretty or be parsed.

The endless threads on the behavior of passing signed types to isalnum()
and the other ctype routines eventually convinced me to just use (unsigned
char) in the vast majority of circumstances. Similarly, I now typically
use unsigned types more often than not, since that's typically
more appropriate. Rare is the circumstance that I blithely toss an
(int) anywhere; closest I usually get are enum types.

I suspect Dave's harsh proscription was hyperbole. Still, instructive.
Feb 19 '07 #22
"=?utf-8?B?SGFyYWxkIHZ hbiBExLNr?=" <tr*****@gmail. comwrote:
Richard Bos wrote:
"Malcolm McLean" <re*******@btin ternet.comwrote :
However if you have a large array of values, then it is reasonable to use
char to save space. An obvious case is 24-bit colour images. Using ints to
store every channel would just throw memory away.
OTOH, in that case I would never use either int _or_ char, but always
unsigned char (and if I had C99, uint8_t).

Why uint8_t? What benefits does it provide over unsigned char? If
CHAR_BIT == 8, then unsigned char matches the requirements of uint8_t,
and if CHAR_BIT 8, then an implementation is not allowed to provide
uint8_t anyway.
To document that it's the eight-bittedness that I want, not the
character-holdingness (if you'll allow me to coin two nonce-words).

Richard
Feb 19 '07 #23
Harald van Dijk wrote, On 19/02/07 08:29:
Ian Collins wrote:
>Harald van Dijk wrote:
>>Ian Collins wrote:
Harald van Dijk wrote:
Richard Bos wrote:
>"Malcolm McLean" <re*******@btin ternet.comwrote :
>>However if you have a large array of values, then it is reasonable to use
>>char to save space. An obvious case is 24-bit colour images. Using ints to
>>store every channel would just throw memory away.
>OTOH, in that case I would never use either int _or_ char, but always
>unsigned char (and if I had C99, uint8_t).
Why uint8_t? What benefits does it provide over unsigned char?
Less typing?
typedef unsigned char uchar;

(I'm dealing with some code that does this right now.)
At least uint8_t is standardised.

It wasn't a completely serious reply. I don't think code should use
such typedefs.
There are times when you want a type of a specific size and, if you
don't have it, you will have to do recoding (I've come across it with 18
bit times). In such cases what better than a time that is guaranteed to
either meet your requirements or not exist?

One example of such a situation is what are called "wrap angles" where I
used to work. An angle would come in on an interface (how does not
matter) presented as a 16 bit number scaled to represent exactly 1
complete circle. I.e. 0x4000 is 90 degrees, 0x8000 is 180 degrees. This
gives you a few very good reasons for using an exact width type.
1) That is how the number comes in and you will have to deal with it as
such on the interface
2) It simplifies doing some geometry since you can just let the numbers
wrap (using unsigned types of course)
3) You can use tables of a sensible size (still leave enough space in
ROM for the program) to implement very fast trig operations.

An example for wanting an 8 bit unsigned integer type is if you are
dealing with an 8 bit greyscale image. Yes, even in this day and age
people deal with 8 bit greyscale images. If that is how your data comes
in then that is what you want to use.
--
Flash Gordon
Feb 19 '07 #24
Richard Bos wrote:
"=?utf-8?B?SGFyYWxkIHZ hbiBExLNr?=" <tr*****@gmail. comwrote:
Richard Bos wrote:
"Malcolm McLean" <re*******@btin ternet.comwrote :
However if you have a large array of values, then it is reasonable to use
char to save space. An obvious case is 24-bit colour images. Using ints to
store every channel would just throw memory away.
>
OTOH, in that case I would never use either int _or_ char, but always
unsigned char (and if I had C99, uint8_t).
Why uint8_t? What benefits does it provide over unsigned char? If
CHAR_BIT == 8, then unsigned char matches the requirements of uint8_t,
and if CHAR_BIT 8, then an implementation is not allowed to provide
uint8_t anyway.

To document that it's the eight-bittedness that I want, not the
character-holdingness (if you'll allow me to coin two nonce-words).
Fair enough. "unsigned char" has pretty much become synonymous with
"byte" to me, so to me it would make more sense.

<OTAdditionally , I forgot, but uint8_t can be provided on C90
systems even when CHAR_BIT 8, if it includes padding bits. POSIX
required implementations to provide uint8_t even when it was not
limited to 8-bit systems. </OT>

Feb 19 '07 #25
"=?utf-8?B?SGFyYWxkIHZ hbiBExLNr?=" <tr*****@gmail. comwrote:
Richard Bos wrote:
"=?utf-8?B?SGFyYWxkIHZ hbiBExLNr?=" <tr*****@gmail. comwrote:
Richard Bos wrote:
"Malcolm McLean" <re*******@btin ternet.comwrote :
However if you have a large array of values, then it is reasonable to use
char to save space. An obvious case is 24-bit colour images. Using ints to
store every channel would just throw memory away.

OTOH, in that case I would never use either int _or_ char, but always
unsigned char (and if I had C99, uint8_t).
>
Why uint8_t? What benefits does it provide over unsigned char? If
CHAR_BIT == 8, then unsigned char matches the requirements of uint8_t,
and if CHAR_BIT 8, then an implementation is not allowed to provide
uint8_t anyway.
To document that it's the eight-bittedness that I want, not the
character-holdingness (if you'll allow me to coin two nonce-words).

Fair enough. "unsigned char" has pretty much become synonymous with
"byte" to me, so to me it would make more sense.
It is in C, but byte is not synonymous with octet. Most image formats
are described in octets, not in system bytes, for obvious reasons.

Richard
Feb 19 '07 #26
LuisC wrote:
>
What is better for holding small numbers in a program?

I know that char uses less memory, but, with 32 or 64 bits systems,
there are advantages (such as processing time) in using int instead of
char for small numbers?
It is possible that, on some hardware, it is slower to write a char
than an int.

Consider a platform with a 32-bit bus, 8-bit chars, and 32-bit ints.
All memory access is done in 32-bit "words". To read an 8-bit char,
the 32-bit value is read, and 24 bits are discarded. However, in
order to write an 8-bit char, the system must first read the entire
32-bit value, modify the appropriate bits, and write the new 32-bit
value back to memory.

Such a platform would, if it permitted such operations, require two
reads to get an unaligned 32-bit value, and two reads plus two
writes to write an unaligned 32-bit value.

However, such matters are beyond the scope of the C language itself.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
Feb 19 '07 #27
On Feb 16, 4:12 pm, Ben Pfaff <b...@cs.stanfo rd.eduwrote:
"Dave Hansen" <i...@hotmail.c omwrites:
Never use plain char. For anything. I'm serious.

What do you make your strings out of?
uint8_t unless a wider type is required.

Regards,
-=Dave

Feb 19 '07 #28
On Feb 16, 6:18 pm, Keith Thompson <k...@mib.orgwr ote:
"Dave Hansen" <i...@hotmail.c omwrites:
On Feb 16, 1:41 pm, "LuisC" <luiscardozocar re...@gmail.com wrote:
What is better for holding small numbers in a program?
I know that char uses less memory, but, with 32 or 64 bits systems,
there are advantages (such as processing time) in using int instead of
char for small numbers?
Depends what you want. All of the following assumes you want a signed
integer type to hold values in the range of +/-127:
If you want the fastest type, use int_fast8_t.
If you want the smallest type, use int_least8_t.
If you want a type that is exactly 8 bits wide, use int8_t.

The above are available only if you have the <stdint.hhead er (i.e.,
either you have a C99 implementation, or a non-C99 implementation that
provides it). Note that if your implementation doesn't have
<stdint.h>, it's not too difficult to roll your own.
Valid point. But, it's harder to roll your own if you don't know
whether int is faster than signed char, for example.
>
Never use plain char. For anything. I'm serious.

Strings are arrays of plain char, and are widely used in stdio.
Not all strings...

The problem with plain char is that you don't really know what it is.
It's a Heisentype.

Well, I suppose you could write an introspective function, like

int is_plain_char_s igned(void)
{
char test = -1;

return test == -1;
}

But then you'd have to know when to use it, and what to do with the
result.

Regards,
-=Dave

Feb 19 '07 #29
On Feb 19, 2:45 am, William Ahern <will...@25than dClement.comwro te:
On Fri, 16 Feb 2007 14:12:03 -0800, Ben Pfaff wrote:
"Dave Hansen" <i...@hotmail.c omwrites:
Never use plain char. For anything. I'm serious.
What do you make your strings out of?
[...]
I suspect Dave's harsh proscription was hyperbole. Still, instructive.
Slight hyperbole. All sweeping generalizations are false.

Regards,
-=Dave

Feb 19 '07 #30

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

Similar topics

4
2269
by: Nobody | last post by:
Lets say I have a class that is only available if a specific DLL (a.dll) is present. I can't link to that DLL through lib files or my app will fail on any machine that doesn't have a.dll. So I do LoadLibrary()'s and keep function pointers in my wrapper class... Which is a better style? DWORD CClass::SomeFunc() { if (m_pfn != NULL)
20
1879
by: sugaray | last post by:
Hi, can somebody help me out with a better version of the following functions which only convert decimal integer to it's corresponding binary form. the problem i'm having now is that I can't figure out how to handle when 0 is passed as parameter in only one function, my code below have to add one more function to handle this situation, any help is appreciated, thanx.
86
3989
by: Randy Yates | last post by:
In Harbison and Steele's text (fourth edition, p.111) it is stated, The C language does not specify the range of integers that the integral types will represent, except ot say that type int may not be smaller than short and long may not be smaller than int. They go on to say, Many implementations represent characters in 8 bits, type short in
64
4668
by: ng5000 | last post by:
Hi, What's the point of a signed char? As I see it a char represents a character (not an integer, use an int type e.g. short int if you want an 8 bit number, or one of the new types, uint8 I think). I don't know of any character sets that use negatives, e.g. 65 is 'A' and -65 is 'a'?!? I'm sure I'm missing something, any ideas?
11
2061
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure containing pointers into a "flattened" bit stream. Here is my code (it dosen't work). I would be grateful for any feedback that helps fix this. My intention
8
1663
by: alternativa | last post by:
Hi, what would you say - which way of building a class is better? Why? /**** 1 ****/ class Person { public: Person(char *n, char *sn, int d); ~Person(); protected: char *name;
7
318
by: rsk | last post by:
char *i_reg_fname = "none"; -- Message posted using http://www.talkaboutprogramming.com/group/comp.lang.c/ More information at http://www.talkaboutprogramming.com/faq.html
89
5814
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the right thing to use for every var that holds the number of or size in bytes of things. * size_t should only be used when dealing with library functions.
3
6440
by: vainstah | last post by:
Hello Guys and Galls, To start off, I have reached the solution I was looking for, but I would like comments and feedback on the solution I have reached and tips/tricks on making it more elegant. I am not satisfied with the underlying machinery of the solution though. I am an advanced C programmer and most do object-based programming in C++. Please do not reply to this article with references to basic material or obvious tips. An...
0
9792
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
11142
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
10416
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
7971
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
5797
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
5991
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4612
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
4220
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3233
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.