# Thread: char[256] ... why?

1. ## char[256] ... why?

how come i see 256 as the number of bytes in character arrays so often?
Code:
`char * myChar[256];`
is there something magical that happens?

2. No. There is nothing magical about 256. 1, 2, 4, 8, 16, 32, 64, 128, 256,...1024, are all powers of 2^x. It is just a practice among programs since we deal with 0s and 1s.

Kuphryn

3. it's a common size

4. Generally, a char is 1 byte - 8 bits - 256 possible values.

In games and such it is wise to hold state information about all the possible values (for key presses and things). You can then reference a state based on it's ASCII value.

Did I make enough sense with that?

5. you only need 1 byte to do that... not 256.
so its just a convention to use char name[256]; ?

6. No, you would need 256 bits if all you stored was if key was up or down. I've still always seen 256 bytes since it's probably faster to access.

It's best that you keep your data on word boundaries,
some compilers do this automatically.

Knuth gives out \$2.56 since 256 pennies is one hexadecimal dollar.

7. *goes crazy and doesn't understand*
a regular char type has is 1 byte.. .that's 255 or so diff. values it can store. my question is why do people use 256 bytes (not bits!!) so often.

8. You need to handle multiple keypresses.

Some implementations of malloc are optimized for
powers of 2.

BUFSIZ and the size of a virtual page
are usually powers of 2.

When you subtract a unsigned number by 256 all your
doing is clearing the 8 bit. Doing an and instruction might
be faster on a really old processor. Well you can make your
code more confusing doing it

9. >>why do people use 256 bytes (not bits!!) so often.

Even though that would be the most size efficient, the smallest standard datatype in C/C++ is a char. There is no single bit variable type, and often times it's more work if you're trying to manipulate bits (and people are generally lazy).

10. >how come i see 256 as the number of bytes in character arrays so often?
The exact reason why 256 is used to so much is because the programmer chose to use it, probably because they felt it was not too much and not too little. I personally think that 256 is too small for a char array since they are usually used as buffers for input and you never know how much input there is.

-Prelude

11. i think prelude finally got what i was asking (i'm not good at explaining problems i think). thakns

Popular pages Recent additions