如何在 C 程序中用 iso_8859-1 输出度数符号?

how to output degree symbol in C programs with iso_8859-1?

我查了iso_8859-1的说明书,找到度数符号:

Oct   Dec   Hex   Char   Description
260   176   B0     °     DEGREE SIGN

代码:

int main( int argc, char *argv[])
{
    char chr = 176; //stores the extended ASCII of a symbol
    printf("Character with an ascii code of 251: %c \n", chr);
    return 0;
}

但它打印出来是 ?

如何在C程序中输出度数符号?我需要包含一些文件吗?

要使用 ASCII 以外的其他字符,在大多数平台上,您必须切换到能够处理此类字符的 "locale"。在 main 的开头,您应该

   #include <locale.h>
   int main(void) {
     setlocale(LC_ALL, "");
   ...
   }

此处的空字符串 "" 字面上代表您平台的默认语言环境。

现在,您的平台支持什么语言环境是您必须自己找出的,您没有给我们足够的信息。我强烈建议不要像你提议的那样使用过时的,而是使用 UTF-8。

通常你可以在普通的 C 字符串中自然地嵌入 UTF-8 字符序列,所以像 "105° C" 这样的东西应该作为度数字符打印出来。

摄氏度的 ASCII 码是 248。您可以在 C 中将其声明为字符。

#include<stdio.h>
void main()
{
char ch=248;
printf("Today's temperature was 23%cC",ch);
}