以阵列的打印元件 [英] Print elements in an array

查看:128
本文介绍了以阵列的打印元件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/950051/confused-about-c-macro-expansion-and-integer-arithmetic\">Confused关于C宏扩展和整数运算结果
  谜语(C语言)

下面的C程序的输出是打印数组中的元素。但是,当实际运行,它没有这样做。

The output of the following C program is to print the elements in the array. But when actually run, it doesn't do so.

  #include<stdio.h>

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

  int main()
  {
      int d;

      for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
          printf("%d\n",array[d+1]);

      return 0;
  }

这是为什么?

推荐答案

这将失败,因为的sizeof 是返回类型的值为size_t ,这是无符号的。这将导致比较推进 1 来签名,这通常是一个非常大的价值,从而作出比较失败。

This fails because sizeof is returning a value of type size_t, which is unsigned. This causes the comparison to promote the -1 to unsigned, which is generally a very large value and thus make the comparison fail.

您应该收到警告标志不匹配。

You should receive warnings for the sign mismatch.

这篇关于以阵列的打印元件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆