Thread: Get rid of space, use scanf or getchar. Which is better?

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    83

    Get rid of space, use scanf or getchar. Which is better?

    Hi,

    I want the user to be able to enter a command then a character, like for example:

    push r

    I want the command to be stored in the array command, and the character to be stored in the variable c.

    Now I wonder what the best way to get rid of the space is, using scanf or getchar (see below for code, only thing that I changed between the 2 versions is the statement before the comment "get rid of space")? Or maybe it doesnt matter?

    Code:
    include <stdio.h>
    #define MAX 200
    
    void push(char c);       // Puts a new element last in queue
    char pop(void);            // Gets the first element in queue
    
    static char s[MAX];
    
    
    int main(void)
    {
        char c;
        char command[10];
        
        printf("Enter push character to put a new element in in queue. Enter pop to retrieve firs element in queue:\n");
        scanf("%s", command);
       
        scanf("%c", &c);    // Get rid of space
        scanf("%c", &c);
        
        printf("command: %s\n", command);
        
        printf("Element: %c\n", c);
        
        return 0;
        
    }

    Code:
    include <stdio.h>
    #define MAX 200
    
    void push(char c);       // Puts a new element last in queue
    char pop(void);            // Gets the first element in queue
    
    static char s[MAX];
    
    
    int main(void)
    {
        char c;
        char command[10];
        
        printf("Enter push character to put a new element in in queue. Enter pop to retrieve firs element in queue:\n");
        scanf("%s", command);
    
        getchar();                         // Get rid of space
        scanf("%c", &c);
        
        printf("command: %s\n", command);
        
        printf("Element: %c\n", c);
        
        return 0;
        
    }
    Thanks if you want to help.
    Last edited by jjohan; 09-24-2014 at 01:15 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    This is bad.

    Code:
        scanf("%c", &c);    // Get rid of space
        scanf("%c", &c);
    Change it to this and it is best!

    Code:
        scanf(" %c", &c);    // skip leading space

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You can get the string and the character in one line:

    Code:
    scanf("%s %c", command,&c);
    This would also eliminate a potential problem with your initial code, if the user enters more than one space between the command and the character.

    For truly robust input handling, though, I'd suggest reading the entire input with "fgets()" and parsing the string as needed.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    If you just want to ignore spaces, you can place a space character in the format string to scanf. This will skip over 0 or more whitespace characters, so user can type "push a" or "push a" and the result will be the same. Example

    Code:
    scanf(" %d", &x); // skip over whitespace and then read in an int and store it in location &x

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    83
    Thank you all for giving good advice!

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by c99tutorial View Post
    If you just want to ignore spaces, you can place a space character in the format string to scanf. This will skip over 0 or more whitespace characters, so user can type "push a" or "push a" and the result will be the same. Example

    Code:
    scanf(" %d", &x); // skip over whitespace and then read in an int and store it in location &x
    Please try NOT to post this stuff; the only time that a space really seems to help is in front of a %c in all other cases it normally does nothing useful.

    FYI: The %d skips over white-spaces by default!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by c99tutorial View Post
    If you just want to ignore spaces, you can place a space character in the format string to scanf. This will skip over 0 or more whitespace characters, so user can type "push a" or "push a" and the result will be the same. Example

    Code:
    scanf(" %d", &x); // skip over whitespace and then read in an int and store it in location &x
    A trailing new line character does not affect an integer input, only character input.

    [edit]
    Beaten to replying again - Today is not my day!
    [/edit]
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf getchar
    By hit in forum C Programming
    Replies: 5
    Last Post: 08-21-2012, 02:47 AM
  2. getchar and scanf
    By begin in forum C Programming
    Replies: 9
    Last Post: 06-20-2010, 07:51 PM
  3. getchar won't take any input after scanf
    By pshirishreddy in forum C Programming
    Replies: 2
    Last Post: 08-02-2009, 11:46 AM
  4. scanf, getchar, gets, etc. are ignored.
    By daltore in forum C Programming
    Replies: 19
    Last Post: 12-27-2007, 10:47 PM
  5. scanf and getchar
    By axe in forum C Programming
    Replies: 7
    Last Post: 01-11-2006, 04:45 AM