while(batting_record!='/n');

This is a discussion on while(batting_record!='/n'); within the C Programming forums, part of the General Programming Boards category; What does this mean? while the input data is not a new line? or a / character n?...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    135

    while(batting_record!='/n');

    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
    Posts
    5,851
    '/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%.

  3. #3
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    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.

    Code:
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21