r/cprogramming • u/Mediocre-Trainer-132 • Feb 26 '26
What's the best way to make a string variable?
I generally use:
char* string_variable = "text here";
But if there's a better way, let me know.
12
Upvotes
23
u/bearheart Feb 26 '26
That's a pointer to a string literal, so correctly it should be
constqualified:const char* s = "foo";In fact, your compiler should generate a warning about this.
16
u/HashDefTrueFalse Feb 26 '26
C doesn't really have strings per se, just char arrays and pointers to chars. That's a perfectly fine way to do it as long as you don't need to modify it.
6
u/Brilliant-Orange9117 Feb 26 '26
In most cases I would prefer
const char string_var[] = "text here"for the following reasons:const. You're not allowed to write to them.const char * const.sizeofdoes the right thing e.g. if I want tomalloc()+memcpy()it.4
u/SmokeMuch7356 Feb 26 '26
For a read-only string that you will never modify, that works, although you should declare it
const:that way if you accidentally try to modify it:
etc., the compiler will yell at you.
For a string whose contents need to be modifiable but will never be longer than some known maximum length, use an array of
char:This variable can be written to:
For a string whose contents need to be modifiable and may need to grow beyond some known length, allocate space dynamically with
mallocorcalloc, and extend withrealloc:1
u/Suspicious_Ad_5096 Feb 28 '26
I just do string := “this is a string”
3
u/DawnOnTheEdge Feb 26 '26 edited Feb 26 '26
Absolutely all pointers to string constants should be declared
const! The only reason you are even allowed to leave theconstout is for backward-compatibility with old code written before theconstkeyword existed. Never do it in new code.If the pointer itself is not going to be changed to point to a different string constant, declare it as a
constpointer toconstdata, so:In C23, you can declare it
constexpr, which enables constant-folding optimization. Often it’s useful to know the length statically, and you can do that with:You can then use
string_var_lenas an array size or in other constant expressions.You might also want to store UTF-8 string constants. Most modern compilers will just let you save your source code in UTF-8 and it will work, but the most portable way to do this is:
This is guaranteed to get you a UTF-8 string constant, regardless of what your source and execution character sets are currently configured as.
2
u/markand67 Feb 26 '26
Its the worse. Assigning a non-const from a string literal.
string_variable[0] = 'a'; is already UB.There are no universal rule, it depends on the needs.
char foo[3456]are the cheapest, static analyzer will find out-of-bounds usage easily. no leak. however, large strings can break the stack especially on embeddedconst char *msg = "hello world";)char *should either point to a fixed size array, a unique character or a dynamically allocated string.2
u/pjl1967 Feb 26 '26
It depends. First, that should be:
since string literals are immutable. Second, there's also:
The first requires 5 bytes for the text and likely 8 bytes for the pointer totaling 13 bytes.
The second requires 5 bytes — period.
1
u/PepperHead41 Feb 27 '26