scanf

This is a discussion on scanf within the C Programming forums, part of the General Programming Boards category; I have been writing a program that uses scanf to scan in a number. It doesn't seem to work very ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    9

    scanf

    I have been writing a program that uses scanf to scan in a number. It doesn't seem to work very well. I have heard that scanf does not do error checking. But is there a way to check the return status of scanf to see if it read in a number correctly? if not, is there a better way to do this? And yes, I have read the FAQ's, but still haven't got to the point where I feel comfortable using pointers and arrays yet, so some of the examples there can be kind of confusing.

    Next, what is the difference between the "return" and "exit" functions? They obviously do something different, but I am not sure what.

    And last, how do I include debugging information in my program?

    I am using gcc 2.95 running on a SunOS 4.1.4 with a sparc processor.

    Any advice would be appreciated.

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    Instead of scanf, take a look at fgets. Then you can convert the string to an int using atoi or to a float using atof.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    9
    OK, thanks. One more question. Can you look up every function in C with the man pages on a UNIX system? I've noticed that i can do that for some, but have only tried a few so far.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    9
    Ok, thanks. That might make it a little easier to get a grip on all of these functions that people keep mentioning.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    Using strtol() is probably the best approach as mentioned by others.

    There are a couple of things to be aware of when using it however. For your purposes, the most important is probably that strtol() stops reading the given string at the first character it cannot recognise as part of a number. Hence, if I were to write:

    Code:
    strtol("45fool", NULL, 10)
    The return value would be 45. I remember when doing an assignment once I had to scan the string to ensure all the characters were digits before passing it to strtol (this scanning was achieved by some function in string.h - although it could be written seperately also)
    Beware the fury of a patient man.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Looking at forbidden information shows a way to use strtol in which the second argument is not NULL, allowing the programmer to validate the potential number without using anything from string.h. Adapting its example,
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
       char buf[] = "45fool";
       char *p;
       long int i = strtol(buf, &p, 10);
       if ( buf[0] != '\n' && (*p == '\n' || *p == '\0') )
       {
          printf ("Valid number of %ld entered\n", i);
       }
       else
       {
          printf ("Invalid number entered\n");
       }
       return(0);
    }
    
    /* my output
    Invalid number entered
    */
    This allows such things as validation of non-decimal values as well, alleviating the need to check for digit characters. For example,
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
       char buf[] = "123abc";
       char *p;
       long int i = strtol(buf, &p, 16);
       if ( buf[0] != '\n' && (*p == '\n' || *p == '\0') )
       {
          printf ("Valid number of %ld entered\n", i);
       }
       else
       {
          printf ("Invalid number entered\n");
       }
       return(0);
    }
    
    /* my output
    Valid number of 1194684 entered
    */
    And the same could be done for smaller bases in which even some decimal characters would not be valid.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    Looking at forbidden information shows a way to use strtol in which the second argument is not NULL, allowing the programmer to validate the potential number without using anything from string.h
    Yep.
    There are many different ways to approach parsing a number, and this is another one of them. Generally when parsing input, I opt for my own custom validation as there is a great deal more flexibility.
    Beware the fury of a patient man.

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    61

    Re: scanf

    >> Originally posted by Gsibbery
    >> And last, how do I include debugging information in my program?

    Use gcc's -g switch.
    $ENV: FreeBSD, gcc, emacs

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 07:35 AM
  2. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 03:39 PM
  3. Replies: 2
    Last Post: 02-20-2005, 12:48 PM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21