Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size of string literal consisting of escaped characters

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

like image 231
smac89 Avatar asked Feb 18 '14 04:02

smac89


1 Answers

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.

like image 190
Shafik Yaghmour Avatar answered Sep 18 '22 22:09

Shafik Yaghmour