where many noticed that the size of the character buffer is defined as:

const int bufferSize = 255; char buffer[bufferSize+1]; 

why add one? After all, char can store 256 values, why not immediately define it like this:

 const int bufferSize = 256; char buffer[bufferSize]; 

same for character array:

 const int MAX = 256; char array[MAX]; 
  • It was necessary to ask those who so declared arrays. :) - Vlad from Moscow
  • :) Can it be declared this way because then you can use the same constant as the value of the last element of the array? Even if this feature will not be used in the code. Instead of creating 2 constants, one for the maximum size of the array, and the other for the highest index of the array. I only have this explanation. - Michael
  • one
    Perhaps the buffer is a string? String length plus zero bytes? - andreymal
  • Exactly! Thank! if you can delete a comment and post it in the answers? I like it when karma is over 15 (coming soon) - Michael

1 answer 1

After all, char can store 256 values, why not immediately define it like this:

The size of char has nothing to do with the size of the allocated buffer — at least a megabyte :)

This is highlighted for convenience, if you allocate bufSize+1 byte - you can store a string in it in bufSize characters (another one for the terminating zero character).

As for 255 specifically, it seems to me that the reasons here are rather psychological - like a round number (in the sense of a binary :)) number, plus usually this particular size cannot exceed the file name (for example, _MAX_FNAME in Windows).