三元運算符存在的隱式轉換現象

#include<stdio.h>

int main(){
	int x = 4;
    printf("%d\n",x>4?99.0:9);
	printf("%d\n",x>4?99:9);
	printf("%d\n",x>4?99:9.0);
	printf("%f\n",x>4?99:9.0);


    if(x>4){
         printf("%d\n",99.0);
	}else{
	     printf("%f\n",9);
	}
}

編譯警告

 gcc -g -Wall test_three.cpp -o test_three
test_three.cpp: In function ‘int main()’:
test_three.cpp:5:33: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat=]
         printf("%d\n",x>4?99.0:9);
                                 ^
test_three.cpp:7:26: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat=]
  printf("%d\n",x>4?99:9.0);
                          ^
test_three.cpp:12:28: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat=]
          printf("%d\n",99.0);
                            ^
test_three.cpp:14:18: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=]
   printf("%f\n",9);

運行結果:

  ./test_three 
-2076639704
9
2147483646
9.000000
9.000000

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章