C Programming: Integer Search

In the C language, searching for an integer can be implemented using loop structures and conditional statements. Here is a simple example code:

#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int target = 3;
    int found = 0;

    for (int i = 0; i < sizeof(arr)/sizeof(arr[0]); i++) {
        if (arr[i] == target) {
            found = 1;
            printf("找到整数 %d 在数组中的位置为 %d\n", target, i);
            break;
        }
    }

    if (!found) {
        printf("未找到整数 %d\n", target);
    }

    return 0;
}

In the code above, an integer array named ‘arr’ is first defined, followed by an integer ‘target’ to represent the integer to search for. A loop is used to iterate through the array ‘arr’, and in each iteration, the current element is checked to see if it matches the target. If a match is found, the position of the integer in the array is outputted, and the loop is exited. If the integer target is not found after the iteration is completed, a message indicating the integer was not found is outputted.

bannerAds