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 4910
Keith Thompson wrote:
"Dave Hansen" <id**@hotmail.c omwrites:
Never use plain char. For anything. I'm serious.

Strings are arrays of plain char, and are widely used in stdio.
Out of curiosity, should <stdio.htreat strings as arrays of plain
char, or internally read them as arrays of unsigned char? The
difference could be important, because it would mean that snprintf(0,
0, "%s", s) might report a different length than strlen(s), if s
contains negative zeroes.

Feb 17 '07 #11

"LuisC" <lu************ *****@gmail.com wrote in message
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?
The main advantage of int is that it is clear to everyone that you are
dealing with an integer.

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.

Speed varies and can be hard to calculate. Normally a char will handled
internally in a register which is likely to be the same size as an integer
register. However memory access may well be slower because chars are not
aligned on hardware boundaries. On the other hand it may be faster because
you need to make fewer reads.
Feb 17 '07 #12
Harald van Dijk wrote:
Out of curiosity, should <stdio.htreat strings as arrays of plain
char, or internally read them as arrays of unsigned char?
<stdio.his a header. I assume you are referring to functions defined
in <stdio.h>.
The
difference could be important, because it would mean that snprintf(0,
0, "%s", s) might report a different length than strlen(s), if s
contains negative zeroes.
In both cases, the string is terminated with the terminating null
character, which is defined as the first byte containing all zero bits,
regardless of whether plain char is signed or unsigned and regardless of
the binary representation of negative values.

Would that require C-written functions for systems that have negative
zeros for plain char types to cast the char* to unsigned char* in order
to properly detect the terminating null character? I suspect so. Does
that render unportable the classic string copy loop?

Cross-posted to comp.std.c.

--
Thad
Feb 17 '07 #13
Thad Smith wrote:
Harald van Dijk wrote:
Out of curiosity, should <stdio.htreat strings as arrays of plain
char, or internally read them as arrays of unsigned char?

<stdio.his a header. I assume you are referring to functions defined
in <stdio.h>.
Yes.
The
difference could be important, because it would mean that snprintf(0,
0, "%s", s) might report a different length than strlen(s), if s
contains negative zeroes.

In both cases, the string is terminated with the terminating null
character, which is defined as the first byte containing all zero bits,
regardless of whether plain char is signed or unsigned and regardless of
the binary representation of negative values.
Thank you, I missed the fact that negative zero cannot ever match the
definition of a null character.
Would that require C-written functions for systems that have negative
zeros for plain char types to cast the char* to unsigned char* in order
to properly detect the terminating null character? I suspect so. Does
that render unportable the classic string copy loop?
It does, that's why for the functions declared in <string.hther e is
an explicit mention that the strings are to be treated as arrays of
unsigned char (in 7.21.1p3). I was wondering about <stdio.hbecau se
that paragraph does not apply to other functions, but I'm glad to see
that it's addressed, even if not explicitly, already.

Feb 17 '07 #14
"Malcolm McLean" <re*******@btin ternet.comwrote in message
news:Re******** *************** *******@bt.com. ..
>
"LuisC" <lu************ *****@gmail.com wrote in message
>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?
The main advantage of int is that it is clear to everyone that you are
dealing with an integer.
Exactly. Code so that your intent is clear; if you're dealing with numbers
that can fit in 16 bits or less, use int or unsigned int. Premature
optimization is the root of all evil. Don't play games with using char for
integers until you (a) have a performance problem and (b) can prove char
fixes it.
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.
OT: People still use 24-bit images in memory? Every general-purpose system
I've worked with used 32-bit RGB (with 8 padding bits) or RGBA in memory,
and optionally translated to 24-bit (or 48-bit, for X11) for certain disk or
network formats.
Speed varies and can be hard to calculate. Normally a char will handled
internally in a register which is likely to be the same size as an integer
register. However memory access may well be slower because chars are not
aligned on hardware boundaries. On the other hand it may be faster because
you need to make fewer reads.
char should always be aligned from a C programmer's perspective. If you're
dealing with one of the odd systems (e.g. Cray, early Alphas) that doesn't
have hardware support for 8-bit reads, you'll likely know that and can
introduce suitable alternate code conditionally used with #ifdefs. Most
modern systems have the same performance for char, int, and long -- and many
for long long as well.

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov

--
Posted via a free Usenet account from http://www.teranews.com

Feb 17 '07 #15
"Malcolm McLean" <re*******@btin ternet.comwrote :
"LuisC" <lu************ *****@gmail.com wrote in message
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?

The main advantage of int is that it is clear to everyone that you are
dealing with an integer.

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).

Richard
Feb 19 '07 #16
Richard Bos wrote:
"Malcolm McLean" <re*******@btin ternet.comwrote :
"LuisC" <lu************ *****@gmail.com wrote in message
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?
The main advantage of int is that it is clear to everyone that you are
dealing with an integer.

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.

Feb 19 '07 #17
Harald van Dijk wrote:
Richard Bos wrote:
>>"Malcolm McLean" <re*******@btin ternet.comwrote :
>>>"LuisC" <lu************ *****@gmail.com wrote in message

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?

The main advantage of int is that it is clear to everyone that you are
dealing with an integer.

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?

--
Ian Collins.
Feb 19 '07 #18
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.)

Feb 19 '07 #19
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.

--
Ian Collins.
Feb 19 '07 #20

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
10745
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...
1
10848
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10417
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
7972
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
7130
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
5798
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
4614
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
3234
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.