This is the mail archive of the
mailing list for the Cygwin project.
Re: Broken C code (specifically, gcc and \r)
- To: cygwin at sourceware dot cygnus dot com
- Subject: Re: Broken C code (specifically, gcc and \r)
- From: "13mb80000-HallM(10053584)37x10" <marcus at bighorn dot dr dot lucent dot com>
- Date: Fri, 28 Jan 2000 09:51:18 -0700 (MST)
- Reply-To: "13mb80000-HallM(10053584)37x10" <marcus at bighorn dot dr dot lucent dot com>
> I do not know whether it is intentional or accidental but gcc (and several
> other tools) seem to handle \n and \r\n as identical. (Even WordPad can do
If you look at ctype, you will find that \r is defined as a whitespace
character, so gcc treats it as such, just like \n. So, wherever you have
\r\n you just have two whitespace characters and \n gives just one. To
C and C++, any string of whitespace characters looks the same.
I am not sure if this has been addressed by newer versions of gcc, but as of
a year or two ago, there is one case where the extra \r before a newline is
very significant to C/C++. Actually, it's the pre-processor. Consider a
#define that looks like:
#define MYSYMBOL part of the definition is on this line \
and the rest is on this line.
Now, the backslash at the end of the first line, because it is followed by
a newline, continues the definition to the following line. But, if the
line ending is \r\n, then the backslash isn't followed by a newline at all,
but by a \r (a whitespace character). Since there is no particular
interpretation of '\' followed by '\r', the sequence is replaced by the
second character (just like "\ " would give a space, or "\y" would give
'y'). Now, the next character is a \n character without a preceeding \,
so it terminates the macro definition.
Maybe (hopefully!) cpp has been extended to understand that the three
character sequence '\' '\r' '\n' should be treated the same as '\' '\n',
but beware just in case...
Want to unsubscribe from this list?
Send a message to firstname.lastname@example.org