Do strict literals guarantee that they will be adjacent to each other in memory?

Is it possible to guarantee that string literals are stored next to memory?

Therefore, below you need to get a conclusion hellohello

printf(3 + "%d");
printf("hello");
+5
source share
1 answer

No, string literals are stored wherever your compiler fantasizes. The fact that you declared two of them in consecutive lines does not matter. You cannot make any assumptions about where the compiler stores them.

The compiler can do all kinds of things. For example, if you write the following code

printf("hello");
printf("hello");

then the compiler is completely free to create only one literal. Or not.

+10
source

All Articles