|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
请问为什么编译不成功?
1 #include <stdio.h>
2
3 int main()
4 {
5
6 int i;
7 char j;
8 float k;
9
10 i=123;
11 j='c';
12 k=3.14;
13
14 printf("size of int is %d\n",sizeof(int));
15 printf("size of char is %d\n",sizeof(char));
16 printf("size of k is %d\n",sizeof k);
17 return 0;
18 }
19
下面是错误提示:
xinruiliu ~/1/1-1$ gcc 1.c
1.c:14:30: warning: format specifies type 'int' but the argument has type
'unsigned long' [-Wformat]
printf("size of int is %d\n",sizeof(int));
~~ ^~~~~~~~~~~
%lu
1.c:15:31: warning: format specifies type 'int' but the argument has type
'unsigned long' [-Wformat]
printf("size of char is %d\n",sizeof(char));
~~ ^~~~~~~~~~~~
%lu
1.c:16:28: warning: format specifies type 'int' but the argument has type
'unsigned long' [-Wformat]
printf("size of k is %d\n",sizeof k);
~~ ^~~~~~~~
%lu
3 warnings generated.
这不是错误。是三个警告,这个没啥问题
warning是警告,error才是错误
这个警告可以不消除,不影响执行,但是若想消除也是可以的。
warning中已经给出了警告的原因,sizeof的返回值是unsigned long类型,格式化应该用%lu,而你用的是%d。
你将三个%d都改为%lu即可。
- #include <stdio.h>
- int main() {
- int i;
- char j;
- float k;
- i = 123;
- j = 'c';
- k = 3.14;
- printf("size of int is %lu\n", sizeof(int));
- printf("size of char is %lu\n", sizeof(char));
- printf("size of k is %lu\n", sizeof k);
- return 0;
- }
复制代码
|
|