Thread: while(batting_record!='/n');

  1. #1
    Registered User
    Join Date
    Oct 2012


    What does this mean?

    while the input data is not a new line? or a / character n?

  2. #2
    Registered User
    Join Date
    Jun 2005
    '/n' will cause a compilation error.

    Within a character constant or a string literal, character sequences starting with a \ (backslash, not forward slash) are called escape sequences. The escape sequence \n (backslash followed by an n) represents a newline. So, '\n' is a single character, and "\n" is a string literal consisting of two characters, one with value '\n' and the other zero.

    Note that \n is not the only escape sequence. Any basic C text will describe them, or (noting that you should sanity-check most things you find on the internet, including this post) Google for "C escape sequence"
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2012
    This is, in all likelyhood, a programmer mistake (she must have wanted a backslash and the absence of the semicolon).

    As it is, it's either an infinite loop or a do nothing statement depending on the value of batting_record.
    If batting_record has a value of '/n' (a multi-character constant which value is implementation defined) it is a do nothing statement
    if batting_record has a different value it is an infinite loop: doing nothing (empty body) does not change the value and all it does (over and over and over) is compare the values.

    int batting_record;
    while (batting_record != '\n') {
        batting_record = getchar();
    This code, on the other hand, keeps reading characters from the standard input stream up to and including a newline character. Note that this code does not check for errors, as a proper code should do.

Popular pages Recent additions subscribe to a feed