Unfortunately, i couldn't find an answer about my sizeof function question. So yet another topic about the sizeof function.
''The sizeof operator returns the number of bytes in a variable type, or the number of bytes occupied by an array.''
With the following code i expect the answer: 4, 3 and 7, which i believe are the number of bytes of the corresponding strings
Serial.print("sizeof data is "); Serial.println(sizeof("data"));
Serial.print("sizeof lol is "); Serial.println(sizeof("lol"));
Serial.print("sizeof test123 is "); Serial.println(sizeof("test123"));
However the results are:
sizeof data is 5
sizeof lol is 4
sizeof test123 is 8
Could someone explains why the results are acutally 1 higher then what i expected?
When the text "lol" is in memory, how would the software know how long it is ?
Some computer languages add a variable in front of it, so you get 3 lol in memory.
The C language puts a zero-terminator at the end, so you get lol 0x00.
It is not the ASCII character for zero "0", but it is really zero, all bits are zero. That can be written as "\0".
I would avoid the use of multicharacter literals because, "The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined."
it's more complicated than that in C++ until we get to C++23
You would get a warning about a multi-character character constant
you get 4 not because data has 4 letters but because you actually got an int and you were running likely on a 32 bits architecture. It would have been 2 on a UNO.
Multicharacter constants were inherited by C from the B programming language. Although not specified by the C standard, most compilers (MSVC is a notable exception) implement multicharacter constants as specified in B: the values of each char in the constant initialize successive bytes of the resulting integer, in big-endian zero-padded right-adjusted order, e.g. the value of '\1' is 0x00000001 and the value of '\1\2\3\4' is 0x01020304.
In C++, encodable ordinary character literals have type char, rather than int.