我在C语言书中找到了这个例子。 此代码将转换输入数字库并将其存储在数组中。这条语句背后的逻辑是什么:for(--index; index> = 0; --index)?
#include <stdio.h>
int main(void)
{
const char base_digits[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
int converted_number[64];
long int number_to_convert;
int next_digit, base, index = 0;
printf("type a number to be converted: \n");
scanf("%ld", &number_to_convert);
printf("Base\n");
scanf("%i", &base);
do
{
converted_number[index] = number_to_convert % base;
++index;
number_to_convert = number_to_convert/base;
}
while (number_to_convert != 0);
// now display the number
printf("converted number = :");
for (--index; index >= 0; --index)
{
next_digit = converted_number[index];
printf("%c", base_digits[next_digit]);
}
printf("\n");
return 0;
}
我无法理解最后一个循环。它应该有助于扭转阵列,但我不明白!
这行是什么意思:for (--index; index >= 0; --index)
?
我想补充一点,这是一本书,你应该在垃圾桶里,如果你遇到类似的循环折腾在它里面。 ** dasblinkenlight **轻松地突出了一种更好的方法,任何值得他的盐的程序员都可以提出。 –