Question

Code in question:

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    cout << sizeof("\n\r\t") << endl; // prints 4
    cout << strlen("\n\r\t") << endl; // print 3
    return 0;
}

I am confused because I always thought that it is standard that sizeof char is always 1 byte, but in the above code, it is printing 4.

Is there an explanation for this or is there an exception to this rule for escaped characters? Please enlighten me

Was it helpful?

Solution 2

This

"\n\r\t"

is a so-called string literal. It is stored in memory as a constant character array with terminating zero. Each escape character is one character.

So this string literal has three explicitly specified characters plus the terminatimg zero. In total there are four characters in the literal.

As for function strlen then it does not take into account the terminating zero. So it will report only three characters that were specified explicitly in the string literal.

The function strlen uses the terminating zero as the mark where it shall stop to count characters in a string.

As for the operator sizeof then it returns total memory in bytes occupied by an object. As your string literal has type const char[4] then sizeof will return 4. It is the total memory in bytes occupied by the string literal.

OTHER TIPS

The only difference here is that strlen does not include the null terminating character while sizeof will. The C documentation for strlen is actually better in this case since it includes the statement:

The null character is excluded from the length.

For some clarification a string literal is an array which includes a null terminating character, from the the draft C++ standard section 2.14.5 String literals paragraph 8 says:

Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration (3.7).

and paragraph 15 says:

[...]The size of a narrow string literal is the total number of escape sequences and other characters, plus at least one for the multibyte encoding of each universal-character-name, plus one for the terminating ’\0’.

and sizeof applied to an array will give you the total number of bytes in the array from section 5.3.3 Sizeof paragraph 3:

[...]When applied to an array, the result is the total number of bytes in the array. This implies that the size of an array of n elements is n times the size of an element.

Strings are null terminated hence there is the null character \0 at the end.

\n, \r, \t, and \0 are all a single byte in size and thus make 4 bytes in total!

To take an excerpt from the selected answer to this question which dovetails quite well:

The string literal has the type 'array of size N of [const] char' where N includes the terminal null.

Remember, arrays do not decay to pointers when passed to sizeof.

By calling sizeof() on a string literal, you are literally trying to find the size of the string literal in memory.

This included the null terminating character which is automatically appended to the string literal by your compiler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top