Programmer Coding

1- D Array in C Language

One dimensional Array in C (1-D)?

Arrays are a fundamental concept in programming and they have different properties. A one-dimensional array, also called an array, is an array that has only a single dimension or row. In this article we will take a closer look at the C programming language, including its syntax, examples, and output.

Syntax

dataType arrayName[arraySize];

Example

#include <stdio.h>

int main() {

int numbers[5] = {100, 200, 300, 400, 500};

for(int i=0; i<5; i++) {

printf(“numbers[%d] = %d\n”, i, numbers[i]);

}

return 0;

}

Output

numbers[0] = 100

numbers[1] = 200

numbers[2] = 300

numbers[3] = 400

numbers[4] = 500

 

Initializing One Dimensional Array

In C programming language, we can initialize a one-dimensional array while declaring it or later in the program. We can initialize a one-dimensional array while declaring it by using the following syntax:                                                                    dataType arrayName[arraySize] = {element1, element2, …, elementN};

“dataType” specifies the data type of the array.

– ‘arraySize’ specifies the number of elements in the array.

– “{element1, element2, …, elementN}” represents the value of the element in an array. The number of elements must be equal to “arraySize”.

Example

  1. #include <stdio.h>
  2. intmain() {
  3. int numbers[5] = {10, 20, 30, 40, 50};
  4. for(int i=0; i<5; i++) {
  5. printf(“numbers[%d] = %d\n”, i, numbers[i]);
  6. }
  7. return 0;
  8. }

 

Advantages of One dimensional Array (1-D)

  • Compact storage: One-dimensional arrays provide continuous blocks of memory to store the contents of data of the same type, thus improving memory usage efficiency.
  • Efficient Access: Content in a link can be accessed directly from the counter, ensuring constant access, which is very effective.
  • Simple syntax: Arrays in C have a simple declaration and initialization syntax, making them easy to use and understand.
  • Flexibility: One-dimensional arrays can store a specified number of elements at compile time or dynamically allocate memory at run time using parameters, providing flexibility in data management.
  • Efficient Iteration: One-dimensional arrays are good for inverse operations such as traversal or search due to their in-memory concatenation.
  • Versatility: One-dimensional arrays can store many types of data, including numbers, symbols, floating-point numbers, and special data, making them very versatile.
  • Compatibility: One-dimensional arrays are widely supported and compatible with many C libraries and programming environments, providing portability and interoperability.
  • Ease of Passing to Functions: One-dimensional arrays are easily passed as arguments to functions, allowing structure and reuse.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top