×

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in an array is referred to element of the array. The elements of the array share the same variable name but are unique in their own way and also have a different index number which is also called a subscript.

An array can of any data type such as int, float, double, and so on. If an array declared is of the data type int, then all the elements within that array should be of int data type only. This rule is the same with other data types as well.

The array comes into use when a programmer is creating an application to store the roll numbers of 100 students. As we all know, a variable allows us to store only one value at a time, and it cannot be used for cases like this, as declaring many variables for a single entity (student) is not a good idea.

In a situation like this, arrays give a better way to store the data. Arrays can be either a single dimensional array or a multidimensional array. The number of a subscript or the index elements usually determines the dimension of an array.

One dimensional array

In simple words, a one-dimensional array can be thought of as a row where the elements are stored one after the other. It is also defined as an array that has only one subscript that is used to specify the particular element of an array. It is a structured collection of the components that are accessed individually by indicating the position of the elements within a single index value.

Syntax

datatype array_name [size_of_array];

Where;

  • datatype: It is the type of data for the elements within an array. It follows the C standard rules of data type.
  • array_name:It is used to represent the name of the array and is user defined and must be a valid identifier.
  • size_of_array:It indicated the number of the elements of an array that will be able to hold.

E.g.:

 int a[100]; /*a is of the array data type integer(int) and can store only 100 elements in it*/
 float b[10];/*b is of the array data type float and can store only 10 elements in it*/
 char c[20];/*c is of the array data type character(char) and can store 20 elements in it*/ 

When an array is declared at the beginning, it consists of garbage values.

Initializing an array

The array can be initialized in two situations such as;

  1. Compile time initialization (static initialization)
  2. Runtime initialization(dynamic initialization)

Rules for declaring a one-dimensional array

  • An array variable must be declared before being used in a program.
  • The declaration must have a data type (int, float, char, double, etc.), variable name, and subscript.
  • The subscript represents the size of the array. If the size is declared as 10, programmers can store 10 elements.
  • Every array index number always begins from 0 such as if an array variable is declared as s[100], then it ranges from 0 to 99.
  • Every array element will be stored in a separate memory location.

Compile time initialization

The array declared at the beginning in which the elements will be declared to it during the compile time is written.

Syntax

datatype array_name [array_size] = (list of the elements of an array);

E.g.:

 int n[3] = {0, 1, 2};
 #include<stdio.h>
 int main()
 {
  int n[10]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
 printf("%d", n[0]);
 printf("%d", n[1]);
 printf("%d", n[2]);
 printf("%d", n[3]);
 printf("%d", n[4]);
 printf("%d", n[5]);
 printf("%d", n[6]);
 printf("%d", n[7]);
 printf("%d", n[8]);
 printf("%d", n[9]);
 } 

Output

One Dimensional Array in C

Runtime initialization of an array

Runtime initialization in the array concept means it can be initialized at the runtime. It means that the array elements are initialized after the compilation of the program.

E.g.:

 #include<stdio.h>
 int main()
 {
 int arr[5], i;
 for(i = 0; i< 5; i++)
 {
 printf("Enter a[%d]: ", i);
 scanf("%d", &arr[i]);
 }
 printf("\nPrinting elements of the array: \n\n");
 for(i = 0; i< 5; i++)
 {
 printf("%d ", arr[i]);
 }
 /* signal to operating system program ran fine */
 return 0;
 } 

Output

One Dimensional Array in C

Declaration of an array of integer 5 and variable i, which is of type int. Then ‘a’ loop is used to enter the elements into an array at run time.

In scanf() statements, ‘&’ operator will be used on the element ‘arr[i]’ of an array; hence we have int, similar to float, char, and so on.

The printf() statement prints the elements in the console.


Related Topics

Integer Promotions in C

As we know that some of the data types such as char, short int, Enum takes a smaller number of bytes than compared to int. When an operation is applied...

3 minutes read.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

GCD of Two Numbers in C

The GCD means the greatest common divisor of two or more integers, it is also called as hcf. The gcd returns the greatest integer of the given two integers that...

3 minutes read.

Attributes in C

Attributes are one of modern C++ key features. It is the process by which initiator can add more information to the language instead of introducing new keywords for every attribute....

4 minutes read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

String Handling functions in C

String :- The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any...

5 minutes read.

How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two...

3 minutes read.

Decimal to Hexadecimal in C

Decimal to Hexadecimal in C Let us first understand the definitions of decimal and hexadecimal. In algebra, a decimal number is defined as a number whose whole number part and a...

3 minutes read.

Call by Value and Call by Reference in C

Call by reference and call by value are two different ways of passing arguments to a function in C programming language. Call by reference means that the called function is...

4 minutes read.

C program to compare the two strings

C program to compare the two strings Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with...

4 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

Storage Class in C

C storage class is used to define the scope variables and function. There are four various types of storage classes that are given below. auto: The auto keyword is the default...

1 minute read.

C String Manipulation programs without using Library Functions

C string manipulation programs without using Library Functions The string is the character list, and typically we operate with library functions to read and print the entire string, but here you...

6 minutes read.

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.

fork() in C

Introduction To create a new function in the system, there is a need for a system call, i.e. fork system call. It is also known as the child process. These child...

2 minutes read.

Null character in C

Introduction The Null character in the C programming language is used to terminate the character strings. In other words, the Null character is used to represent the end of the string...

3 minutes read.

Inline Function in C

Inline Function in C A normal function will become an inline function when the function prototype of that function is prepended with the keyword "inline". Inline functions are the function where...

4 minutes read.

Float in islower() in C

Introduction: This article briefly discusses the float in islower() in C. The islower() function checks if the input character passed inside the function is lowercase. Lowercase letters include (a-z). The islower()...

4 minutes read.