Hi, I am learning C as a hobby. Was looking at a textbook that I got at the fair. They talking about basic types etc and have the following code:

char ch;
int i;
i = 321;
ch = i;
printf("ch=%d\n",ch);

so what I did was to compile it to see what ch would be.
I get ch=65

So this is what i dont get. if i=321, then ch=i, why wont ch=321?

While compiling the code above I thought I would change the i value.

I tried i= 101 got ch=101; i=128 got ch=-128.

Is it something I am doing wrong when compiling or am I missing this whole char = 1 byte etc.

Thank you in advance.

Nothing wrong. A char type can hold values from -128 to 127 only, a total of 256 values. So, 321 is too large and the extra basically falls out the end.
321 - 256 = 65
128 - 256 = -128

An unsigned char can hold values from 0 to 255. Still 256 values.

when you want to hold integer value in a char..!!
use modulo operator to ensure the range in 0-255!!

like int a=300;
char ch=a%256;
so it will store 54 in ch.

modulo operator ensures the range...!!

why char stores on the range of 0-255
-------------------------------------------------

As an char takes 1 byte*(8 bits) in memory so it cant store only 256
distinct binary values (2^8).

As an char takes 1 byte*(8 bits) in memory so it cant store only 256
distinct binary values (2^8).

A char is one byte, but that doesn't make that it has always 8 bits, there are also machines where a byte consists of 6 bits for example.
If you want to get the number of bits for a char 'object', then you can use the CHAR_BIT macro, as defined in limits.h.
A byte is just an abstract term for the smallest addressable unit of your system.

The ASCII character set is technically a 7-bit character set, which means that it can only hold 2^7 = 128 characters portably.
However, there are several extensions to the ASCII character set, which extend it to use a full eight bit range, the upper range is not portable because it can produce different output on different systems (which for instance use different code page settings).

A char is one byte, but that doesn't make that it has always 8 bits, there are also machines where a byte consists of 6 bits for example.
If you want to get the number of bits for a char 'object', then you can use the CHAR_BIT macro, as defined in limits.h.
A byte is just an abstract term for the smallest addressable unit of your system.

The ASCII character set is technically a 7-bit character set, which means that it can only hold 2^7 = 128 characters portably.
However, there are several extensions to the ASCII character set, which extend it to use a full eight bit range, the upper range is not portable because it can produce different output on different systems (which for instance use different code page settings).

Actually i meant 2 things
1)1 byte is reserved in memory for a char
2)and in one byte maximum of 256 disticnt values can be generated
by taking (0,1) as per permutation and combination.
Thats it.i dinn't take the thing to machine level
Thanks anyways.

>and in one byte maximum of 256 disticnt values can be generated
But...but... This doesn't apply to 12-bit bytes :P

>and in one byte maximum of 256 disticnt values can be generated
But...but... This doesn't apply to 12-bit bytes :P

12-bit bytes exist?or its a joke?

12-bit bytes exist?or its a joke?

Nope, not a joke.
It's just: most of the time the word 'byte' is interpreted as an octet of bits (8 bits), but this is not fully correct, as a byte is an abstract term for the smallest addressable unit for your system.
If the smallest addressable unit is 12 bits on a certain system, then a byte on that particular system consists out of 12 bits.

Comments
Nice explanation of byte.
This article has been dead for over six months. Start a new discussion instead.