Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many bytes does a #define string (string literal) take?

Tags:

c++

c

variables

#define STR "test1"

Why does this take 6 bytes?

sizeof(STR) = 6

like image 863
T.T.T. Avatar asked Mar 11 '26 03:03

T.T.T.


2 Answers

There is a trailing '\0' at the end.

like image 106
DigitalRoss Avatar answered Mar 12 '26 17:03

DigitalRoss


a #define just does a text replacement before compiling.

#define STR "test1"
sizeof(STR);

is actually seen by the compiler as

sizeof("test1");

now why is that 6 and not 5? because there's a null terminator at the end of the string.

like image 28
miked Avatar answered Mar 12 '26 18:03

miked