Searching for a Value in an Array in C Programming

Searching for a Value in an Array in C Programming

Programming languages are used to communicate with computers and to create software applications. C programming language is one of the most popular programming languages used for system programming, embedded systems, and game development. One of the essential aspects of programming is searching for values in an array. This article will explore the code for searching for a value in an array in C programming.

Introduction to Searching for a Value in an Array

Searching for a value in an array is a common task in programming. An array is a collection of similar data types, and searching for a value in an array requires iterating through the array and comparing each element with the value being searched for. In C programming, we can use the for loop to iterate through an array and compare the elements.

The Code for Searching for a Value in an Array

The code for searching for a value in an array in C programming is shown below:

#include<stdio.h>
int main()
{
    int num[]={12,34,56,78,19};
    int pos=-1,value,i;
    scanf("%d",&value);
    for(i=0;i<5;i++)
    {
        if(value==num[i]){
            pos=i+1;
            break;
        }
    }
    if(pos==-1){
        printf("value is not common");
    }
    else printf("value %d is found at %d position",value,pos);
}

Let’s analyze the code in detail.

Line 1: Include Header File

The first line of the code is the preprocessor directive #include<stdio.h>. This line includes the standard input-output header file, which provides functions for input and output operations.

Line 2: Define the main() Function

The second line of the code defines the main() function, which is the starting point of any C program. The function returns an integer value, which represents the exit status of the program.

Line 4: Declare and Initialize the Array

The fourth line of the code declares and initializes an integer array named num. The array contains five elements, which are 12, 34, 56, 78, and 19.

Line 5-6: Declare and Initialize Variables

The fifth and sixth lines of the code declare and initialize two integer variables named pos and value. The pos variable is used to store the position of the value in the array, and the value variable is used to store the value being searched for.

Line 7: Get the Value from the User

The seventh line of the code uses the scanf() function to get the value being searched for from the user.

Line 8-12: Search for the Value in the Array

The eighth to twelfth lines of the code use a for loop to iterate through the array and search for the value. Inside the for loop, the if statement checks if the current element of the array is equal to the value being searched for. If the value is found, the position of the value in the array is stored in the pos variable, and the loop is terminated using the break statement.

Line 13-16: Print the Result

The thirteenth to sixteenth lines of the code use an if-else statement to print the result. If the value is not found, the program prints a message stating that the value is not in the array. If the value is found, the program prints a message stating the position of the value in the array.

Conclusion

Searching for a value in an array is a fundamental task in programming, and the for loop is a common construct used to iterate.

Array in C Programming

Quora | Youtube | Facebook

How do you use Scanf to enter integers into the array in C?

Leave a Comment

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

Scroll to Top
NEW TOXIC ROCKET CYCLE DECK in Clash Royale 2023 Best Deck in Clash Royale Best Deck for Arena 8 in Clash Royale Best Hog deck for Pro players Best Clash Royale Deck