小编Agg*_*969的帖子

C 宏列表大小解析但不进入 for 循环

我有以下代码,当我尝试使用 sizeof list a 作为 for 循环中的条件时,它不会进入它,但如果我对其进行硬编码,它就会进入循环。为什么会发生这种行为?

#include <limits.h>
#include <malloc.h>
#include <stdio.h>

#define MY_VAL (sizeof(a) / sizeof(a[0]))
#define MY_NEW_VAL 5

int a[] = {1, 2, 3, 4, 5, 6, 7};

int main() {
  int i;
  for (i = -1; i <= (MY_VAL - 2); i++) {
    printf("In loop 1\n");
  }
  for (i = -1; i <= MY_NEW_VAL; i++) {
    printf("In loop 2\n");
  }
}
Run Code Online (Sandbox Code Playgroud)

输出-->

In loop 2
In loop 2
In loop 2
In loop 2
In loop …
Run Code Online (Sandbox Code Playgroud)

c macros

1
推荐指数
1
解决办法
60
查看次数

标签 统计

c ×1

macros ×1