Thread: scanf question

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    4

    scanf question

    Hey guys, I am pretty new to C. Is there anyway to use scanf to ignore letters and just read digits?

    Would this be valid syntax within the scanf method:

    scanf("%*[^0-9]d");

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    11
    Try the following ways,

    Code:
    scanf("%[0-9]s",&s);              // Read only digits.
    scanf("%[^0-9\n]s", s);          // Read other than digits up to newline.
    or

    Code:
    scanf(" %[^\n]", s);               // read all whitespace, then store all characters up to a newline

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    Are those implementations reading and storing strings?

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    Yeah,it will seek the input from user by reading from standard input and stores the given value to the pointer specified as the second argument to scanf function which is given after the format specifier.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: scanf question

    Actually, your syntax is wrong.
    scanf("%*[^0-9]d");

    Normally, If you used %d in scanf it will read only digits.
    For Ex: If I give 123abc it will take only 123. But if you give abc123. It will take some garbage value.

    And you are trying to use [ range ] in scanf.
    It will also not suitable.

    Note: [ range ] will not have any format characters. And it will read and store the value as string. It needs to specify like,
    scanf("[^0-9]",s); // Correct
    scanf("[^0-9]s",s); // Wrong

    You can try some other way to read only integers.
    Try,
    * getchar() check whether the character is in 0-9 range.

    And If you want to read and store integers try to use , getw() or fread() but this will be in binary text. not as like normal text. For more clarification read about "fread"

    scanf() will not suitable for your requirement.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    Thank you very much guys, you helped a lot. I have a better understanding now.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    scanf("[^0-9]",s); // Correct
    you actually missing %
    Code:
    scanf("%[^0-9]",s); // Correct
    adding * you can avoid storing the string you want just skip
    Code:
    scanf("%*[^0-9]"); // Correct
    And I suppose - as the above parsing should stop at the first digit - you can just add format for integer after that

    Code:
    int n;
    scanf("%*[^0-9]%d", &n); // Correct
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Scanf question for beginner
    By somekid413 in forum C Programming
    Replies: 1
    Last Post: 12-15-2009, 04:56 PM
  2. Question about scanf and spaces or whitespace
    By jensbodal in forum C Programming
    Replies: 4
    Last Post: 11-26-2009, 04:12 PM
  3. Replies: 7
    Last Post: 05-25-2006, 12:51 PM
  4. Scanf Question
    By shiyu in forum C Programming
    Replies: 4
    Last Post: 01-31-2003, 08:48 AM
  5. Simple question with scanf()
    By MadStrum! in forum C Programming
    Replies: 3
    Last Post: 01-20-2003, 10:41 AM