sizeof(char)

O

Olaf \El Blanco\

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

Vladimir S. Oka

Olaf said:
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
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.
 
R

ranjmis

Olaf said:
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
 
N

Nick Keighley

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.

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

Olaf \El Blanco\

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

Vladimir S. Oka

Olaf said:
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".
 
R

ranjmis

Olaf said:
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
 
K

Keith Thompson

Vladimir S. Oka said:
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.)
 
K

Keith Thompson

ranjmis said:
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'.
 
N

Nick Keighley

Olaf said:
FILE * fnumbers;
fnumbers=fopen("numbers", "w");
for (n=0; n<=MAX; n++) {
fprintf (fnumbers, "%d ", n);

as others have pointed out this prints a series of character encodings
of the numbers. This might be a more interesting program.

#include <stdio.h>

#define MAX 257

int main (void)
{
FILE *f;
int n;
f = fopen ("numbers", "w");
for (n = 0; n <= MAX; n++)
fprintf (f, "%c", n);
fclose (f);
return 0;
}

This will do more like you want. It will print a single character
(rather
than multiple characters) for each fprint() call. If a character is 8
bits then
it will output 0x0 for 256 and 0x1 for 257 (the 0x notation indicates a

hexadecimal value).

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.

It's ok, in a sense no genuine question is stupid. You've just got a
bit of a block
and in short while the "penny will drop" and you'll be wondering why
you ever
had a problem. A problem only occurs when people forget that this ever
happened to them and get sniffy with people who know less about a
particular
subject than they do.


--
Nick Keighley

"...the characters are legible and well-known, but when put
together they do not say anything that leaves an imprint
on the modern mind."

"Like instructions for programming a VCR?"

(Neal Stephenson "Snow Crash")
 
V

Vladimir S. Oka

Keith Thompson opined:
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.)

Thanks for that, Keith. I had one foot out the office door while
composing the above, as I was keen to start the longest weekend on
this small island, otherwise bereft of public holidays.
 
S

Simon Biber

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

If it's a text file, then you're probably storing a number as a sequence
of ASCII characters (or possibly another encoding). The number 1 will be
output as a single character, which is usually a single byte, having the
binary value 110001 and the decimal value 49.
And the "number" 12? Is also a byte? 00001100?

No, it's two characters, '1' and '2'. The first character probably has
binary value 110001 (49) and the second probably has binary value 110010
(50). I say "probably" because on some systems the character encoding
used may not be compatible with ASCII.
And the "number" 300?

The three characters '3', '0' and '0', in that order.

If you want to store numbers in a binary format, you can do that
instead. You then have to decide how many bytes you want to store for
each integer, and in what order to store the bytes.

For example:
fputc(val >> 24 & 255, file);
fputc(val >> 16 & 255, file);
fputc(val >> 8 & 255, file);
fputc(val >> 0 & 255, file);

The code above will output 4 bytes, each containing an 8-bit sequence of
the original value. The bytes are output in big-endian order. That means
that the most significant byte is output first, and the least
significant byte is output last. You can reverse the order of the 4
statements to make it output little-endian order instead.

If you want to store less bytes, just leave out the first statements. If
you want to store more bytes, add more statements and continue the
pattern, changing the shift value by 8 each time.

Simon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top