Public Group

# C programming 256 byte buffer significance?

This topic is 4132 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello. I'm pretty new to programming and I just wanted to ask this question that has been bugging me for a little while. As I've been going through some tutorials on the C language, I've come across the number 256 a lot, relating to character arrays (char string[256]) or buffers ( fgets(buffer,256,fp); ). This might sound dumb, but is 256 some kind of special number? I know you can have character arrays larger than 256, and you can read more data from a file than 256 bytes, so I was just wondering why I'm seeing it crop up so much. Thanks, and sorry if this is posted in the wrong place. ~Don

##### Share on other sites
256 is a power of two. Because of the binary nature of computers, powers of two have various forms of significance in various situations. They're also usually considered "nice" numbers by programmer folks for the same reason.

An array of 256 elements can be indexed by values 0-255, or exactly the value range of a typical byte. 256 was likely chosen for the "niceness" factor, however, as the performance considerations involved are effectively zero.

##### Share on other sites
It's just a nice round number.
If you write it in binary, it looks like 10000000 (or 2^8)

So it's for the same reason you tend to prefer powers of ten in normal decimal numbers. (You'd rather talk about a chunk of 1000 objects than, say, 984)

##### Share on other sites
It is also true that on many older systems, and small embedded devices, there was a performance gain to be had from allocating memory in power of 2 chunks.

##### Share on other sites
Quote:
 Original post by jpetrieAn array of 256 elements can be indexed by values 0-255, or exactly the value range of a typical byte. 256 was likely chosen for the "niceness" factor, however, as the performance considerations involved are effectively zero.

There could be legitimate performance reasons, like it fits nicely in a cache line, or it results in less memory fragmentation. But that's true in general.

##### Share on other sites
C tends to be somewhat low-level, and as such is very apealing to developers with assembly background or other low-level design and implementation techniques.

As such, they are absolutely in love with binary logic operations (shift, rotate, negate), as well as carefully structuring the data in memory.

As a result of this, you'll see lots of powers of two (or decremented by one, or negated, or some other tranformation). This is just a consequence of binary logic the computers use.

256 on itself has no meaning. It might be suitable for some specific hardware or architecture, but number itself as such isn't relevant.

The only significance it does have is that the size of such array can be stored in exactly one byte.

Another historical reason could be Pascal strings. Those were represented with leading byte (8-bits) indicating the number of characters that follow. With Delphi succession, other variants were offered, which could use 16 or 32-bit (2 and 4 byte) length specification, allowing for much longer strings.

As with many things, history leaves its mark. BTW: How much is baker's dozen?

##### Share on other sites
256 is the size of the array to hold the entire ascii table. If you're making a lookup table for chars, you would use an array of size 256.

- Goishin

##### Share on other sites
Quote:
 Original post by AntheusAs with many things, history leaves its mark. BTW: How much is baker's dozen?

13. The baker makes enough dough for 12 loaves of bread, but he will always have a bit left over which he'll make into 1 extra loaf.

Ed

##### Share on other sites
Quote:
Original post by Degra
Quote:
 Original post by AntheusAs with many things, history leaves its mark. BTW: How much is baker's dozen?

13. The baker makes enough dough for 12 loaves of bread, but he will always have a bit left over which he'll make into 1 extra loaf.

Ed

I'd heard that the origin of the bakers dozen came from the fact that at one time in history, a baker could be put to death for selling underweight bread, so always included an extra loaf in with his dozen just to make absolutely sure he was safe.

##### Share on other sites
Quote:
 Original post by Goishin256 is the size of the array to hold the entire ascii table. If you're making a lookup table for chars, you would use an array of size 256. - Goishin

There are only 128 elements in ASCII. You are probably thinking about one of the many ASCII extensions, but the only reason that these have 256 symbols is because it's a nice number which can be stored in one byte on modern computers (i.e. because of the previous reasons mentioned in this post).

Also the OP talks about the length of string, not the number of possible characters so it doesn't really matter. I have seen 1028 (2^10) and 4096 (2^12) being used too when getting larger inputs (files for instance).

1. 1
2. 2
Rutin
25
3. 3
4. 4
5. 5

• 10
• 10
• 13
• 20
• 14
• ### Forum Statistics

• Total Topics
632946
• Total Posts
3009363
• ### Who's Online (See full list)

There are no registered users currently online

×