錯誤:溢出在隱恆定轉化率[-Werror =溢出]錯誤:溢出在隱恆定轉化率[-Werror =溢出]
#include<stdio.h>
int main()
{
char ch=200;
printf("\n%d",ch);
return 0;
}
IAM的運行上http://ideone.com/YNkKT6#view_edit_box此代碼和獲取的隱式轉換的錯誤。 我需要做什麼修改,原因是什麼?
錯誤:溢出在隱恆定轉化率[-Werror =溢出]錯誤:溢出在隱恆定轉化率[-Werror =溢出]
#include<stdio.h>
int main()
{
char ch=200;
printf("\n%d",ch);
return 0;
}
IAM的運行上http://ideone.com/YNkKT6#view_edit_box此代碼和獲取的隱式轉換的錯誤。 我需要做什麼修改,原因是什麼?
n3376 3.9.1/1
Plain char, signed char, and unsigned char are three distinct types. A char, a signed char, and an unsigned char occupy the same amount of storage and have the same alignment requirements (3.11); that is, they have the same object representation.
什麼是char
是實現定義的,因此,你需要unsigned char
這裏,它處理的值(0-255)。
看起來像你的char
是signed char
,它接受從-128到127的值。而200對於它來說太大了,並且會溢出成負數。
修復它,改變char
到int
或unsigned char
int main()
{
unsigned char ch=200;
printf("\n%d",ch);
return 0;
}
一個'char'是** **從來沒有一個'簽署char'。 'char'可以是有符號或無符號的(實現定義的),但它總是與'signed char'和'unsigned char'不同的類型。 – Angew
這是一個沒有區別的區別:C代碼的行爲在任何情況下都不能與'char'與'signed char'或'unsigned char'的同義詞區分開來。 (我對C++不太確定。) –
解決:)謝謝:) – sv1