Confusing Array in C ( Array Representation and Initialization )
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.
Read More Articles
- Use of getch(),getche() and getchar() in C
- Switch Case Statement Example Program In C Programming Language
- C Character Set
- Convert a Floating-point value to an Integer in C
- Data Input and Output gets and puts Example Program In C
- Special Operators In C
- Pointer Representation and Pointer Example Programs
- C Data Input and Data Output
- Simple While Loop Example Program In C Programming Language
- Data Output printf and putchar Example Program In C
- C Introduction
- C Operators
- Storage Classes In C
- C Pointers
- File Management
- C Identifiers
- Loop Control Statements
- Hello World - Simple C Program
- C Array
- Single Character Output Function : putchar()
- C Reserve Words
- C Specific Properties and Implementation
- If else Statement Example Program In C Programming Language
- If Statement Example Program In C Programming Language
- Confusing Array in C ( Array Representation and Initialization )