1. The first declaration:

     char buf[10] = "";
    

    is equivalent to

     char buf[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    
  2. The second declaration:

     char buf[10] = " ";
    

    is equivalent to

     char buf[10] = {' ', 0, 0, 0, 0, 0, 0, 0, 0, 0};
    
  3. The third declaration:

     char buf[10] = "a";
    

    is equivalent to

     char buf[10] = {'a', 0, 0, 0, 0, 0, 0, 0, 0, 0};
    

As you can see, no random content: if there are fewer initializers, the remaining of the array is initialized with 0. This the case even if the array is declared inside a function.

Answer from ouah on Stack Overflow
🌐
Reddit
reddit.com › r/cprogramming › initialize a char array?
r/cprogramming on Reddit: Initialize a char array?
January 9, 2024 -

How come this works:

Char name[20] = "Mary";

But this doesn't.

Char name[20];

Name[20] = "Mary";

Am I correct in that the above fails because I'm telling it to put "Mary" in the name[20] element? Is that how it's interpreted?

Is there a way to initialize char name[20] array separately from the declaration without using srtcpy()? Just wondering

Thanks

🌐
Quora
quora.com › How-do-I-initialize-a-char-array-in-C
How to initialize a char array in C - Quora
Answer (1 of 11): To initialize a char array in C, you can use either of the following methods: 1. Initializing at the time of declaration: > [code]char str[] = "Hello, World!"; [/code] In this method, the size of the array is automatically ...
Top answer
1 of 6
70

It's anyways bad practice to initialie a char array with a string literal.

The author of that comment never really justifies it, and I find the statement puzzling.

In C (and you've tagged this as C), that's pretty much the only way to initialize an array of char with a string value (initialization is different from assignment). You can write either

char string[] = "october";

or

char string[8] = "october";

or

char string[MAX_MONTH_LENGTH] = "october";

In the first case, the size of the array is taken from the size of the initializer. String literals are stored as arrays of char with a terminating 0 byte, so the size of the array is 8 ('o', 'c', 't', 'o', 'b', 'e', 'r', 0). In the second two cases, the size of the array is specified as part of the declaration (8 and MAX_MONTH_LENGTH, whatever that happens to be).

What you cannot do is write something like

char string[];
string = "october";

or

char string[8];
string = "october";

etc. In the first case, the declaration of string is incomplete because no array size has been specified and there's no initializer to take the size from. In both cases, the = won't work because a) an array expression such as string may not be the target of an assignment and b) the = operator isn't defined to copy the contents of one array to another anyway.

By that same token, you can't write

char string[] = foo;

where foo is another array of char. This form of initialization will only work with string literals.

EDIT

I should amend this to say that you can also initialize arrays to hold a string with an array-style initializer, like

char string[] = {'o', 'c', 't', 'o', 'b', 'e', 'r', 0};

or

char string[] = {111, 99, 116, 111, 98, 101, 114, 0}; // assumes ASCII

but it's easier on the eyes to use string literals.

EDIT2

In order to assign the contents of an array outside of a declaration, you would need to use either strcpy/strncpy (for 0-terminated strings) or memcpy (for any other type of array):

if (sizeof string > strlen("october"))
  strcpy(string, "october");

or

strncpy(string, "october", sizeof string); // only copies as many characters as will
                                           // fit in the target buffer; 0 terminator
                                           // may not be copied, but the buffer is
                                           // uselessly completely zeroed if the
                                           // string is shorter!
2 of 6
12

The only problem I recall is assigning string literal to char *:

char var1[] = "september";
var1[0] = 'S'; // Ok - 10 element char array allocated on stack
char const *var2 = "september";
var2[0] = 'S'; // Compile time error - pointer to constant string
char *var3 = "september";
var3[0] = 'S'; // Modifying some memory - which may result in modifying... something or crash

For example take this program:

#include <stdio.h>

int main() {
  char *var1 = "september";
  char *var2 = "september";
  var1[0] = 'S';
  printf("%s\n", var2);
}

This on my platform (Linux) crashes as it tries to write to page marked as read-only. On other platforms it might print 'September' etc.

That said - initialization by literal makes the specific amount of reservation so this won't work:

char buf[] = "May";
strncpy(buf, "September", sizeof(buf)); // Result "Sep"

But this will

char buf[32] = "May";
strncpy(buf, "September", sizeof(buf));

As last remark - I wouldn't use strcpy at all:

char buf[8];
strcpy(buf, "very long string very long string"); // Oops. We overwrite some random memory

While some compilers can change it into safe call strncpy is much safer:

char buf[1024];
strncpy(buf, something_else, sizeof(buf)); // Copies at most sizeof(buf) chars so there is no possibility of buffer overrun. Please note that sizeof(buf) works for arrays but NOT pointers.
buf[sizeof(buf) - 1] = '\0';
🌐
IBM
ibm.com › docs › en › i › 7.4.0
Initialization of character arrays
We cannot provide a description for this page right now
🌐
Delft Stack
delftstack.com › home › howto › initialize char array in c
How to Initialize Char Array in C | Delft Stack
February 2, 2024 - In this case, we declare a 5x5 char array and include five braced strings inside the outer curly braces. Note that each string literal in this example initializes the five-element rows of the matrix.
🌐
Sanfoundry
sanfoundry.com › c-tutorials-character-array-initialization
Character Array Initialization in C - Sanfoundry
December 31, 2025 - A character array is an array where each element holds a char type. It’s commonly used to store strings (a sequence of characters ending with a null character ‘\0’). ... You must include space for the null character (‘\0’). If you write char str[10] = “Sanfoundry”; → Error or warning: too many initializers...
🌐
GeeksforGeeks
geeksforgeeks.org › c language › how-to-initialize-char-array-in-struct-in-c
How to Initialize Char Array in Struct in C? - GeeksforGeeks
July 23, 2025 - When we create a structure instance (variable) in C that includes a char array, we can set an initial value for that char array directly using the list initialization.
Find elsewhere
🌐
Cppreference
en.cppreference.com › w › c › language › array_initialization.html
Array initialization - cppreference.com
1) string literal initializer for character and wide character arrays · 2) comma-separated list of constant(until C99) expressions that are initializers for array elements, optionally using array designators of the form [ constant-expression ] = (since C99)
🌐
SEI CERT
wiki.sei.cmu.edu › confluence › display › c › STR11-C.+Do+not+specify+the+bound+of+a+character+array+initialized+with+a+string+literal
STR11-C. Do not specify the bound of a character array initialized with a string literal - SEI CERT C Coding Standard - Confluence
Subclause 6.7.9, paragraph 14, of the C Standard [ISO/IEC 9899:2011], says: An array of character type may be initialized by a character string literal or UTF−8 string literal, optionally enclosed in braces.
🌐
Cplusplus
cplusplus.com › forum › general › 83475
Initializing Char Arrays - C++ Forum
I just wanted to know what is safer to use. Thank you, Ammo ... The safer option is to use C++ string objects. ... That's assignment, my friend. Initialisation is giving a variable/object a value during a declaration context. You can initialise an array by using a brace-enclosed initialiser-list:
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 136005-help-initializing-char-array.html
help with initializing a char array
CHEATA( bar, foo ) ; printf( "I cheated: %s\n", bar ); return 0; } Technically it's not assigning arrays, so no, you can't actually assign an array. But you can cheat your way around it. Quzah. Last edited by quzah; 03-21-2011 at 03:11 PM. Reason: added CHEATA · Hope is the first step on the road to disappointment. ... is it possible to initialise a char array separately from where it is declared without using either a for loop or initialising the array character by character?
🌐
Scaler
scaler.com › home › topics › what is character array in c?
What is Character Array in C? - Scaler Topics
October 10, 2025 - The strlen() function is present in the string.h header and is used to find the length of a string. It has the following syntax, This function returns the length of the string. A easy way to initialize a character array is using string assignment.
🌐
Cplusplus
cplusplus.com › doc › tutorial › ntcs
Cplusplus
In both cases, the array of characters myword is declared with a size of 6 elements of type char: the 5 characters that compose the word "Hello", plus a final null character ('\0'), which specifies the end of the sequence and that, in the second case, when using double quotes (") it is appended automatically. Please notice that here we are talking about initializing an array of characters at the moment it is being declared, and not about assigning values to them later (once they have already been declared).
🌐
Florida State University
cs.fsu.edu › ~myers › cgs3408 › notes › arrays.html
Array Basics
The input is stored in the character array and the null character is automatically appended. Note also that the & was not needed in the scanf call (word1 was used, instead of &word1). This is because the name of the array by itself (with no index) actually IS a variable that stores an address (a pointer). &nbsp; arrayinit.c -- example of array declarations and initializer ...
🌐
The Geek Stuff
thegeekstuff.com › 2011 › 12 › c-arrays
C Arrays Basics Explained with 13 Examples
March 6, 2014 - In the above declaration/initialization, we have initialized array with a series of character followed by a ‘\0’ (null) byte.
🌐
Scaler
scaler.com › topics › array-initialization-in-c
Array Initialization in C
August 16, 2022 - As we already know that array initialization starts from index zero, and the elements 100, and 200 will be at indexes 0, and 1 respectively. ... 3rd3rd index initializes with the value zero(0). So, to avoid zero values make it a priority to correctly define the size of the array while using ...
🌐
Heavydeck
heavydeck.net › blog › c-pointers-arrays-and-pitfalls
C pointers, arrays and initialization pitfalls | Heavydeck developer blog
September 23, 2021 - If we actually wanted to (for some reason) declare and initialize a pointer from an array literal, in C99 (not ANSI/C89) we would need to explicitly cast the array literal as… and array literal. /*Declare and initialize...*/ /*...char pointer from an array of numbers explicitly casted as such*/ const char *char_pointer_from_casted_array = (const char[]) {0x41, 0x41, 0x41, 0x41, 0x41, 0x00};