C++ Const Char data size Confusion

138 Views Asked by At

I'm working on a challenge to build a C++ text adventure game that will fit on a 1.44MB floppy disk. I'm using ASCII art for chapter images. Each ASCII art image is made out of 40 "cout" lines with 78 character in the line to form the image.

For example:

cout << "#############################################################################" << endl;

From my understanding, "cout" defines the contents as a "const char".

After some research I understand a char data type is 1 byte but I could not find a reliable answer to the size of a "const char".

If I'm understanding the data size right, the maths for one ASCII image should be 78(char)*40(lines) to get 3120 bytes or 3.12KB.

I'm unsure whether or not the characters themselves effects the size and if the addition of "const" increase the data size.

1

There are 1 best solutions below

0
Ted Lyngmo On

From my understanding, "cout" defines the contents as a "const char".

It does not. std::cout is an std::ostream for which there are a number of operator<< overloads of which one is:

std::ostream& operator<<(std::ostream& os, const char* s);

This merely reads the characters pointed at by s until a null terminator (\0) is encountered and outputs them to the device.

I understand a char data type is 1 byte but I could not find a reliable answer to the size of a "const char"

constness doesn't affect the size. The size of a const char is the same as the size of a non-const char - and the size is defined to be 1. You can print the result of sizeof(char) and sizeof(const char) to verify.

3120 bytes or 3.12KB

78*40 equals 3120, that's correct, but 3120 bytes is not 3.12 KB. KB is the old way of writing KiB, that is, kilobinary. 3120 is 3.046875 KiB or 3.12 kB.