You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言中const char*与static const char*的区别及异常行为疑问

Understanding 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* and static const char* differ only in where the pointer is stored, not where the string lives. Both point to persistent .rodata data.
  • For const integers: const int* (pointing to a local non-static int) points to stack data (invalid after function return), while static const int* points to static-storage data (valid for the program’s lifetime).

内容的提问来源于stack exchange,提问作者Cosmo Sterin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 10:57:26