Home > Back-end >  Searching in array in C
Searching in array in C

Time:12-16

#include <stdio.h>

int main()
{

    int arr[] = {10, 20, 30, 8, 2};
    int index = -1, ele;

    printf("Enter the elment you want to search :");
    scanf("%d", ele);
    for (int i = 0; i < 5; i  )
    {
        if (arr[i] == ele)
            ;
        index = i;
        break;
    }

    if (index == -1)
    {
        printf("Not found\n");
    }
    else
    {
        printf("Found at %d", index);
    }

    return 0;
}

the above code isn't printing the result. I am learning C programing from past few days. Can You please help me out with this. I am not getting any type of error.

CodePudding user response:

Your if statement doesn't have any code within it (just a semi colon). Prefer using braces


        if (arr[i] == ele) {
          index = i;
          break;
        }

Also, you'll need to specify the address of the variable you are reading into when using scanf, e.g.

    scanf("%d", &ele); //< take the address of 'ele' using &
  •  Tags:  
  • c
  • Related