C語言中的警告overflow in implicit constant conversion


程序很簡單,

 1 #include<stdio.h>
 2 #define Name  "Keal"
 3 #define Address "W.DC"
 4 #define Male "Man"
 5 #define With 40
 6 #define SPACE ' '
 7 void starbar(char ch,int num);
 8 int main(void)
 9 {    starbar('*',With);
10     putchar('\n');
11     
12     starbar(SPACE,18);
13     printf("%s\n",Name);
14     starbar(SPACE,18);
15     printf("%s\n",Address);
16     starbar(SPACE,19);
17     printf("%s\n",Male);
18     
19     starbar('#',With);
20     putchar('\n');
21     return 0;
22 }
23 void starbar (char ch, int num)
24 {    int i;
25     for(i=0;i<num;i++)
26     {
27         putchar(ch);
28         
29     }
30 }
簡單的小函數 程序

剛開始的時候,出現了 overflow in implicit constant conversion。

這個錯誤就是:常量轉換溢出。C語言中char, int, float, double,unsigned char, unsigned int 等數值有極限范圍,當它們之間(隱式)轉換時,可能因 數值極限 而超界 溢出。有的編譯器會報告這一類型的錯誤,並不是所有編譯器都會報告。

 

后來才發現,是因為 宏定義 

1 #define SPACE    '  '  //打了兩個空格
2 
3 #define SPACE    ' '  //打了一個空格

 那個starbar 函數函數原型 :

1 void starbar(char ch,int num);

所以多打了一個就是字符串了···嗚嗚,當然超出 char的范圍了·····以后這種 warning 還是要注意·····防患於未然,養成好習慣·····


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM