Home > Backend Development > C++ > Explain the characteristics and operation methods of arrays in C language

Explain the characteristics and operation methods of arrays in C language

王林
Release: 2023-09-04 16:57:16
forward
1552 people have browsed it

Explain the characteristics and operation methods of arrays in C language

An array is a homogeneous sequential collection of data items on a single variable name.

For example, int Student[30];

Here, student is an array name containing a collection of 30 data items with a single variable name.

Features

The characteristics of arrays are as follows -

  • Arrays are always stored in contiguous memory locations.

  • It can store multiple values ​​of similar type that can be referenced with a single name.

  • The pointer points to the first of the memory block position, which is assigned to Array name.

  • Arrays can be integer, character, or floating-point data types and can only be initialized during declaration.

  • Specific elements of an array can be modified individually without changing

  • All elements in the array can be distinguished by their index number.

Operations h2>

Operations on arrays include -

  • Search - Used to find whether a specific element To exist or not.

  • Sort - Helps to sort the elements in an array in ascending or descending order.

  • Traverse - Process each element in the array sequentially.

  • Insert - Helps to insert elements in an array.

  • Delete - Helps to delete elements from an array.

Sample Program

The following is a C program for searching for elements in an array -

Real-time Demonstration

#include <stdio.h>
#define MAX 100 // Maximum array size
int main(){
   int array[MAX];
   int size, i, search, found;
   printf("Enter size of array: ");
   scanf("%d", &size);
   printf("Enter elements in array: ");
   for(i=0; i<size; i++){
      scanf("%d", &array[i]);
   }
   printf("</p><p>Enter element to search: ");
   scanf("%d", &search);
   found = 0;
   for(i=0; i<size; i++){
      if(array[i] == search){
         found = 1;
         break;
      }
   }
   if(found == 1){
      printf("</p><p>%d is found at position %d", search, i + 1);
   } else {
      printf("</p><p>%d is not found in the array", search);
   }
   return 0;
}
Copy after login

Output

The output is as follows-

Enter size of array: 5
Enter elements in array: 11 24 13 12 45
Enter element to search: 13
13 found at position 3found
Copy after login

The above is the detailed content of Explain the characteristics and operation methods of arrays in C language. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template