Re: New Line Constant Error!

From: Chakkaradeep (Chakkaradeep_at_discussions.microsoft.com)
Date: 01/26/05


Date: Wed, 26 Jan 2005 01:03:06 -0800

Hi Joakim and Bruce,

Thanks a lot, i clearly understood this "new line thing" and i really thank
Bruce and Joakim for their Explanation.Good Job Guys..keep it up!

with regards,
C.C.Chakkaradeep

"Bruce Wood" wrote:

> In C# (as in C and C++), the backslash character ("\") has special
> meaning: it is an "escape" that combines with the character after it to
> make a special character that you can't type into your program. Some
> famous uses of it, all the way back to C, are \n for a newline
> (linefeed) control character, and \0 meaning the character with value 0
> (the NUL control character).
>
> So, any time you type a string and put a backslash \ in it, the
> compiler thinks that you want to indicate a special character, and
> tries to combine the backslash with the following character to create
> something else.
>
> However, what if you really want a backslash character in your string?
> What if you want \n to mean not a newline control character, but really
> a backslash followed by an en? Then you have to use a double-backslash,
> \\, which the compiler reads as a special sequence that folds down into
> a single backslash in the string itself.
>
> Take a look at the following code:
>
> string a = "\n";
> string b = "\\n";
>
> The string called "a" contains a _single character_: a newline, or
> linefeed control character, because the sequence backslash-en is
> interpreted by the compiler as a request for this special character.
>
> The string called "b" contains _two characters_: a backslash, which the
> compiler put into the string because it saw the "escaped backslash"
> sequence: two backslashes in a row... followed by an en character.
>
> Now in C and C++ this was all there was to it: everywhere in your
> string that you wanted a single backslash character, you had to type
> two into your program so that the compiler would know what to do. If
> you wanted two, you would have to type four into your program, etc.
> (Note that this is only the _compiler_ doing this, not the runtime. So,
> if you read in a line from a file that has the characters "\n" on it,
> these do _not_ get folded into a newline character, because the
> _compiler_ isn't reading the file... your program is.)
>
> However, because C# runs on the Windows operating system, and Windows
> traditionally uses backslash to separate directory names in file paths,
> Microsoft realized that forcing you to type double-backslashes for
> every backslash you wanted in a hard-coded string in your program was
> going to be... well, a pain in the ass. So, they added a new marker,
> the "@" marker.
>
> When you put an "@" at the beginning of a string, it means, "Compiler:
> there are no special characters in this string. Don't do your normal
> special-character processing on this one. Just put exactly what I type
> into the string." So, the declaration for string "b" above could be
> changed to:
>
> string b = @"\n";
>
> which produces the same thing internally: a two-character string
> containing a backslash and an en.
>
> In answer to your specific question, the compiler gives you an error
> because you said:
>
> int n = str_old.IndexOf('\');
>
> In this case, the backslash indicates a special character sequence, and
> the following character is a single quote. That tells the compiler that
> you really want a single quote mark in the character sequence, and that
> the single quote that directly follows the backslash is _not_ the
> ending delimiter for the character sequence. So, the compiler goes on
> adding characters to the sequence (closed parenthesis, then semicolon)
> until it arrives at the end of the line and realizes that something
> went wrong. In effect, you never ended the character constant because
> you told the compiler that the second single quote was "special" by
> preceding it with a backslash.
>
> As Joakim pointed out, you could fix this by either doubling the
> backslash, a la C:
>
> int n = str_old.IndexOf('\\');
>
> or adding the "@" marker to the start, to tell the compiler not to
> treat the backslash specially:
>
> int n = str_old.IndexOf(@'\');
>
>



Relevant Pages

  • Re: backslash-issues
    ... > valid character for the compiler (it is not part of the language's ... If i try to compile the following with my F-compiler: ... Error: test_backslash.f95, line 13: Character parameter BACKSLASH must ...
    (comp.lang.fortran)
  • Re: Eigenartiges Verhalten von "String[] java.lang.String.split(String regex)" - Falscher Split
    ... "It is an error to use a backslash prior to any alphabetic character ... Alle anderen Backslashes sind nicht erlaubt und ... musst du das mit ...
    (de.comp.lang.java)
  • Re: gfortran diagnostics and so on
    ... Well, in f0003, backslash is part of the standard Fortran character set. ... Because the backslash is part of the standard Fortran character set, the default behavior should be the printable character, **NOT** some kind of magic introductory character that transforms the interpretation of following character. ... The one I like best is to use one of the popular extensions to designate a particular literal string according to the C language. ...
    (comp.lang.fortran)
  • Re: New Line Constant Error!
    ... it is an "escape" that combines with the character after it to ... So, any time you type a string and put a backslash \ in it, the ... which the compiler reads as a special sequence that folds down into ... special-character processing on this one. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: bash-script -- filename conversion of Whitespace, "", ""
    ... bash 2.05b awk GNU awk 3.03 ... than one space character in a row, the result will only have one backslash-space combo. ... The backslash is retained and passed on to the ... An apostrophe does not need any backslash in front of it to ...
    (comp.os.linux.misc)