Different ways to Initialize all members of an array to the same value in C Last Updated : 10 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report An array is a collection of data that holds fixed number of values of same type. For example: if you want to store marks of 100 students, you can create an array for it. int num[100];How to declare an array in C?Data_type array_name[size_of_array]; For example, float num[10];Initializing all members of an array to the same value can simplify code and improve efficiency. Below are some of the different ways in which all elements of an array can be initialized to the same value: Initializer List: To initialize an array in C with the same value, the naive way is to provide an initializer list. We use this with small arrays. int num[5] = {1, 1, 1, 1, 1}; This will initialize the num array with value 1 at all index. We may also ignore the size of the array: int num[ ] = {1, 1, 1, 1, 1} The array will be initialized to 0 in case we provide empty initializer list or just specify 0 in the initializer list. int num[5] = { }; // num = [0, 0, 0, 0, 0]int num[5] = { 0 }; // num = [0, 0, 0, 0, 0]Designated Initializer: This initializer is used when we want to initialize a range with the same value. This is used only with GCC compilers. [ first . . . last ] = value; int num[5]={ [0 . . . 4 ] = 3 }; // num = { 3, 3, 3, 3, 3} We may also ignore the size of array: int num[ ]={ [0 . . . 4 ] = 3 }; // num = { 3, 3, 3, 3, 3}Macros: For initializing a huge array with the same value we can use macros. C #include<stdio.h> #define x1 1 #define x2 x1, x1 #define x4 x2, x2 #define x8 x4, x4 #define x16 x8, x8 #define x32 x16, x16 int main(void) { // array declaration int num[] = { x32, x8, x4, x1}; int size = sizeof(num)/ sizeof(int); // 32+8+4+1= 45 printf("The size of the array is %d\n", size); printf("The value of element in the array at index 5 is %d ", num[4]); return 0; } OutputThe size of the array is 45 The value of element in the array at index 5 is 1 Using For Loop: We can also use for loop to initialize an array with the same value. C #include<stdio.h> int main(void) { int size = 6; int val = 1; // array declaration int arr[size]; int i; // initializing array elements for (i = 0; i < size ; i++){ arr[i] = val; } // print array printf("The array is:"); for (i = 0; i < size ; i++){ printf("%d ", arr[i]); } return 0; } OutputThe array is:1 1 1 1 1 1 Comment More infoAdvertise with us Next Article Different ways to Initialize all members of an array to the same value in C A AkshitaSaraf Follow Improve Article Tags : Technical Scripter C Language Technical Scripter 2018 Similar Reads C Programming Language Tutorial C is a general-purpose mid-level programming language developed by Dennis M. Ritchie at Bell Laboratories in 1972. It was initially used for the development of UNIX operating system, but it later became popular for a wide range of applications. Today, C remains one of the top three most widely used 5 min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read Decorators in Python In Python, decorators are a powerful and flexible way to modify or extend the behavior of functions or methods, without changing their actual code. A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality. Decorators are 10 min read What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac 13 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi 6 min read Like