下面的代码在 ubuntu 14.04.3上可以编译,GCC 版本是 4.8.4
-
#include <stdio.h>
-
#include <stdlib.h>
-
int main()
-
{
-
int my_int;
-
-
printf("Arithmetic Operator");
-
-
my_int=6;
-
printf("my_int=%d,-my_int=%d\n",my_int,-my_int);
-
-
printf("int 1+2=%d\n",1+2);
-
printf("int 5-1=%d\n",5-1);
-
printf("int 5*2=%d\n",5*2);
-
-
// printf("\n9 div 4=2 remaind 1:\n");
-
printf("int 9/4=%d\n",9/4);
-
printf("int 9%%4=%d\n",9%4);
-
printf("double 9/4=%f\n",9.0/4.0);
-
-
-
my_int=10; //will overwrite previous my_int;
-
int my_int;
-
-
printf("Arithmetic Operator");
-
-
my_int=6;
-
printf("my_int=%d,-my_int=%d\n",my_int,-my_int);
-
-
printf("int 1+2=%d\n",1+2);
-
printf("int 5-1=%d\n",5-1);
-
printf("int 5*2=%d\n",5*2);
-
-
// printf("\n9 div 4=2 remaind 1:\n");
-
printf("int 9/4=%d\n",9/4);
-
printf("int 9%%4=%d\n",9%4);
-
printf("double 9/4=%f\n",9.0/4.0);
-
-
-
my_int=10; //will overwrite previous my_int;
-
printf("my_int=10: %d\n",my_int);
-
-
my_int++;
-
printf("my_int++: %d\n",my_int);
-
-
my_int+=5;
-
printf("my_int+=5: %d\n",my_int);
-
-
my_int--;
-
printf("my_int--: %d\n",my_int);
-
-
my_int-=2;
-
printf("my_int-=2: %d\n",my_int);
-
-
my_int*=5;
-
printf("my_int*=5: %d\n",my_int);
-
-
my_int/=2;
-
printf("my_int/=2: %d\n",my_int);
-
-
my_int%=3;
-
printf("my_int%%=3: %d\n",my_int);
-
-
return 0;
-
}
但是在 kali linux 上不能编译,gcc 版本是 5.4.0
编译报错如下。
-
gcc arithmetic.c -o arithmetic
-
arithmetic.c: In function ‘main’:
-
arithmetic.c:23:9: error: redeclaration of ‘my_int’ with no linkage
-
int my_int;
-
^
-
arithmetic.c:5:9: note: previous declaration of ‘my_int’ was here
-
int my_int
但是用{}就能解决掉这个问题:
我在第二次声明变量my_int 之前加了{} ,当然按照道理来说,最后声明的同名变量应该会覆盖前面的变量,所以在GCC 4.8.4 上没有报错,即使没有{}, 而在GCC 5.4.0 我就得
加上{}才能编译通过。
-
#include <stdio.h>
-
#include <stdlib.h>
-
int main()
-
{
-
int my_int;
-
-
printf("Arithmetic Operator");
-
-
my_int=6;
-
printf("my_int=%d,-my_int=%d\n",my_int,-my_int);
-
-
printf("int 1+2=%d\n",1+2);
-
printf("int 5-1=%d\n",5-1);
-
printf("int 5*2=%d\n",5*2);
-
-
// printf("\n9 div 4=2 remaind 1:\n");
-
printf("int 9/4=%d\n",9/4);
-
printf("int 9%%4=%d\n",9%4);
-
printf("double 9/4=%f\n",9.0/4.0);
-
-
-
my_int=10; //will overwrite previous my_int;
-
{
-
int my_int;
-
-
printf("Arithmetic Operator");
-
-
my_int=6;
-
printf("my_int=%d,-my_int=%d\n",my_int,-my_int);
-
-
printf("int 1+2=%d\n",1+2);
-
printf("int 5-1=%d\n",5-1);
-
printf("int 5*2=%d\n",5*2);
-
-
// printf("\n9 div 4=2 remaind 1:\n");
-
printf("int 9/4=%d\n",9/4);
-
printf("int 9%%4=%d\n",9%4);
-
printf("double 9/4=%f\n",9.0/4.0);
-
-
-
my_int=10; //will overwrite previous my_int;
-
printf("my_int=10: %d\n",my_int);
-
-
my_int++;
-
printf("my_int++: %d\n",my_int);
-
-
my_int+=5;
-
printf("my_int+=5: %d\n",my_int);
-
-
my_int--;
-
printf("my_int--: %d\n",my_int);
-
-
my_int-=2;
-
printf("my_int-=2: %d\n",my_int);
-
-
my_int*=5;
-
printf("my_int*=5: %d\n",my_int);
-
-
my_int/=2;
-
printf("my_int/=2: %d\n",my_int);
-
-
my_int%=3;
-
printf("my_int%%=3: %d\n",my_int);
-
}
-
return 0;
-
}
阅读(1279) | 评论(0) | 转发(0) |