當我從main調用函數wordToInt(c)
時,返回值與它在函數內打印的返回值不同。函數返回long long unsigned int的返回值不正確
main()
{
long long unsigned item;
char c[6] = "ABCDE";
item = wordToInt(c);
printf("Main The item is :%llu\n",item);
}
long long unsigned wordToInt(char *c)
{
long long unsigned int k,item=0;
int i,len;
int j,p=0;
len = (int) strlen(c);
for(i = len-1;i>=0;i--)
{
if(c[i] == 'c')
j = 42;
else if(c[i] == '*')
j = 99;
else
j = (int) c[i];
j = j%100;
k = pow(100,p);
p++;
item = item + (j*k);
}
printf("Function item is :%llu\n",item);
return item;
}
程序的輸出是:
Function item is :6566676869
Main The item is :18446744071686293893
誰能告訴我爲什麼會出現不一致的輸出?
*錯誤:初始化字符串的字符數組太長* - 編譯器不是有用的嗎? – chris
char c [5] =「ABCDE」;你不認爲你應該給大小爲6.一個額外的空字符。 –
@VikramSingh即使是6也會發生同樣的錯誤 – gopi