Vous êtes sur la page 1sur 5

1-D Array In C | Programmerdouts

programmerdouts.blogspot.com/2019/06/1-d-array-in-c.html

One-dimensional Array

Declaration of 1-D Array


Syntax:
datatype array_name[size]
Size : means how many elements you want to store in an array.
If number of element is more than specified size ,than interpreter
will throw an error.
If number of element is less than specified size than interpreter will
not show an error, just the value at that empty indexes there will be
a garbage value.

Lets declare some Arrays

#include<stdio.h>

void main()
{
int student[10]; // array of 10 students.

int marks[10]; // marks of 10 student of one subject.

float percent[10]; //array of percentage of 10 students.

Initialization of an Array.

1/5
As we declare a variable and assign a value to it ,the same thing is
also done with an array.
We use curly bracket to initialize the array.
Inside, that curly bracket we write all the element in an sequence.

Syntax:

datatype array_name[size] = {element1,element2,element3,...,..,};

Lets declare some Arrays and do their initialization.

#include<stdio.h>

void main()
{
int student_roll[5] = {21,32,65,78,98}; // assigning value to an array of roll no
of 5 student.

int marks[5] = {58,65,48,97,56}; // assigning value to an array of marks of 5


student of one subject.

float percent[10] = {56.56,65.5,59.65,94.65,63.56}; //assigning value to an


array of percentage of 5 student.

Accessing the elements of an Array


Accessing elements of an array is very easy and it works very fast.
We access elements by its index number, as you know every
element of an array has an unique index number, with that
accessing of element is done.

Syntax:

array_name[index_number]
Lets Access some element of an Array

2/5
#include<stdio.h>

void main()
{ int rollno, rollno2, rollno3, mark1, mark2,mark3; //declaring variables
float percent1, percent2,percent3;

int student_roll[5] = {21,32,65,78,98}; // Initializing array of roll no of 5


student.
rollno = student_roll[0]; // accessing the element at index 0 which is 21 and
assigning to a variable
rollno2 = student_roll[1]; // accessing the element at index 1 which is 32 and
assigning to a variable
rollno3 = student_roll[2]; // accessing the element at index 2 which is 65 and
assigning to a variable

int english_marks[5] = {58,65,48,97,56}; // Initializing array of marks of 5


student of one subject.
mark1 = english_marks[0]; // accessing the element at index 0 which is 58 and
assigning to a variable
mark2 = english_marks[1]; // accessing the element at index 1 which is 65 and
assigning to a variable
mark3 = english_marks[2]; // accessing the element at index 2 which is 48 and
assigning to a variable

float percent[10] = {56.56,65.5,59.65,94.65,63.56}; //Initializing an array of


percentage of 5 student.

percent1 = percent[0]; // accessing the element at index 0 which is 56.56 and


assigning to a variable
percent2 = percent[1]; // accessing the element at index 1 which is 365.5 and
assigning to a variable
percent3 = percent[2]; // accessing the element at index 2 which is 59.65 and
assigning to a variable

// printing the marks ,roll no, percentage of accessed data from the arrays.
printf("\nStudent whose roll no is %d has %d marks in English and achieved %f
percentage",rollno,mark1,percent1);
printf("\nStudent whose roll no is %d has %d marks in English and achieved %f
percentage",rollno2,mark2,percent2);
printf("\nStudent whose roll no is %d has %d marks in English and achieved %f
percentage",rollno3,mark3,percent3);

3/5
Output:
Student whose roll no is 21 has 58 marks in English and achieved 56.56
percentage
Student whose roll no is 32 has 65 marks in English and achieved 65.5
percentage
Student whose roll no is 65 has 48 marks in English and achieved 59.65
percentage

Further Concepts:
Concept of Initializing One Dimensional Array
How to take array elements as input from the User
What is Two Dimensional Array ?
Concept of Two Dimensional Array
How to initialize Two Dimensional Array?
How to take Elements of Two dimensional array from the User?
How to display 2D Array As an Table?
Practice Program Regarding Arrays

Practice Programs
Programs regarding Arrays
Programs Regarding 2d arrays
Programs regarding matrix
And lot more

Further Topics:

People Also Searched:

4/5
5/5