| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | [C] (Thanks)An interesting question |
Hi prabir! :-)
ps> Thanks a lot. Well Mr i want to know why then we prefer to store a
ps> int (small integer) in a char datatype ?
Depends. If you have a lot of them and they all have fairly small values
(0-255 for unsigned char, -128-127 for char which is signed for most
compilers), you can save space by using char instead of int. Ints are 32
bits most of the time, so an array of 4000 ints takes 4 times as much space
as an array of 4000 chars.
On modern CPUs and compilers, you don't really gain much from using types
that are smaller than int. The biggest use for char is for representing
strings as either char arrays or char pointers.
ps> For eg. in K&R there goes
ps> one example where the author tries to store the months , which are
ps> small integers(1, 2, 3..., 12) in a char array (char months[] =
ps> {1,2,3...12} )and after that does arithmetic on the elements of this
ps> array. How is this possible, after all elements in the array are
ps> characters not integers ? Can u pls explain me a bit .
A char is just a smaller integer type, there is no real connection to
characters that make up words. You can handle them just like integers, they
just can't hold big values.
Ciao
Pascal
--- Msged/LNX 6.1.1
* Origin: Beware of a dark-haired man with a loud tie. (1:153/401.2)SEEN-BY: 633/267 270 @PATH: 153/401 307 140/1 106/2000 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.