Monday 10 May 2021

One dimensional arrays in C with examples

A one dimensional array is a group of elements of same type and stored under one subject or index. (OR)

A one dimensional array is single subscript variable which hold more one value under a single name.

i) Declaration of array

Like variables, arrays must be declared before storing values into it. The following is the syntax to declare a one dimensional array.

Syntax: type array-name[size];

Example: int a[3];

A declaration tells to the compiler three things 

1. Array type

2. Array name

3. Number of elements stored in the array( size of array)

ii) storing values on array or initialization

We can store values in array on the same way as the ordinary variable when they are declared. The general syntax is

Syntax: type array-name[size]={ list of values}

Example: int a[3]={10,20,30};

We can also store values incompletely like

Example: char ch[5]={'a','b','c'};

The rest of the locations stores default values depending on array types.

We can also store values individually using subscript like

ch[3]='d';

We can copy an entire array into another array like

int b[3];

b=a;

We can store values into large arrays using for loop like

int a[500];

for(i=0;i<500;i++)

{

scanf("%d", &a[i]);

}

 iii) Accessing elements of the array

The individual elements of an array are identified using the combination of array name and index value. We use the following general syntax to access individual elements of an array.

syntax: array-name[index value];

Example:

for(i=0;i<500;i++)

{

printf("%d", a[i]);

}

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top