how to declare and initialization of an array.
Declare and Initialize an Array in C: A Beginner's Guide
To declare an array, you need to specify the data type of the array elements, followed by the name of the array, and the number of elements the array will contain (also known as the array size). The syntax for declaring an array can vary depending on the programming language you're using. Here are some examples:
Deceleration: Datatype variable name[number of elements];
Example:- int a[ 5 ]
here int is the data type and a is the variable name and under the index operator [ ] is the number of elements of an array(size).
Array initialization: Declare as well as assigning the array elements is called initialization
int a[ 5 ]={10,20,30,40,50};
// initialization
here int is the data type and a is the variable name and under the index operator [ 5 ] deceleration the size of an array and also assigning the five elements.
There are three-way to initialization of an array is possible
- int a[ ]={ 10,20,30,40,50}; when we are the initialization of an array providing the number of elements is optional
- int a[ 5 ]={10,20,30}; it is called particle initialized an array remanning elements are filled with zeros
- int a[ 5 ]={ 10,20,30,40,50,60,70 }; this is excess elements initialized in this case compiler gives a warning not an error
#include
void main()
{
int a[5]={10,20,30,40,50};//initialization
printf("%d\n",sizeof(a));
}
output:-5
Note: int a[ ]; //error whenever you declare an array we need to provide the size of an array.
Further Reading:
- [ what is an array ]
- [ how to declare and initialization an array ]
- [ Dynamic Declaration of an array ]
- [ How to scan an array and how to print it on screen. ]
Top Resources
- Learn-C.org
- C Programming at LearnCpp.com
- GeeksforGeeks - C Programming Language
- C Programming on Tutorialspoint
- Codecademy - Learn C
- CProgramming.com
- C Programming Wikibook
- C Programming - Reddit Community
- C Programming Language - Official Documentation
- GitHub - Awesome C
- HackerRank - C Language
- LeetCode - C
Enroll Now:
[ C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"
Contact Us:
- For any inquiries, please email us at [info@electro4u.net].
- Follow us on insta [ electro4u_offical_ ] for updates and tips.
Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!