有人能解释一下sizeof运算符吗
这个是一个用户输入年份,然后打印该年每个月的天数的程序,如果是是闰年,2月份会打印29天然后小甲鱼用到了一个我从没见过的表达形式,在程序的第16行,也就是下面这一段,有人能解释一下这段程序的意思吗? sizeof(days)是计算出了days这个数组的占位,是12,然后i<12然后又除以sizeof(days,结果是1吗?然后i<12/1?
for (i = 0; i < sizeof(days) / sizeof(days); i++)
#include <stdio.h>
int main()
{
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int i, year;
printf("请输入一个年份:");
scanf("%d", &year);
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
{
days = 29;
}
for (i = 0; i < sizeof(days) / sizeof(days); i++)
{
printf("%2d月份:%d天\n", i+1, days);
}
return 0;
} 噢,一个int是占4个字节的,所以days占了48个,而days【0】占了4个 上面的是小甲鱼的程序,我程序就比较复杂了#include <stdio.h>
int main() {
int a, c, d = 0, e = 0, f;
do {
printf("请输入一个年份:");
scanf("%d", &a);
if (a % 4 == 0) {
for (c = 0; c < 12; c++) {
int b = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
printf("%d月份:%d天\n", c + 1, b);
e++;
}
e = 0;
} else {
for (f = 0; f < 12; f++) {
int g = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
printf("%d月份:%d天\n", f + 1, g);
d++;
}
d = 0;
}
} while (1);
return 0;
} https://fishc.com.cn/thread-211803-1-1.html
页:
[1]