×

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

Enumeration (Enum) in C

Enumeration (Enum) in C: In the C programming language, the enum is referred to as an enumerated type. Enum is a user defined data type consisting of integer values and...

3 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

3 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.

Different ways to Declare the Variable as Constant in C

There are a wide range of ways of making the variable as steady Using const keyword: The const catchphrase permits a software engineer to inform the compiler that a specific variable should...

5 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

Round Robin Scheduling in C

Round Robin Scheduling in C Round robin is a CPU (Central Processing Unit) scheduling algorithm designed to share the time systems. It is one of the simplest and easiest scheduling algorithms...

4 minutes read.

Nested Loops in C Programming Examples

A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a “loop inside the...

6 minutes read.

Return array from function in C

Return array from function in C C programming does not require the return to a function of a whole array as an argument. However, you can return a pointer to an array without...

2 minutes read.

Purpose of a Function Prototype in C

A function prototype in C is a declaration of a function that specifies the function's name, return type and parameters. It has the following syntax: return_type function_name(parameter_list); For example, the prototype for...

4 minutes read.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.

Types of Function in C

A function is a piece of code that accomplish a certain operation for a specific task. There are two types of functions in C: System-defined function (Library Function)User-defined function 1. System defined Function...

8 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.

Loop Questions in C

Question 1: What is For loop syntax? Ans: The syntax that has been used for the ‘for’ loop in C programming language is: for (initialization statement /*for providing a value to variables*/; test...

19 minutes read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

7 Best IDEs for C/C++ Developers in 2024

As we all know that in programming languages, C is known as the building block which cannot be contradicted, and C++ is the prolong kind of C and it can...

4 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

Break, continue and goto statement in C

Break statement Break statement is used to break the process of a loop (while, do while and for) and switch case. Syntax: break; Example 1 while(test Expression) { // codes if(condition for break){ break; } // codes } Example 2 For(int it, condition,...

1 minute read.

#error #pragma in C

#error, #pragma in C #error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which...

4 minutes read.

How to find square root in C Language

Before understanding the square root program in C language, one must know about the square root of a number. A square root is a mathematical term. The square root of a...

5 minutes read.