Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sizeof(char)

Reply
Thread Tools

sizeof(char)

 
 
Olaf \El Blanco\
Guest
Posts: n/a
 
      04-13-2006
How many bits we need to save the "number" 1 in a C text file? 00000001? 1
byte?
And the "number" 12? Is also a byte? 00001100?
And the "number" 300?


 
Reply With Quote
 
 
 
 
Vladimir S. Oka
Guest
Posts: n/a
 
      04-13-2006

Olaf El Blanco wrote:
> How many bits we need to save the "number" 1 in a C text file?


Please clarify what you mean by the above, and also how it relates to
the subject. Particularly, what do you mean by "number"? A `int`, a
`short int`, a `long`, a `double`?

sizeof(char) == 1

Always, by definition (C refers to it as 1 byte). The number of bits in
a byte depends on the implementation, and is found in <limits.h> in a
macro CHAR_BIT (the minimum required is .

> 00000001? 1 byte?
> And the "number" 12? Is also a byte? 00001100?
> And the "number" 300?


I don't know whether this is what you're looking for, but in C, all
undecorated integer constants are of type `int`. The range you can
store in an `int` is also found in <limits.h> (INT_MIN and INT_MAX).
How many bytes you need to store an `int`, you'll find if you evaluate:

sizeof(int)

So, 1, 12, and 300, as shown above will all take `sizeof(int)` bytes to
store.

 
Reply With Quote
 
 
 
 
ranjmis
Guest
Posts: n/a
 
      04-13-2006

Olaf El Blanco wrote:
> How many bits we need to save the "number" 1 in a C text file? 00000001? 1
> byte?


what is this "C text file"
really the statement is confusing me. where & in what format exactly
you want to save the "number".

> And the "number" 12? Is also a byte? 00001100?
> And the "number" 300?


can you please elaborate what are you trying to achieve

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      04-13-2006
It is better to include the subject in the body of your text as well as
the subject
line.

Subject: sizeof(char)

byt definition sizeof(char) is 1. Always. Even if chars happen to be 32
bits
(it happens) sizeof(chr) is 1.


Olaf El Blanco wrote:
> How many bits we need to save the "number" 1 in a C text file? 00000001? 1
> byte?


typically one byte but it wouldn't normally be 00000001. For instance a
common encoding is ASCII and in that '1' is 00110001 (ask ANSI if you
want to know why
). Other encodings might you a different bit pattern (EBCDIC) or
more bytes
(eg. various Unicode encodings).

> And the "number" 12? Is also a byte? 00001100?


in a text file it will be typically two characters which is often two
bytes '1' and '2'
with ASCII bit patterns of 00110001 00110010. Again other encodings
might be less compact.

> And the "number" 300?


three characters

If you really *have* to make a small file then consider binary. Don't
do this lightly
as it makes your file harder to read. Disk space is cheap so I'd nearly
always
use a textual representation. Many data files are XML encoded.


--
Nick Keighley

 
Reply With Quote
 
Olaf \El Blanco\
Guest
Posts: n/a
 
      04-13-2006
FILE * fnumbers;
fnumbers=fopen("numbers", "w");
for (n=0; n<=MAX; n++) {
fprintf (fnumbers, "%d ", n);

All those numbers will be saved like chars, no?
0 --> 1 byte --> 00000000
1 --> 1 byte --> 00000001
..
..
..
OK.

But when n=257... What happend?

Sorry, I'm sure the question is stupid... But I need to understand this king
of things... Thanks.




 
Reply With Quote
 
Vladimir S. Oka
Guest
Posts: n/a
 
      04-13-2006

Olaf "El Blanco" wrote:
> FILE * fnumbers;
> fnumbers=fopen("numbers", "w");


Here you open the file as a *text* file.

> for (n=0; n<=MAX; n++) {
> fprintf (fnumbers, "%d ", n);


Presumably `n` is of integer type: `char`, `int`, ...?

>
> All those numbers will be saved like chars, no?


No, not like `char`s, as in the C type `char`. Rather, they will be
output as characters, as in *text* representation of the number. So, an
integer with value 32 will be output as a character sequence "32".

Also note that `printf()` integer arguments are of type `int`, and the
parameters you pass will be converted accordingly.

> 0 --> 1 byte --> 00000000
> 1 --> 1 byte --> 00000001


No, you get "0", "1", and so on...

> But when n=257... What happend?


The above hopefully makes you realise that in case n is 257, you'll get
"257" into your file.

> Sorry, I'm sure the question is stupid... But I need to understand this king
> of things...


I think your confusion was caused by using abbreviation "char(s)" when
you really meant "character" (e.g., as in a numeric character in a text
file), and not the C type `char`. These are not really interchangeable.

Now, using "wb" in `fopen()`, you could open the file in binary mode,
and then you'd `fwrite()` stuff to it. In this case, what is stored in
the file is binary representation of your numbers. Say, if `n` is
`int`, and sizeof(int)==2, and n == 1, and CHAR_BIT==8, depending on
the endinanness of the underlying architecture, you'd get 0x00 0x01 or
0x01 0x00 in your, now binary, file "numbers".

 
Reply With Quote
 
ranjmis
Guest
Posts: n/a
 
      04-13-2006

Olaf "El Blanco" wrote:
> FILE * fnumbers;
> fnumbers=fopen("numbers", "w");
> for (n=0; n<=MAX; n++) {
> fprintf (fnumbers, "%d ", n);
>
> All those numbers will be saved like chars, no?
> 0 --> 1 byte --> 00000000
> 1 --> 1 byte --> 00000001
> .
> .
> .
> OK.
>
> But when n=257... What happend?


the quest is really getting interesting but it still lacks clarity like
you didn't tell us about value of MAX, data type of n

assuming n as int : at 257 i am sure fprintf will print 257 into the
file

>
> Sorry, I'm sure the question is stupid... But I need to understand this king
> of things... Thanks.


 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      04-13-2006
"ranjmis" writes:

> can you please elaborate what are you trying to achieve


Is it conceivable that he is trying to, gasp, *learn* something?


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-13-2006
"Vladimir S. Oka" <> writes:
[...]
> I don't know whether this is what you're looking for, but in C, all
> undecorated integer constants are of type `int`. The range you can
> store in an `int` is also found in <limits.h> (INT_MIN and INT_MAX).


Quibble: All undecorated integer constants whose values are in the
range INT_MIN..INT_MAX are of type int. The type of an undecorated
decimal integer constant is the first of int, long int, long long int
in which its value can be represented. (That's in C99; I think the
C90 rules are different.)

--
Keith Thompson (The_Other_Keith) kst- <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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-13-2006
"ranjmis" <> writes:
> Olaf "El Blanco" wrote:
>> FILE * fnumbers;
>> fnumbers=fopen("numbers", "w");
>> for (n=0; n<=MAX; n++) {
>> fprintf (fnumbers, "%d ", n);
>>
>> All those numbers will be saved like chars, no?
>> 0 --> 1 byte --> 00000000
>> 1 --> 1 byte --> 00000001
>> .
>> .
>> .
>> OK.
>>
>> But when n=257... What happend?

>
> the quest is really getting interesting but it still lacks clarity like
> you didn't tell us about value of MAX, data type of n
>
> assuming n as int : at 257 i am sure fprintf will print 257 into the
> file


More specifically, it will print the three characters '2', '5', and '7'.

--
Keith Thompson (The_Other_Keith) kst- <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57