3

I have started learning C++. And now I am learning about fundamental types. There I read that sizeof(char) = 1 always.

So my question is why is it so? Is there a intuitive reason or historical reason behind this. I mean they could've chosen any number like 2 or 3 but instead it is 1. So there must be a reason.

PS: I know that in C++ a byte is not necessarily 8-bits. I have read that:

The fundamental storage unit in the C + + memory model is the byte. A byte is at least large enough to contain any member of the basic execution character set and is composed of a contiguous sequence of bits, the number of which is implementation-defined.

Can we find the reason for sizeof(char) = 1 using the above quote and maybe using some other quote from the C++ standard.

Edit:

I think the following quote from the standard:

Objects declared as characters (char) shall be large enough to store any member of the implementation’s basic character set.

combined with the first quote given in my question can answer my question. Also, by implementation's basic character set i suppose they mean basic source character set and not basic execution character set. But i am not sure if this is correct. Also, i am not 100% sure if these 2 quotes explain why sizeof(char) = 1.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • 2
    there has to be a smallest unit and `char` is the name of it – M.M Jan 09 '22 at 08:40
  • 1
    Computers work on bytes (both in memory and on-disk) as the basic unit of addressability. `char`, `signed char` and `unsigned char` are the types that correspond to that basic unit. – Paul Hankin Jan 09 '22 at 08:48
  • Both `byte` and (`unsigned`) `char` have a size 1. Your reference probably is specifying how many bits are in a byte. There are computers with a different small word size like 10 bits. It must be able to contain elementary text (EBCDIC/ASCII) so at least 7 bits. Note that in Java `byte` is 1 byte, `char` is 2 bytes UTF-16. And then there are wide w_chart. – Joop Eggen Jan 09 '22 at 08:55
  • We don't need to deduce `sizeof(char) == 1`, it's part of the definition of `sizeof` (https://timsong-cpp.github.io/cppwp/n4868/expr.sizeof#1.sentence-4). Consider it an axiom. – StoryTeller - Unslander Monica Jan 09 '22 at 09:00
  • `char` is the basic unit of measure, so its size is one for the same reason that one metre is exactly one metre long. – molbdnilo Jan 09 '22 at 09:01
  • 1
    `Why is sizeof(char) = 1 always?` Because the standard says that it is. – eerorika Jan 09 '22 at 09:04
  • Becasue the C++ standard defines it as such : https://eel.is/c++draft/basic.fundamental, and https://eel.is/c++draft/expr.sizeof. sizeof is pretty explicit in its definition : "The result of sizeof applied to any of the narrow character types is 1" – Pepijn Kramer Jan 09 '22 at 09:08
  • I think the following quote from the standard: *""Objects declared as characters (char) shall be large enough to store any member of the implementation’s basic character set.""* combined with the quote given in my question can answer my question. Can anyone comment/answer if it is so. In the quote mentioned in this comment, by **implementation's basic character set** i suppose they mean *basic source character set* and not *basic execution character set*. – Jason Jan 09 '22 at 09:26
  • @Rick - No quote to support this, but sizeof surely returns the runtime size, not the compile time size. Otherwise you would not be able to build cross-compilers, where the execution happens on a different type of computer. – BoP Jan 09 '22 at 10:25
  • @BoP "*sizeof surely returns the runtime size, not the compile time size*" - that is absolutely wrong. `sizeof` is evaluated only at compile time. – Remy Lebeau Jan 09 '22 at 21:35
  • @Remy - In this context it was about whether the source character set influences the sizeof result. Even if it happens at compile time, the result reflects the system where the code is to run ("the runtime size", having no better term). – BoP Jan 09 '22 at 21:47

0 Answers0