有人能解释一下为什么下面的程序什么都不输出吗?(复制)

时间:2021-11-29 01:42:30

This question already has an answer here:

这个问题已经有了答案:

The following program compiles successfully but when i ran it ,it prints nothing when i initialize the for loop with -1 but when i initialize for loop with 0 it successfully traverse all the array.I want to ask that can we don't traverse the array when we initialize the for loop with negative value??

下面的程序编译成功,但是当我运行它时,当我用-1初始化for循环时,它不会打印任何东西,但是当我用0初始化for循环时,它成功地遍历了所有的数组。我想问,当我们用负值初始化for循环时,我们能不能不遍历数组?

#include <stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23, 34, 12, 17, 204, 99, 16};

int main(void) {
    int d;
    //printf("%d",TOTAL_ELEMENTS);
    for (d = -1; d <= (TOTAL_ELEMENTS - 2); d++) 
    {
        printf("%d ",d);
        printf("%d\n", array[d+1]);
    }    
    return 0;
}

4 个解决方案

#1


2  

The result of sizeof operator is of type size_t, which is an unsigned type.

sizeof运算符的结果是类型size_t,它是一个无符号类型。

As a result, the type of TOTAL_ELEMENTS is also unsigned. When -1 is compared with it, it's converted to a big unsigned number. That's why d <= (TOTAL_ELEMENTS - 2) is false.

因此,TOTAL_ELEMENTS的类型也是无符号的。当-1和它比较时,它就变成了一个大的无符号数。这就是为什么d <= (TOTAL_ELEMENTS - 2)是假的。

#2


1  

This doesn't do what you think it does:

这并不像你想象的那样:

d <= (TOTAL_ELEMENTS - 2)

Instead, do this:

相反,这样做:

d <= int(TOTAL_ELEMENTS - 2)

Otherwise you've got a signed-vs-unsigned comparison, and your -1 becomes the largest possible size_t.

否则,您将得到一个signe -vs-unsigned比较,而您的-1将成为最大的size_t。

#3


1  

Here d <= (TOTAL_ELEMENTS - 2) operands are subject of usual arithmetic conversions (6.3.1.8). And actually integer promotions rules act in your case:

这里d <= (TOTAL_ELEMENTS - 2)操作数是通常的算术转换的对象(6.3.1.8)。事实上整数促销规则在你的案例中起作用:

  1. If both operands have the same type, then no further conversion is needed.

    如果两个操作数具有相同的类型,则不需要进一步的转换。

  2. Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.

    否则,如果两个操作数都有带符号整数类型或都有无符号整数类型,那么具有较小整数转换级别的操作数将转换为具有较大级别的操作数类型。

  3. Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.

    否则,如果具有无符号整数类型的操作数的秩大于或等于其他操作数的类型的秩,则使用带符号整数类型的操作数转换为具有无符号整数类型的操作数类型。

  4. Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.

    否则,如果带符号整数类型的操作数的类型可以表示具有无符号整数类型的操作数类型的所有值,那么具有无符号整数类型的操作数将被转换为具有符号整数类型的操作数的类型。

  5. Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.

    否则,两个操作数都被转换为与带符号整型的操作数类型对应的无符号整型。

According to what you've got your code falls into clause #3, then your signed -1 is converted via rule (6.3.1.3):

根据您得到的代码属于第3条,然后您的签名-1通过规则(6.3.1.3)进行转换:

Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

否则,如果新类型是未签名的,则该值将通过反复添加或减去一个大于新类型中可以表示的最大值来转换,直到该值位于新类型的范围内为止。

As result it becomes a very large unsigned value, that is surely greater than TOTAL_ELEMENTS - 2 and you'll never enter the loop.

因此,它成为一个非常大的无符号值,肯定大于TOTAL_ELEMENTS——2,您永远不会进入循环。

#4


0  

sizeof produces a result of size_t, which is unsigned. Compare a signed and unsigned type and you can only expect things to blow up.

sizeof产生的结果是size_t,它是无符号的。比较一个有符号的和无符号的类型,你只能期望事情会失败。

To elaborate, when you try to use both signed and unsigned type in arithmatic operations, the signed type will be promoted to unsigned type, producing a huge number. Thus, the value of d , promoted to unsigned type, will fail to meet the condition d <= (TOTAL_ELEMENTS - 2);, hence the loop body will not execute.

要详细说明,当您尝试在算术操作中使用签名和无符号类型时,签名类型将被提升为无符号类型,产生一个巨大的数字。因此,将d提升为无符号类型的值将不能满足条件d <= (TOTAL_ELEMENTS - 2);因此循环体将不会执行。

For operators that expect operands of arithmetic type cause conversions. This pattern is called the usual arithmetic conversions. for this particular case, quoting the standard, chapter §6.3.1.8

对于期望算术类型的操作数导致转换的操作数。这种模式称为通常的算术转换。对于这个特殊的情况下,引用标准,章§6.3.1.8

Otherwise, the integer promotions are performed on both operands. Then the following rules are applied to the promoted operands:

否则,在两个操作数上执行整型提升。然后对提升操作数应用以下规则:

[...]

[…]

  • Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.
  • 否则,如果具有无符号整数类型的操作数的秩大于或等于其他操作数的类型的秩,则使用带符号整数类型的操作数转换为具有无符号整数类型的操作数类型。

and, regarding the rank,

关于排名的,

  • The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any.
  • 任何无符号整数类型的秩都应该等于相应的有符号整数类型的秩(如果有的话)。

Also, for reference, quoting C11, chapter 7.19, (emphasis mine)

同样,为了参考,引用C11,第7.19章(强调我的)

size_t

size_t

which is the unsigned integer type of the result of the sizeof operator;

是sizeof运算符结果的无符号整型;

Hint: Enable compiler warning and it will point to your mistake.

提示:启用编译器警告,它将指向您的错误。

#1


2  

The result of sizeof operator is of type size_t, which is an unsigned type.

sizeof运算符的结果是类型size_t,它是一个无符号类型。

As a result, the type of TOTAL_ELEMENTS is also unsigned. When -1 is compared with it, it's converted to a big unsigned number. That's why d <= (TOTAL_ELEMENTS - 2) is false.

因此,TOTAL_ELEMENTS的类型也是无符号的。当-1和它比较时,它就变成了一个大的无符号数。这就是为什么d <= (TOTAL_ELEMENTS - 2)是假的。

#2


1  

This doesn't do what you think it does:

这并不像你想象的那样:

d <= (TOTAL_ELEMENTS - 2)

Instead, do this:

相反,这样做:

d <= int(TOTAL_ELEMENTS - 2)

Otherwise you've got a signed-vs-unsigned comparison, and your -1 becomes the largest possible size_t.

否则,您将得到一个signe -vs-unsigned比较,而您的-1将成为最大的size_t。

#3


1  

Here d <= (TOTAL_ELEMENTS - 2) operands are subject of usual arithmetic conversions (6.3.1.8). And actually integer promotions rules act in your case:

这里d <= (TOTAL_ELEMENTS - 2)操作数是通常的算术转换的对象(6.3.1.8)。事实上整数促销规则在你的案例中起作用:

  1. If both operands have the same type, then no further conversion is needed.

    如果两个操作数具有相同的类型,则不需要进一步的转换。

  2. Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.

    否则,如果两个操作数都有带符号整数类型或都有无符号整数类型,那么具有较小整数转换级别的操作数将转换为具有较大级别的操作数类型。

  3. Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.

    否则,如果具有无符号整数类型的操作数的秩大于或等于其他操作数的类型的秩,则使用带符号整数类型的操作数转换为具有无符号整数类型的操作数类型。

  4. Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.

    否则,如果带符号整数类型的操作数的类型可以表示具有无符号整数类型的操作数类型的所有值,那么具有无符号整数类型的操作数将被转换为具有符号整数类型的操作数的类型。

  5. Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.

    否则,两个操作数都被转换为与带符号整型的操作数类型对应的无符号整型。

According to what you've got your code falls into clause #3, then your signed -1 is converted via rule (6.3.1.3):

根据您得到的代码属于第3条,然后您的签名-1通过规则(6.3.1.3)进行转换:

Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

否则,如果新类型是未签名的,则该值将通过反复添加或减去一个大于新类型中可以表示的最大值来转换,直到该值位于新类型的范围内为止。

As result it becomes a very large unsigned value, that is surely greater than TOTAL_ELEMENTS - 2 and you'll never enter the loop.

因此,它成为一个非常大的无符号值,肯定大于TOTAL_ELEMENTS——2,您永远不会进入循环。

#4


0  

sizeof produces a result of size_t, which is unsigned. Compare a signed and unsigned type and you can only expect things to blow up.

sizeof产生的结果是size_t,它是无符号的。比较一个有符号的和无符号的类型,你只能期望事情会失败。

To elaborate, when you try to use both signed and unsigned type in arithmatic operations, the signed type will be promoted to unsigned type, producing a huge number. Thus, the value of d , promoted to unsigned type, will fail to meet the condition d <= (TOTAL_ELEMENTS - 2);, hence the loop body will not execute.

要详细说明,当您尝试在算术操作中使用签名和无符号类型时,签名类型将被提升为无符号类型,产生一个巨大的数字。因此,将d提升为无符号类型的值将不能满足条件d <= (TOTAL_ELEMENTS - 2);因此循环体将不会执行。

For operators that expect operands of arithmetic type cause conversions. This pattern is called the usual arithmetic conversions. for this particular case, quoting the standard, chapter §6.3.1.8

对于期望算术类型的操作数导致转换的操作数。这种模式称为通常的算术转换。对于这个特殊的情况下,引用标准,章§6.3.1.8

Otherwise, the integer promotions are performed on both operands. Then the following rules are applied to the promoted operands:

否则,在两个操作数上执行整型提升。然后对提升操作数应用以下规则:

[...]

[…]

  • Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.
  • 否则,如果具有无符号整数类型的操作数的秩大于或等于其他操作数的类型的秩,则使用带符号整数类型的操作数转换为具有无符号整数类型的操作数类型。

and, regarding the rank,

关于排名的,

  • The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any.
  • 任何无符号整数类型的秩都应该等于相应的有符号整数类型的秩(如果有的话)。

Also, for reference, quoting C11, chapter 7.19, (emphasis mine)

同样,为了参考,引用C11,第7.19章(强调我的)

size_t

size_t

which is the unsigned integer type of the result of the sizeof operator;

是sizeof运算符结果的无符号整型;

Hint: Enable compiler warning and it will point to your mistake.

提示:启用编译器警告,它将指向您的错误。