что произойдет, если мы случайно сравним знаковые и беззнаковые целые числа?

простой ответ: зависит! (если предполагаемая функциональность работает, это здорово!)

сначала плохой случай~:

в плохом случае: мы хотим напечатать от «0» до «3». но он не будет работать точно, как мы предполагали.

Примечание:

Стандарт C не предоставляет никакой информации о том, «что происходит, когда доступ к массиву выходит за пределы объявленного размера массива». Таким образом, поведение «неопределенно».

Значения, которые вы видите в выводе, различаются: зависит от того, «где компилятор выделяет память для вашей программы и какие другие переменные находятся помимо памяти, выделенной вашим массивом».

#include <stdio.h> 

/*
  role of 'test' function is: 
    print the elements with in the given 'left' and 'right' indices.
*/

// attention: i have defined left as unsigned int, right as signed int.
void test(int *arr, unsigned int left, int right, unsigned int N)
{
    // we are not in the limits of the array size.
    if (right >= N)
    {
        // throw a error and return
        printf("Input arguements are not in the range!!");
        return;
    }

    // this piece of code is: very important.

    /*
        if right is 3, left is 0,
            then compiler upgrades right to 'unsigned int' because, 
            we are comparing with the higher priority order 'data type'.

            which lead to 'undefined behaviour' which accessing the array. 
            because, we havenot defined that kind of size of array.
    */
    while (right >= left){
        /*
          when the value of right becomes 0, right-- becomes negative. 
          further, in the next iteration, compiler changes data type of 
          'right' to 'unsigned int'
        */
        printf("%d\n", arr[right--]);
    }

    return;
}
int main(){
    int arr[] ={1, 2, 3, 4, 5, 6};

    // this is a bad case. 
    test(arr, 0, 3, sizeof(arr)/sizeof(int));
    return 0;
}

выход:

4
3
2
1
0
6
0
-5
0
0
0
6422016
.
.

тот же код: но хороший случай:

слева как 1 (кроме 0), справа как «3».

#include <stdio.h> 

/*
  role of 'test' function is: 
    print the elements with in the given 'left' and 'right' indices.
*/

// attention: i have defined left as unsigned int, right as signed int.
void test(int *arr, unsigned int left, int right, unsigned int N)
{
    // we are not in the limits of the array size.
    if (right >= N)
    {
        // throw a error and return
        printf("Input arguements are not in the range!!");
        return;
    }

    // this piece of code is: very important.
    /*
        comparison terminates before 'right' variable enters negative side and trigger 'upgradation of datatype' by compiler.
    */
    while (right >= left){
        printf("%d\n", arr[right--]);
    }

    return;
}
int main(){
    int arr[] ={1, 2, 3, 4, 5, 6};

    // this is a bad case. left as '1', right as '3'.
    test(arr, 1, 3, sizeof(arr)/sizeof(int));
    return 0;
}

выход:

4
3
2

Спасибо за чтение~.