Confusing Array in C ( Array Representation and Initialization )
On this page (9sections)
Definition Of Array:
A Collection Of Homogeneous type Variable.
Syntax:
data-type variable[ boundary ];
Example:
int a[100]
here 'a' has 100 integer variable.
a[0],a[1],a[2].....a[99].
In Array, the value storing may differ sometimes. Refer to the following two programs. By those two programs, we understand how to store values in the array.
Simple Program for Understanding Array Initialization: Example Program: 1
/* Example Program For Array In C Programming Language
little drops @ thiyagaraaj.com
Coded By:THIYAGARAAJ MP */
// Header Files
#include<stdio.h>
#include<conio.h>
//Main Function
void main()
{
// Variable Declaration
char Name[5]="gotfried";
clrscr(); //clear the screen
printf("%s",Name);
getch();
}
Output:
Too many initializers.
Description:
The above program will not work, because the value initialized is out of array bound. And so,error will occur during compilation. The below program eradicates this error.
Simple Program for Understanding Array Initialization: Example Program: 2
/* Example Program For Array In C Programming Language
little drops @ thiyagaraaj.com
Coded By:THIYAGARAAJ MP */
// Header Files
#include<stdio.h>
#include<conio.h>
//Main Function
void main()
{
// Variable Declaration
char Name[5];
int i=0;
clrscr(); //clear the screen
Name[0]='g';
Name[1]='o';
Name[2]='t';
Name[3]='f';
Name[4]='r';
Name[5]='i';
Name[6]='e';
Name[7]='d';
while(i<8)
{
printf("%c",Name[i]);
i++;
}
getch();
}
Output:
gotfried
Description:
The above program will work because the array initialization is correct in this case. At first, the array is declared with the array bound of 5, but at the time compilation, the compiler doesn’t bother about the declaration. At the time of compilation, array initialization is taken into account and the action is performed.
Related Pages
- C Archive — Browse all C Archive.
- 2+3 and 5 are not equal In C — More in c blog.
- The Use of * and & in C/C++ — More in c blog.
- Use of getch(),getche() and getchar() in C — More in c blog.
Frequently Asked Questions
What does this C program do?
It is a C example program that demonstrates Confusing Array in C ( Array Representation and Initialization ), including the complete source code and the expected sample output.
How do I compile and run this C program?
Save the code in a `.c` file, compile it with `gcc filename.c -o program`, then run it with `./program` (or `program.exe` on Windows).
What concepts does this example use?
This example uses loops to iterate over data, arrays and user-defined functions, illustrating a common pattern in C programming.