【发布时间】:2016-09-21 13:43:36
【问题描述】:
我不明白为什么下面的代码不能按我想要的那样工作。
int main()
{
char sentence[] = "this will be capitalised";
int i;
for (i=0; i<strlen(sentence); i++)
{
toupper(sentence[i]);
putchar(sentence[i]);
}
printf("\n\n");
return (0);
}
我最终得到了这个:
this will be capitalised
Program ended with exit code: 0
谢谢
【问题讨论】:
-
toupper返回大写字母。您必须分配回源字符串:sentence[i] = toupper(sentence[i]); -
或
putchar(toupper(sentence[i]))如果不再需要结果。 -
技术上应该是
toupper((unsigned char)sentence[i]),以防我们在EBCDIC机器上 -
@M.M 在 EBCDIC 机器上,
unsigned char通常不需要,因为char通常在那里没有签名。它是在其他具有char签名的常见情况的机器上,需要(unsigned char)演员。