是否基本字符集只能在C实现依赖? [英] Does the underlying character set depend only on the C implementation?

查看:134
本文介绍了是否基本字符集只能在C实现依赖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多文章警告说,处理字符值作为整数是不可移植的,例如假设'A'的值是65(如ASCII)。

Many texts warn that processing char values as integers isn't portable, e.g. assuming that the value of 'A' is 65 (as in ASCII).

但是,确定此字符集是ASCII(或扩展形式),或其它字符集?是它由操作系统,或编译器来确定我presuming,这是不依赖于硬件

But what determines whether this character set is ASCII (or an extended form), or some other character set? Is it determined by the operating system, or the compiler? I'm presuming that this isn't dependent on the hardware.

例如,可以在英特尔的PC有个字符集如EBCDIC(理论上)?并可能改变的Linux / Unix的 LANG 环境变量更改为C程序设计的基本字符集的值(如果再重新编译)?

For example, could an Intel PC have a character set such as EBCDIC (in theory)? And could changing the LANG environment variable in Linux/Unix change the values of the basic character set for C programs (if then recompiled)?

(编辑:我现在看到的各种非拉丁字符集在Linux中都具有相同的基本ASCII codeS,如的 KOI8-U - 我认为有迹象表明,有性格的变化集不兼容ASCII)

(edit: I see now that the various non-Latin character sets in Linux all have the same basic ASCII codes, e.g. KOI8-U - I assumed that there were variations that had character sets not compatible with ASCII)

推荐答案

标准不关心任何这些细节,就因为它是关注有唯一的实施。

The standard doesn't care about any of those details, as far as it's concerned there's only "the implementation".

在实践中,硬件和操作系统既可以指定执行的细节,该平台上的C语言实现,预计使用,或者说他们的需要的使用,如果他们想用的系统功能进行互操作(即,与该操作系统或与硬件提供code)所示。所以我们经常说这样的话,在Win32,的sizeof(无效*)== 4 。这是一个缩写,不过,因为有人的可能的,如果他们选择了,写在32位Windows上运行,并有不同大小的指针C实现。我们真正的意思是,在Win32 ABI,在Win32平台上运行的sizeof(无效*)== 4 和C实现不遵循Win32的ABI被排除在外从考虑。

In practice, hardware and OSes can both specify implementation details that C implementations on that platform are expected to use, or that they're required to use if they want to inter-operate with system functions (that is to say, code that is supplied with the OS or with the hardware). So we often say things like, "on Win32, sizeof(void*) == 4". This is a shorthand, though, since someone could, if they chose, write a C implementation that runs on 32 bit Windows and has a different pointer size. What we really mean is, "in the Win32 ABI, sizeof(void*) == 4, and C implementations running on Win32 that don't follow the Win32 ABI are excluded from consideration".

实现可以为所欲为,只要他们不介意他们是否可以(例如)使用下面的系统约定的DLL。字符集可以被定义然而编译器和标准库的作者喜欢,只受什么标准。

Implementations therefore can do whatever they like, provided they don't mind whether or not they can (for example) use dlls that follow the system's conventions. The character set can be defined however the writer of the compiler and standard libraries likes, subject only to what's in the standard.

这是说,字符文字的值是编译时常数。这告诉你,基本执行字符集不能在运行时更改。

That said, the values of character literals are compile-time constants. This tells you that the basic execution character set cannot change during runtime.

此外,如果它是依赖于一个环境变量那么这将是谁的责任,以确保程序具有相同的价值,它正在与编译运行。这将是pretty用户不友好,但是标准不实际的禁止的有人写奇特限制如何程序运行C实现。

Furthermore, if it were to depend on an environment variable then it would be somebody's responsibility to ensure that the program was run with the same value that it was compiled with. This would be pretty user-unfriendly, but the standard doesn't actually forbid someone from writing a C implementation with peculiar restrictions on how programs are run.

这篇关于是否基本字符集只能在C实现依赖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆