Thread: using scanf() to read a character from standard input

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    17

    using scanf() to read a character from standard input

    so running the following code

    Code:
    #include <stdio.h>
    
    int main(void) {
            char c;
            int i=1;
    
    
            while (scanf("%c", &c)==1)
                    printf("loop sequence %i: %c(%i)\n", i++, c, (int)c);
            printf("\ndone\n");
    
    
            return 0;
    }
    would produce something like this

    Code:
    1
    loop sequence 1: 1(49)
    loop sequence 2:
    (10)
    2
    loop sequence 3: 2(50)
    loop sequence 4:
    (10)
    a
    loop sequence 5: a(97)
    loop sequence 6:
    (10)
    b
    loop sequence 7: b(98)
    loop sequence 8:
    (10)
    
    
    done
    it seems a "carriage return" serves two purposes here, one is to signal the program to read in the character typed in before the "carriage return", another serves as a second character typed, how can i do this cleanly, that is without having to use a "carriage return" as the second character to signal "i've typed in the first character already"

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    17
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to signal end of standard input using scanf()
    By brassbin in forum C Programming
    Replies: 9
    Last Post: 10-30-2013, 02:08 PM
  2. Why isn't the first line at the standard input read?
    By BenBusby in forum C Programming
    Replies: 3
    Last Post: 03-21-2013, 04:20 PM
  3. read a number with scanf() and read char[] with fgets()
    By nutzu2010 in forum C Programming
    Replies: 5
    Last Post: 03-11-2011, 05:05 AM
  4. read from standard input a list of filenames?
    By Roger in forum C Programming
    Replies: 1
    Last Post: 10-27-2009, 06:13 PM
  5. Replies: 17
    Last Post: 07-14-2009, 08:17 AM