C语言中const char*与static const char*的区别及异常行为疑问
const char*, static const char*, and the Int vs Char Behavior Gap Great question—you’re totally right to push back on that earlier answer, because string literals have unique behavior in C that’s easy to mix up with other const-qualified variables. Let’s break this down clearly:
First, your core point is 100% correct: string literals like "hello" are not stored on the stack. They live in the program’s read-only data segment (.rodata) and exist for the entire lifetime of the program. That’s why your example works flawlessly:
const char* f() { const char* hello = "hello"; return hello; } int main() { const char* hello_after = f(); printf("%s\n", hello_after); }
The pointer returned by f() points to a .rodata location that’s still valid in main—no segment fault, because the string itself never goes out of scope. This has nothing to do with the const qualifier on the pointer; it’s a fundamental property of string literals.
Why const char* and static const char* act similar (for string literals)
When you write const char* hello = "hello";, the pointer hello is a local stack variable—but it points to the global .rodata string literal. Adding static to the pointer (static const char* hello = "hello";) only changes where the pointer itself is stored: it becomes a static local variable (residing in .data or .bss instead of the stack). But it still points to the exact same long-lived string in .rodata. Since both pointers reference the same persistent data, their behavior in your example looks identical.
Why const int* and static const int* behave differently
Now contrast this with integer constants. Take this code:
const int* g() { const int x = 42; return &x; } int main() { const int* x_after = g(); printf("%d\n", *x_after); }
This is undefined behavior. The variable x is a local const int stored on the stack—when g() returns, the stack frame is destroyed, so x_after points to invalid memory.
But if you make x static:
const int* g() { static const int x = 42; return &x; } int main() { const int* x_after = g(); printf("%d\n", *x_after); }
This works perfectly. The static keyword changes x’s storage duration: it’s now stored in .data or .rodata (not the stack) and exists for the entire program run.
The key difference here is that there’s no equivalent of a string literal for integers. A const int x = 42; inside a function is just a local const-qualified variable (stack-allocated by default), whereas "hello" is a global literal with static storage duration, no matter where you use it.
Is this a GCC-specific hardcoding?
No—this isn’t a GCC trick; it’s defined by the C standard. The standard explicitly states that string literals have static storage duration (they live for the entire program), even when used inside a function. For other const-qualified variables, static storage duration is only granted if you explicitly use the static keyword.
To wrap up:
- For string literals:
const char*andstatic const char*differ only in where the pointer is stored, not where the string lives. Both point to persistent.rodatadata. - For const integers:
const int*(pointing to a local non-static int) points to stack data (invalid after function return), whilestatic const int*points to static-storage data (valid for the program’s lifetime).
内容的提问来源于stack exchange,提问作者Cosmo Sterin

