473,811 Members | 2,924 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 #1
58 4890
In article <11************ *********@q2g20 00cwa.googlegro ups.com>,
LuisC <lu************ *****@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?
Sort of. However:

a) char and iint are allowed to be the same size. sizeof(int) can be 1.
Apparently this happens on some DSPs. So you will not -always- save
memory by using char. Just usually.

b) whether char or int is faster to access on any particular system
is architecture dependant. It is not uncommon for modern architectures
to always fetch a full word and pick the char out of the word
(at a very low level), instead of having seperate logic to read
a single char. And modern architectures that can read a char at a time
sometimes have to bypass the cache and run special bus cycles that
load only the required character, because the memory hardware would
normally supply a full word. But it varies with the system, so
although we can say that "These days char might be slower
than int," you have to know your system very thoroughly to be sure
(and it might change with the next compiler version.)

So, like always: if it matters, measure. (And then throw out those
measurements and go hang out in comp.benchmarks for awhile in order
to learn about the 17 different ways in which your naive benchmark
was not measuring what you thought it was measuring!)
--
Programming is what happens while you're busy making other plans.
Feb 16 '07 #2
LuisC wrote:
What is better for holding small numbers in a program?
int.

I have one rule: just don't use char or even short for this purpose
unless memory is concerned or in some big DB.

Long ago, I did care of this... today, I don't. Especially for function
arguments.
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?
int is supposed to be the "native-ideal" integer. That's the one you
must choose for performance reasons when dealing with simple numbers.
Choose the other types (short or char) for memory reasons.

Still in the chapter of performance, some architectures must do more
operations to handle char correctly where int would just work well on
his own (thus faster).

--
R.N.
Feb 16 '07 #3

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?
In C, char is the appropriate type for character literals and strings,
(in the latter case, as an array of char). If your value needs more
than 16 value bits, use long. If it needs more than 32 bits, long long
is neccessary. Anything under 16 bits is guaranteed to fit in an int
and that would probably be the best choice, unless space is a real
issue. Whether access to char is slower than int is platform and
compiler dependent. In most cases, it's a case of premature
optimisation. Note also that whether a plain char is signed or
unsigned is implementation specified.

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

Never use plain char. For anything. I'm serious.

Regards,

-=Dave

Feb 16 '07 #5
In article <11************ **********@k78g 2000cwa.googleg roups.com>,
Dave Hansen <id**@hotmail.c omwrote:
>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.
Valid in C99, not invented yet by C89.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Feb 16 '07 #6
"Dave Hansen" <id**@hotmail.c omwrites:
Never use plain char. For anything. I'm serious.
What do you make your strings out of?
--
Ben Pfaff
bl*@cs.stanford .edu
http://benpfaff.org
Feb 16 '07 #7
"Dave Hansen" <id**@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.
Never use plain char. For anything. I'm serious.
Strings are arrays of plain char, and are widely used in stdio.

--
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.
Feb 17 '07 #8
Radamanthe wrote:
int is supposed to be the "native-ideal" integer. That's the one you
must choose for performance reasons when dealing with simple numbers.
That's not always true. A conforming compiler for 8-bit embedded
processors produces smaller and faster code for unsigned char than for
int, since int must be at least 16 bits in size.

If you use C99, <stdint.h>: int_fast8_t is the answer for signed
variables using no more than 8 bits, plus similar answers for some other
sizes, such as int_fast16_t.

--
Thad
Feb 17 '07 #9
"LuisC" <lu************ *****@gmail.com wrote:
# What is better for holding small numbers in a program?

Are you dealing with characters or integers? Program for maintenance.
If you're worried about the memory layout, use stdint.h integers
like uint8_t or use struct fields.

# 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?

There's an advantage to making your intent clear. You can even typedef
integers to make your intent clear.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I have no idea what you just said.
I get that alot.
Feb 17 '07 #10

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

Similar topics

4
2268
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
1876
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
3968
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
4652
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
2049
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
1661
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
5786
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
6438
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
9734
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
9607
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
10656
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
10397
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
10410
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
9214
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...
1
7674
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
6897
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
5564
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...

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.