Thread: Need help. printf() and scanf().

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Did you try long long or unsigned long long? You could always just read a string and check each digit to see if it's a number until you run out of string.
    Code:
    char buf[ BUFSIZ ];
    scanf( "%[^0-9]\n", buf );
    printf( "You entered: %s\n", buf );

    Quzah.
    Last edited by quzah; 07-17-2011 at 05:25 AM.
    Hope is the first step on the road to disappointment.

  2. #17
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Unless you are using some brain dead compiler from then 1980s...

    Code:
    #include <stdint.h>
    #include <stdio.h>
    
    int main (void)
      { uint64_t x;   // also try.... unsigned long long int x;
         
         printf("Enter your number : ");
         scanf("%lld",&x);
         printf("%lld",x);
    
         return 0; }
    Will accept positive numbers up to 2^64 -1 or .... 18,446,744,073,709,551,615

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Bear in mind that if you're using visual studio, or one of the many MinGW based ports (which rely on the Microsoft runtime), that you have to use a non-standard format conversion for reading in long long's
    Format Specification Fields: scanf and wscanf Functions (CRT)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #19
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by quzah View Post
    Did you try long long or unsigned long long? You could always just read a string and check each digit to see if it's a number until you run out of string.
    Code:
    char buf[ BUFSIZ ];
    scanf( "%[^0-9]\n", buf );
    printf( "You entered: %s\n", buf );

    Quzah.
    If you're going to post example code, it should be code that cannot segfault. Here a buffer overrun is possible if a user enters a really long string of numbers.

    There's also a bug: ^ shouldn't be there.

    here's a better version, although I'd suggest using CommonTater's code if possible:
    Code:
    char buf[ 1024]={'\0'};
    scanf( "%1023[0-9]\n", buf );
    printf( "You entered: %s\n", buf );
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by King Mir View Post
    There's also a bug: ^ shouldn't be there.[/code]
    There you go. Nice catch. As to overflowing BUFSIZ, good luck with that. I'd be surprised if your shell allowed you to type that much. Oh, sure, I suppose you could pipe a file in, but you have to go out of your way to break it.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with printf and scanf
    By Teiji in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 11:27 PM
  2. printf() and scanf()
    By homeyg in forum C Programming
    Replies: 1
    Last Post: 02-06-2006, 10:43 PM
  3. scanf and printf
    By gmanUK in forum C Programming
    Replies: 5
    Last Post: 11-25-2005, 03:03 PM
  4. printf and scanf
    By studentc in forum C Programming
    Replies: 3
    Last Post: 06-11-2004, 03:07 PM
  5. something about printf (or maybe scanf)
    By netboy in forum C Programming
    Replies: 4
    Last Post: 06-11-2002, 10:26 PM