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

🌐
Cprogramming
cboard.cprogramming.com › cplusplus-programming › 12022-initializing-char-arrays-null.html
initializing char arrays to null
you can also try these to initialize just the first element of the array to null: char buffer[10]; buffer[0] = '\0';//pictoral null char, need the single quotes though buffer[0] = 0;//the integer zero when assigned to char is null char, the quotes and backslash escape char just make it abundantly ...
🌐
Cplusplus
cplusplus.com › forum › beginner › 248142
Is there problem to declare an empty char array?
TO OP: Whenever you write double quotes "", there is always a null character, that's how cstrings work (and for a reason). So when you used "" to initialize, the compiler assumed a null character to terminate. You could try initializing your array like this: char c[] = {}; and you will notice ...
🌐
Cppreference
en.cppreference.com › w › c › language › array_initialization.html
Array initialization - cppreference.com
1) string literal initializer for ... using array designators of the form [ constant-expression ] = (since C99) 3) empty initializer empty-initializes every element of the array...
🌐
Quora
quora.com › How-do-you-initialize-char*-to-an-empty-string-in-C
How to initialize char* to an empty string in C - Quora
Answer (1 of 8): An empty string in C - meaning one that would be a legal-formed string that would be regarded as a string of zero-length by the string.h string functions and other functions that operate on strings - is simply [code ]""[/code]. It is an array of [code ]char [/code]with a element,...
🌐
Delft Stack
delftstack.com › home › howto › initialize char array in c
How to Initialize Char Array in C | Delft Stack
February 2, 2024 - It’s possible to specify only the portion of the elements in the curly braces as the remainder of chars is implicitly initialized with a null byte value. It can be useful if the char array needs to be printed as a character string.
🌐
Jeremy Lindsay
jeremylindsayni.wordpress.com › 2017 › 10 › 07 › two-ways-to-initialize-an-array-in-c
Two ways to initialize an array in C | Jeremy Lindsay
October 7, 2017 - I don’t often post about C, but I’ve been programming a lot in the Arduino world recently, and thought I’d post a quick couple of tips on a small programming challenge I encountered. I needed to declare a character array (of length 64) – this is pretty simple.
Find elsewhere
🌐
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 determined by the size of the string literal. 2. Ini...
🌐
IBM
ibm.com › docs › en › i › 7.4.0
IBM Documentation
We cannot provide a description for this page right now
🌐
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...
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';
🌐
Northern Illinois University
faculty.cs.niu.edu › ~winans › CS501 › Notes › cstrings.html
C Strings
This function takes two arguments: 1) a pointer to a destination array of characters that is large enough to hold the entire copied string (including the null character), and 2) a pointer to a valid C string or a string literal.
🌐
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 ...
🌐
Quora
quora.com › How-do-you-initialize-a-char-array-with-null
How to initialize a char array with null - Quora
There are two different things should not be confused here: 1. to initialize a char array with null: means that it store a string of length cero. To do that, all you need to do is set to zero (end of string value) the first element of the array. ...
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Character array initialization. - Programming - Arduino Forum
March 11, 2016 - I backed into a knowledge of Arduino programming, although I have been programming in many other languages, all the back to 1962. But there is one structure that I hate, and that is the character array. I use String whe…