什么是“\\ n”的下C编译器的旧的Mac OS的价值? [英] What is the value of '\n' under C compilers for old Mac OS?

查看:131
本文介绍了什么是“\\ n”的下C编译器的旧的Mac OS的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

在Mac OS版本多达9个版本,文本文件的标准重新presentation使用的ASCII CR(回车)字符,十进制值13,标志着一个行的末尾。

In versions of Mac OS up to version 9, the standard representation for text files used an ASCII CR (carriage return) character, value decimal 13, to mark the end of a line.

的Mac OS 10,不像早期的版本,是类UNIX,并且使用ASCII LF(换行)字符,十进制值10,标志着一个行的末尾。

Mac OS 10, unlike earlier releases, is UNIX-like, and uses the ASCII LF (line feed) character, value decimal 10, to mark the end of a line.

现在的问题是,什么是字符常量的值的'\\ n''\\ r'在C和C ++编译器为Mac OS之前的OS X版本?

The question is, what are the values of the character constants '\n' and '\r' in C and C++ compilers for Mac OS releases prior to OS X?

有(至少),本来是可以采取两种可能的方法:

There are (at least) two possible approaches that could have been taken:


  1. 款待的'\\ n'作为ASCII码LF的字符,并将其转换成与由CR输出到和从文本流输入(类似于LF之间的转换和CR-LF在Windows系统上);或

  2. 善待的'\\ n'为ASCII CR字符,这就需要对输入或输出任何转换。

  1. Treat '\n' as the ASCII LF character, and convert it to and from CR on output to and input from text streams (similar to the conversion between LF and CR-LF on Windows systems); or
  2. Treat '\n' as the ASCII CR character, which requires no conversion on input or output.

有将与第二个方法的一些潜在问题。其中之一是,code,它假定的'\\ n'是LF可能会失败。 (如code是固有的非便携反正。)另一种是仍然需要为'\\ r',并在一个ASCII-一个独特的价值基于CR系统是唯一明智的价值。和C标准不允许的'\\ n'=='\\ r'(感谢mafso寻找引文,5.2.2条第3款),所以有些其他的值必须用于'\\ r'

There would be some potential problems with the second approach. One is that code that assumes '\n' is LF could fail. (Such code is inherently non-portable anyway.) The other is that there still needs to be a distinct value for '\r', and on an ASCII-based system CR is the only sensible value. And the C standard doesn't permit '\n' == '\r' (thanks to mafso for finding the citation, 5.2.2 paragraph 3), so some other value would have to be used for '\r'.

编译和Mac OS下的 N 的,执行的时候这是什么C程序的输出的 N 的小于10?

What is the output of this C program when compiled and executed under Mac OS N, for N less than 10?

#include <stdio.h>
int main(void) {
    printf("'\\n' = %d\n", '\n');
    printf("'\\r' = %d\n", '\r');
    if ('\n' == '\r') {
        printf("Hmm, this could be a problem\n");
    }
}

问题适用于C和C ++。我presume答案会是两个相同的。

The question applies to both C and C++. I presume the answer would be the same for both.

答案也可能有所差异,从一个C编译器到另一个 - 但我希望编译器实现者都保持相互一致。

The answer could also vary from one C compiler to another -- but I would hope that compiler implementers would have maintained consistency with each other.

需要明确的是,我不要求什么重的Mac OS presentation旧版本用于重新present结束行文本文件中的 的。我的问题是明确,只有约常量的'\\ n''\\ r'在C值或C ++源$ C ​​$ C。我知道,印刷的'\\ n'(无论其价值),以文本流会导致它被转换成系统的尾线重新presentation(在这种情况下,ASCII CR);由C标准要求的行为。

To be clear, I am not asking what representation old releases of Mac OS used to represent end-of-line in text files. My question is specifically and only about the values of the constants '\n' and '\r' in C or C++ source code. I'm aware that printing '\n' (whatever its value is) to a text stream causes it to be converted to the system's end-of-line representation (in this case, ASCII CR); that behavior is required by the C standard.

推荐答案

字符常量的值 \\ r \\ n 在经典Mac OS环境完全一样的,因为它是在其他地方: \\ r 是CR是ASCII 13(符进行); \\ n 是LF是ASCII 10(的0x0A )。这是对经典的Mac OS唯一不同的是, \\ r 被用作文本编辑器标准行结束,就像 \\ n 用于在UNIX系统上,或 \\ r \\ n 在DOS和Windows系统。

The values of the character constants \r and \n was the exact same in Classic Mac OS environments as it was everywhere else: \r was CR was ASCII 13 (0x0d); \n was LF was ASCII 10 (0x0a). The only thing that was different on Classic Mac OS was that \r was used as the "standard" line ending in text editors, just like \n is used on UNIX systems, or \r\n on DOS and Windows systems.

下面是在Mac上的Metrowerks的codeWarrior运行OS 9简单的测试程序的截图,例如:

Here's a screenshot of a simple test program running in Metrowerks CodeWarrior on Mac OS 9, for instance:

请即经典的Mac OS系统没有一个全系统的标准C库!像的printf函数()只有present为C $ cWarrior,其实施标准C编译器特定的库,例如SIOUX为$部分I / O写入输出在它的文本字段的窗口。因此,标准文件的一些实现I / O可能已执行 \\ r 之间的一些自动转换和 \\ n ,这可能是你在想什么。 (许多Windows系统做类似的事情\\ r \\ n 如果你没有通过B标志 fopen()函数,例如。)有肯定没有像在Mac OS的工具箱,虽然。

Keep in mind that Classic Mac OS systems didn't have a system-wide standard C library! Functions like printf() were only present as part of compiler-specific libraries like SIOUX for CodeWarrior, which implemented C standard I/O by writing output to a window with a text field in it. As such, some implementations of standard file I/O may have performed some automatic translation between \r and \n, which may be what you're thinking of. (Many Windows systems do similar things for \r\n if you don't pass the "b" flag to fopen(), for instance.) There was certainly nothing like that in the Mac OS Toolbox, though.

这篇关于什么是“\\ n”的下C编译器的旧的Mac OS的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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