473,395 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Internal representation of char == unsigned small int?

When you say char in C, it internally means "an unsigned small integer
with 1-byte memory", right? More importantly, the internal
representation of char does not mean "int" as in
"machine-dependant/register-size dependant integer, which is normally
four-byte on 32-bit processors", right?

Nov 15 '05 #1
5 3461
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sathyaish wrote:
When you say char in C, it internally means "an unsigned small integer
with 1-byte memory", right? More importantly, the internal
representation of char does not mean "int" as in
"machine-dependant/register-size dependant integer, which is normally
four-byte on 32-bit processors", right?


Nope.

When you say char in C, you mean an object large enough to store any member of
the basic execution character set. You mean an object that is guaranteed to be
able to represent a range of unsigned values between 0 and 65535, and/or a
range of signed values between -128 and 127.

/How/ the compiler implements this object is up to the compiler. So long as it
meets the minimum requirements of a char, then any storage size is legal.

FWIW, by definition, a char takes 1 byte. However, that 1 byte /can/ be 8 or 9
or 32 or 64 or 128 or even 5000 bits wide, as required by the compiler.
/And/ an int object can have the same size as a char object (or, to put it
another way, a char object can have the same size as an int object).
- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFCv2gnagVFX4UWr64RAj35AJsEtCnI/c5kmX0+1dh7IkRySoMO1gCfe99o
p76e4oB8BjnsCvd+FxGOL84=
=RrKr
-----END PGP SIGNATURE-----
Nov 15 '05 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Lew Pitcher wrote:
Sathyaish wrote:
When you say char in C, it internally means "an unsigned small integer
with 1-byte memory", right? More importantly, the internal
representation of char does not mean "int" as in
"machine-dependant/register-size dependant integer, which is normally
four-byte on 32-bit processors", right?


Nope.

When you say char in C, you mean an object large enough to store any member of
the basic execution character set. You mean an object that is guaranteed to be
able to represent a range of unsigned values between 0 and 65535, and/or a

Gakkk. When will I learn to proofread?? I meant unsigned values between 0 and 255.

range of signed values between -128 and 127.

/How/ the compiler implements this object is up to the compiler. So long as it
meets the minimum requirements of a char, then any storage size is legal.

FWIW, by definition, a char takes 1 byte. However, that 1 byte /can/ be 8 or 9
or 32 or 64 or 128 or even 5000 bits wide, as required by the compiler.
/And/ an int object can have the same size as a char object (or, to put it
another way, a char object can have the same size as an int object).
--
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.


- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFCv2iUagVFX4UWr64RAlNmAJ42HxK3pd0VgoxYrBcI9b yxHvmvFgCffHM2
cQWmU/7JGCR4n+0zokWntwk=
=ACoz
-----END PGP SIGNATURE-----
Nov 15 '05 #3
"Sathyaish" <Sa*******@Yahoo.com> writes:
When you say char in C, it internally means "an unsigned small integer
with 1-byte memory", right? More importantly, the internal
representation of char does not mean "int" as in
"machine-dependant/register-size dependant integer, which is normally
four-byte on 32-bit processors", right?


C has three distinct one-byte types: char, signed char, and unsigned
char. "Plain" char has the same representation as either signed char
or unsigned char. The minimum ranges are -127..+127 for signed char,
0..255 for unsigned char.

--
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 #4
"Sathyaish" <Sa*******@Yahoo.com> wrote

When you say char in C, it internally means "an unsigned small integer
with 1-byte memory", right? More importantly, the internal
representation of char does not mean "int" as in
"machine-dependant/register-size dependant integer, which is normally
four-byte on 32-bit processors", right?

In English, we use glyphs to repesent characters. So capital A is a upwards
pointing triangle with a raised lower edge, capital B is a straight line
with two semi circles, and so on.

This is a good system for pencil and paper, but trying to store such shapes
directly on computer would be very wasteful. So instead we use a code - 10
means A, 11 means B, 12 means C, and so on.
Usually this code will be ascii, and usually characters will occupy 8 bits.
However you normally don't have to worry about this. C abstracts the
representation, and handles it for you. If you want an A, you just type char
ch = 'A';

Unfortunately the designers of C made a mistake. On their machine, bytes,
the smallest addressable unit of memory, happend to be 8 bits, which was
also perfect for the ascii code. So they decided to use the same word for a
character and a byte, "char". This causes huge problems when we try to go to
non-Latin languages, but we have to live with it.

The result is that you will often see "unsigned char" or more occasionally
"signed char" used as a small integer. You are not guaranteed 8 bits, though
this is by far the most common value. The macro CHAR_BIT gives you the
number of bits in a char.
Nov 15 '05 #5
"Malcolm" <re*******@btinternet.com> writes:
[...]
The result is that you will often see "unsigned char" or more occasionally
"signed char" used as a small integer. You are not guaranteed 8 bits, though
this is by far the most common value. The macro CHAR_BIT gives you the
number of bits in a char.


But you are guarantee *at least* 8 bits.

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

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

Similar topics

5
by: Mathieu Malaterre | last post by:
Hello, I have the following problem. I need to convert a unsigned char array into a string using only number (0-9) and '.'. The goal being to stored it on the minimal number of bytes. The...
10
by: Mantorok Redgormor | last post by:
I always see posts that involve the representation of integers, where some poster claims that the unerlyding representation of an integer doesn't have to reflect on the actual integer, for example:...
17
by: Mantorok Redgormor | last post by:
are all integers represented internally as just bit vectors? -- nethlek
14
by: Clint Olsen | last post by:
I was wondering if it's considered undefined behavior to use a member of a union when it wasn't initialized with that member. Example: typedef unsigned long hval_t; hval_t hval_init(void) {...
15
by: Robbie Hatley | last post by:
I was struggling to come up with a way to discern the actual bit patterns of the representations of C++ objects (esp. objects of small built-in types), and I came up with the following mess. But...
15
by: khan | last post by:
Hi, I read that pointer representation can non-zero bit pattern, machine specific.Compiler when comes accross value '0' in pointer context, converts it to machine specific null pointer...
10
by: Richard Tobin | last post by:
May all-bits-zero be a trap representation for a pointer? What if I calloc() space for a structure containing pointers? -- Richard -- "Consideration shall be given to the need for as many as...
5
by: anders.weitman | last post by:
Hi! I want to get the representation in memory of a variable of type double and put it in an array of four unsigned short int (I'm on a 32- bits Windows architecture so a double is 64-bits and...
1
by: krishna81m | last post by:
I am a newbie and have been trying to understand conversion from double to int and then back to int using the following code was posted on the c++ google group. Could someone help me out with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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...

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.