69,371
社区成员
发帖
与我相关
我的任务
分享
/*
sizeof返回的类型是无符号整数。
在if语句中,出现测试signed int和unsigned int之间的测试相等性。
所以d被升级为unsigned int类型,而-1被转换成unsigned int的结果将会是一个
非常巨大的数,所以表达式为假。
*/
#include <stdio.h>
#include <stdlib.h>
int array[] = { 1, 23, 4, 5, 43, 3};
#define TOTAL (sizeof(array)/sizeof(array[0]))
int main()
{
int d = -1, x;
if (d <= TOTAL)
printf("no error\n");
else
printf("error!\n");
system("pause");
return 0;
}
char line[100];
gets(line);
上面的代码变成下面更安全:
char line[100];
if (fgets(line, sizeof(line), stdin)==NULL)
exit(1);
8、可以将数组放到结构体中,以实现整体赋值。 #include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
jmp_buf buf;
void call() {
printf("in call() function\n");
longjmp(buf, 1);
printf("You'll never be here.Because I have used 'longjmp'. \n");
}
int main()
{
if (setjmp(buf))
printf("back in main\n");
else {
printf("first time through \n");
call();
}
system("pause");
return 0;
}
运行输出结果:
first time through
in call() function
back in main