how to declare and initialization of an array.

27 Dec 2022 Balmiki Mandal 0 C Programming

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:

Top Resources

 

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!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.