This is the mail archive of the mailing list for the Cygwin project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Broken C code (specifically, gcc and \r)

> 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
> it.)

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...

marcus hall

Want to unsubscribe from this list?
Send a message to

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]