Code explanation

This is a discussion on Code explanation within the C Programming forums, part of the General Programming Boards category; Can anyone tell me what is the use of the temporary variable "int pch" in the following code: Code: int ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    31

    Code explanation

    Can anyone tell me what is the use of the temporary variable "int pch" in the following code:

    Code:
    int skip_record(int cch)
    {
      int pch = EOF;		/* previous char   */
      while ( (cch!= EOF) && ((cch!='\n')  ||  (pch!='\n')) )
        {
          pch = cch;
          cch = getchar();
        }
      while (cch=='\n') cch = getchar();
      return cch;
    }

  2. #2
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    To keep track of what the previous character was.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  3. #3
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    If I read it correctly, the first while loop is going to need 2 consecutive newlines to get out of it. The pch variable keeps track of what the last character was, and both it and the current character has to be newlines to exit (or EOF).

    It looks slightly odd to me out of context and I'm not very good at looking over things like that and interpreting them correctly.
    What is C++?

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    31

    while loops

    Quote Originally Posted by Vicious View Post
    If I read it correctly, the first while loop is going to need 2 consecutive newlines to get out of it. The pch variable keeps track of what the last character was, and both it and the current character has to be newlines to exit (or EOF).

    It looks slightly odd to me out of context and I'm not very good at looking over things like that and interpreting them correctly.
    But what's the use of keeping track of newlines using pch and cch variables?
    what's the use of the second while loop? Is it to skip the newlines in the input?

  5. #5
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    The second while loop shouldn't be reached until the first one has exited. The way it's set up, I am really not sure how that loop will ever execute unless you hit EOF in the first loop. As far as the why goes. I am not sure. It's hard to tell what the intentions are out of context.

    (Again, I may be mis-reading the logic also. Take what I say with a grain of salt)
    What is C++?

  6. #6
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    It will eat input until the data comes in with at least 2 new lines in a row (or an EOF). After that, unless EOF was reached it will eat newline characters until something other than a newline is reached and then return that character.

    From the name of the function, I would assume it is simply skipping all input until it encounters '\n' twice in a row (the end of record marker?) or EOF and then returns the character immediately following the skipped record (or EOF if that was received).

    Without more context I don't think we can give you much more info. For example, I don't know why it is given the initial character as a function parameter. Perhaps it is called by something like
    Code:
    int next_char = skip_record(getchar());
    but consider
    Code:
    int skip_record()
    {
      int pch = EOF;		/* previous char   */
      int cch = getchar();		/* current char    */
      while ( (cch!= EOF) && ((cch!='\n')  ||  (pch!='\n')) )
        {
          pch = cch;
          cch = getchar();
        }
      while (cch=='\n') cch = getchar();
      return cch;
    }
    Since skip_record() doesn't do anything with cch except use it as a control in the while condition, the only reason I can think of to pass cch in rather than do it as my modified function shows is so that the caller can pass EOF to the function to make it basically do nothing.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    Quote Originally Posted by Vicious View Post
    The second while loop shouldn't be reached until the first one has exited. The way it's set up, I am really not sure how that loop will ever execute unless you hit EOF in the first loop. As far as the why goes. I am not sure. It's hard to tell what the intentions are out of context.

    (Again, I may be mis-reading the logic also. Take what I say with a grain of salt)
    I think it's a bogus code with bad logic.
    First while loop: EOF && FALSE -> breaks the first while loop...
    reaches the second while loop, where it skips all the newline characters and returns non newline char.
    But I don't get it -- what's the use of the "pch" the previous character? What is pch useful for?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The first loop will exit provided (1) cch is EOF or (2) cch and pch is a newline. So we need two newlines in a row, and pch keeps track of the last one.

  9. #9
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    It is one way to count several instances of repeating character. Since we are always keeping track of the previous character and the current character, then if both prev and current are '\n', we have 2 newlines in a row.

    Another method could be
    Code:
    int skip_record(int cch)
    {
      int occurrences = 0;  /* occurrences of '\n' in input */
      while ( (cch!= EOF) && (occurrences < 2) )
        {
          if (cch == '\n') 
            occurrences++;
          else
            occurrences = 0;
          cch = getchar();
        }
      while (cch=='\n') cch = getchar();
      return cch;
    }
    Now if 2 occurrences of '\n' appear one after the other, occurrences will be incremented twice and the while loop will terminate because occurrences is no longer less than 2, but if something else comes through between the two '\n' characters then occurrences will reset to 0.

    This version of the function is almost identical, just a different way of counting 2 concurrent newline characters from input. One advantage this would have is that you could more easily change what character you are counting, or you could wait for more than just 2 in a row. However this is probably getting away from the intent of this particular function and is really academic since we still have no idea what the context of this single function is.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    Quote Originally Posted by jEssYcAt View Post
    It is one way to count several instances of repeating character. Since we are always keeping track of the previous character and the current character, then if both prev and current are '\n', we have 2 newlines in a row.

    Another method could be
    Code:
    int skip_record(int cch)
    {
      int occurrences = 0;  /* occurrences of '\n' in input */
      while ( (cch!= EOF) && (occurrences < 2) )
        {
          if (cch == '\n') 
            occurrences++;
          else
            occurrences = 0;
          cch = getchar();
        }
      while (cch=='\n') cch = getchar();
      return cch;
    }
    Now if 2 occurrences of '\n' appear one after the other, occurrences will be incremented twice and the while loop will terminate because occurrences is no longer less than 2, but if something else comes through between the two '\n' characters then occurrences will reset to 0.

    This version of the function is almost identical, just a different way of counting 2 concurrent newline characters from input. One advantage this would have is that you could more easily change what character you are counting, or you could wait for more than just 2 in a row. However this is probably getting away from the intent of this particular function and is really academic since we still have no idea what the context of this single function is.


    If input is
    % A 12345566\n --- (1)
    % A 12323232\n ---(2)
    \n
    % A 12344 ---(3)

    won't the skip_record(int cch) eat up the line (2) without processing it?
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Code Explanation
    By dmkanz07 in forum C Programming
    Replies: 1
    Last Post: 03-27-2007, 08:24 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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