In C99 you can do it using a compound literal in combination with memcpy

memcpy(myarray, (int[]) { 1, 2, 3, 4 }, sizeof myarray);

(assuming that the size of the source and the size of the target is the same).

In C89/90 you can emulate that by declaring an additional "source" array

const int SOURCE[SIZE] = { 1, 2, 3, 4 }; /* maybe `static`? */
int myArray[SIZE];
...
memcpy(myarray, SOURCE, sizeof myarray);
Answer from AnT stands with Russia on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-arrays
Arrays in C - GeeksforGeeks
Note: Arrays with static storage duration, such as a static array or a file-scope array, are initialized to zero if no explicit initializer is provided. Array initialization means assigning initial values to the elements of an array. We can ...
Published: 3 weeks ago
🌐
DigitalOcean
digitalocean.com › community › tutorials › initialize-an-array-in-c
Initialize an Array in C: Methods and Examples | DigitalOcean
Learn all methods to initialize arrays in C programming. Comprehensive guide covering static, dynamic, multidimensional arrays with code examples.
Discussions

Proper way of initialize an array
Option III is non-standard. Avoid it. I would generally prefer Option II over Option I. More on reddit.com
🌐 r/C_Programming
22
13
August 22, 2021
Initializing an array and then filling it with data
Arrays cannot be assigned to, but they can be initialized. You can initialize a 2d array like this: int x[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; More on reddit.com
🌐 r/cprogramming
7
5
July 10, 2023
Initializing a variable sized array?
Note what the error message says - variable lengths arrays can't be initialized. They must be declared only, ie: double A[rows][rows]; and then you'll have to insert the initial values you want yourself. More on reddit.com
🌐 r/C_Programming
7
4
May 1, 2016
How to initialize all elements in an array to 0
https://www.techiedelight.com/initialize-elements-array-same-value-c-cpp/ His examples you have to declare size of array. Same with the stackoverflow examples I just googled. More on reddit.com
🌐 r/cs50
3
1
August 27, 2020
Top answer
1 of 8
36

In C99 you can do it using a compound literal in combination with memcpy

memcpy(myarray, (int[]) { 1, 2, 3, 4 }, sizeof myarray);

(assuming that the size of the source and the size of the target is the same).

In C89/90 you can emulate that by declaring an additional "source" array

const int SOURCE[SIZE] = { 1, 2, 3, 4 }; /* maybe `static`? */
int myArray[SIZE];
...
memcpy(myarray, SOURCE, sizeof myarray);
2 of 8
17

No, you can't set them to arbitrary values in one statement (unless done as part of the declaration).

You can either do it with code, something like:

myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 27;
:
myArray[99] = -7;

or (if there's a formula):

for (int i = 0; i < 100; i++) myArray[i] = i + 1;

The other possibility is to keep around some templates that are set at declaration time and use them to initialise your array, something like:

static const int onceArr[]  = {  0,  1,  2,  3,  4,..., 99};
static const int twiceArr[] = {  0,  2,  4,  6,  8,...,198};
:
int myArray[7];
:
memcpy (myArray, twiceArr, sizeof (myArray));

This has the advantage of (most likely) being faster and allows you to create smaller arrays than the templates. I've used this method in situations where I have to re-initialise an array fast but to a specific state (if the state were all zeros, I would just use memset).


You can even localise it to an initialisation function:

void initMyArray (int *arr, size_t sz) {
    static const int template[] = {2, 3, 5, 7, 11, 13, 17, 19, 21, ..., 9973};
    memcpy (arr, template, sz);
}
:
int myArray[100];
initMyArray (myArray, sizeof(myArray));

The static array will (almost certainly) be created at compile time so there will be no run-time cost for that, and the memcpy should be blindingly fast, likely faster than 1,229 assignment statements but very definitely less typing on your part :-).

🌐
cppreference.com
en.cppreference.com › c › language › array_initialization
Array initialization - cppreference.com
When initializing an object of array type, the initializer must be either a string literal (optionally enclosed in braces) or be a brace-enclosed list of initialized for array members: 1) string literal initializer for character and wide character arrays
🌐
C For Dummies
c-for-dummies.com › blog
Initializing Arrays in C23 | C For Dummies Blog
January 13, 2024 - The array values[] has storage for 20 integers. These storage locations (elements) are defined, but not assigned values. The values could be anything; your code should never assume that all 20 elements are initialized to zero.
🌐
Reddit
reddit.com › r/c_programming › proper way of initialize an array
r/C_Programming on Reddit: Proper way of initialize an array
August 22, 2021 -

Lets say i wanna initialize an array of zeros.
Of course, one could do:

I)

int a[20];
for(int i = 0 ; i < 20; i++) { a[i]=0}

or, one could initialize one element, and the rest would be initialized to zero implicited

II)
int a[20] = {0};

i also found that you can skip the zero :

III)
int a[20] = {};

So, my question is, would the latter (II and III) be considered good practice?

🌐
C For Dummies
c-for-dummies.com › blog
Initializing an Array | C For Dummies Blog
The brackets in the assignment ... trick. Even so, if it works, it’s non-standard and I wouldn’t recommend it. The = { 0 } initialization format is available for array initialization, but I still prefer to use a loop to set clean all the elements’ values....
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_arrays.htm
Arrays in C
Arrays are used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. An array in C is a collection of data items of similar data type. One or more values same data type, which may be primary data types (int, float, char), or user-defined types such as struct or pointers can be stored in an array.
🌐
Programiz
programiz.com › c-programming › c-arrays
C Arrays (With Examples)
November 10, 2024 - In this tutorial, you will learn to work with arrays. You will learn to declare, initialize and access array elements of an array with the help of examples. An array is a variable that can store multiple values.
🌐
Scaler
scaler.com › home › topics › array initialization in c
Array Initialization in C
March 5, 2024 - In the above declaration of array in c, the size of the array is three. 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.
🌐
LabEx
labex.io › questions › how-to-declare-and-initialize-an-array-in-c-136083
How to Declare and Initialize an Array in C | LabEx
July 25, 2024 - This will initialize all elements of the array to 0. ... In this case, the first two elements are initialized with 10 and 20, respectively, and the last three elements are initialized to 0.
🌐
CS UIC
cs.uic.edu › ~jbell › CourseNotes › C_Programming › Arrays.html
C Programming Course Notes - Arrays
Place the initialization data in curly {} braces following the equals sign. Note the use of commas in the examples below. An array may be partially initialized, by providing fewer data items than the size of the array.
🌐
Study.com
study.com › computer science courses › computer science 111: programming in c
Array Initialization in C Programming - Lesson | Study.com
January 12, 2019 - There are other direct and simpler ... to initialize them. The lowest element of the array has its index pointing to 0, so if num is the name of the array then num[0] gives us the first element of the array....
🌐
MangoHost
mangohost.net › mangohost blog › initialize an array in c – syntax and examples
Initialize an Array in C – Syntax and Examples
August 3, 2025 - Forgetting null terminators in strings: Always account for the '\0' character · Array decay in function parameters: Arrays passed to functions become pointers · Assuming uninitialized arrays are zero: Only global/static arrays are automatically zeroed · Buffer overflows: Always validate array bounds, especially with user input · Mixing initialization methods: Be consistent within your codebase
🌐
CodeChef
codechef.com › learn › course › c › BSNOJS › problems › NRJAYK05
Array Initialization in C in undefined
Test your Learn C knowledge with our Array Initialization in C practice problem. Dive into the world of c challenges at CodeChef.
🌐
Log2Base2
log2base2.com › C › array › declaration-and-initialization-of-array-in-c.html
Declaration and initialization of array in c
In the case of array, we know exact array size while initializing it, so we should use for loop.
🌐
TutorialKart
tutorialkart.com › c-programming › c-initialize-array
How to Initialize Array in C Language? Examples
October 22, 2024 - C Function Declaration vs. Definition ... To initialize an Array in C programming, assign the elements separated by comma and enclosed in flower braces, to the array variable.
🌐
Netlify
cscnotes.netlify.app › initializing arrays
Initializing Arrays in C | CSC
The array numbers is initialized with the values 10, 20, 30, 40, and 50 using an initialization list. The size of the array is determined using the sizeof operator · Then used in a for loop to print the elements of the array.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-declare-integer-arrays-with-c-programming
Integer Array in C – How to Declare Int Arrays with C Programming
March 13, 2023 - The first way is to initialize the array during declaration and insert the values inside a pair of opening and closing curly braces, {}.
🌐
guvi.in
studytonight.com › c › arrays-in-c.php
Arrays in C Programming
An array can also be initialized at runtime using scanf() function. This approach is usually used for initializing large arrays, or to initialize arrays with user specified values.