Thread: using while getchar to flush the line

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    using while getchar to flush the line

    if i set up a #define FLUSH while((c=getchar()) != '\n') its quite happy with me using the macro
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define FLUSH while((c=getchar()) != '\n')
    void flush_line(void);
    int main()
    {
        int x = -1, y = 0;
        //char ch;
    
        printf("please enter a number: ");
        scanf(" %1d", &x);
        flush_line();
        printf("please enter a second number: ");
        scanf("%1d", &y);
        printf("\n x = %d and y = %d\n", x, y);
        return 0;
    }
    
    void flush_line(void)
    {
        char c;
        FLUSH;
    }
    however if i delete the char c it doesn't work any more and i get a compiler error saying first time use of variable c.

    Is there a way round this or am i doing something wrong

    many thanks
    coop
    Last edited by cooper1200; 05-07-2019 at 04:44 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You obviously need to declare a variable in order to use it. That it is used in a macro makes no difference as the macro will be replaced with the code associated with the macro.

    I suggest just writing a self-contained function, which you already have with flush_line, so a macro confers no benefit at all: if the compiler will inline the function call, it will inline whether or not you have your FLUSH macro, and if it won't, you incur the function call overhead even with your FLUSH macro, so the macro is pointless.

    If you must have a macro, then maybe:
    Code:
    #define FLUSH do { int c; while ((c = getchar()) != '\n' && c != EOF); } while (0)
    Note that the correct type of c is int because getchar returns an int, and EOF could be outside of the range of char. The do ... while (0) technique is used for multi-line macros to avoid potential problems with forgetting the terminating semi-colon in the code that uses the macro. You would then ditch your flush_line function.

    A better approach since C99 would be to declare flush_line inline in the header, no macro involved, then let the compiler decide whether or not to inline. I wouldn't bother too much though: the timing here is likely to be dominated by I/O, so function call overhead is insignificant.
    Last edited by laserlight; 05-07-2019 at 05:55 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using getchar() to flush newline from input buffer
    By Valour549 in forum C Programming
    Replies: 15
    Last Post: 11-03-2018, 07:49 AM
  2. flush?
    By mp252 in forum C++ Programming
    Replies: 3
    Last Post: 06-22-2013, 10:25 PM
  3. Question on getchar to flush scanf
    By lotrfan in forum C Programming
    Replies: 1
    Last Post: 03-15-2012, 04:59 PM
  4. getchar putchar: a word on each line
    By krazymanrebirth in forum C Programming
    Replies: 27
    Last Post: 09-29-2009, 02:57 PM
  5. Getchar terminating with new line or EOF
    By rocksteady in forum C Programming
    Replies: 1
    Last Post: 10-19-2007, 01:19 PM

Tags for this Thread