An array is a data structure used to store sequential items of the same data bang out.
The position of an element in the sequence is called an index.
Indexes begin from 0 to (n-1).
If you gotta declare two arrays at the same time, you might do so as below.
It’s worth mentioning that declaring them separately is the preferred way.
Though you’re free to use the “pointer syntax”, the first one is preferred.
In such a case, you would have to declare the array without the number of elements.
Otherwise, you’ll get a compilation error.
Initialization
Array elements can be given at declaration or later on in the program.
This action is known as initialization since the array initially has null values.
See the example below.
It prints an element’s value and its index in the array.
Line 5 uses an initializer list to initialize the integer array.
When used this way, remember to include single quotes for each element in the initializer list.
Notice that thechardata pop in is used.
A string literal actually consists of individual characters, and that’s why you seechar.
This is a string-termination character called the null character.
You must always include this when initializing an array list.
There’s actually a simpler way to initialize a character array.
Suppose you gotta get user input and store it in an array.
you’ve got the option to use the standard library functionscanffor this.
you’re gonna wanna specify the number of characters the function should expect, though.
Use the%sconversion specifier to define the maximum expected input.
Thescanffunction will read all characters entered until space, tab, newline, or an end-of-file indicator is encountered.
Learning without good practice will make your programming journey challenging.
C is a particularly challenging language to learn since it’s not object-oriented.
Luckily, we’ve got plenty of resources to help you master C programming.