Thread: Do most people store numbers in char?

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    49

    Do most people store numbers in char?

    Hi, I am still new to c so this might sound like a basic question. Up till now i have been using scanf to store user inputted numbers into int and float ect. I am reading a book called Practical c programming 3rd ed. In its chapter on reading numbers it says that scanf is useless except to expert users and its better to use fget to read numbers.

    eg
    Code:
    char line[100];
    int value;
    
    fgets(line, sizeof(line),stdin);
    sscanf(line, "%d", &value);
    Is that how most people do it? Its just that is the first time i have seen this used and i was wondering if this practice was out dated.
    Also, does this slow the program down at all?

    Thanks for any info

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Is that how most people do it?
    Pretty much everyone who knows what the deal is with scanf() does, yes.

    fgets() is one of the few means you have of controlling user input with any accuracy - scanf() just stops at random points in the input stream, causing much confusion later on.

    To be better, you should check return results
    Code:
    if ( fgets(line, sizeof(line),stdin) != NULL ) {
      if ( sscanf(line, "%d", &value) == 1 ) {
        // success
      } else {
        // not an integer
      }
    } else {
      // EOF or error on input
    }
    If you were really going for safety, then you would use strtol() to convert a string to an int, since it can detect overflow for example (sscanf can't do this).

    > Also, does this slow the program down at all?
    Depends on your implementation - the difference either way is likely to be minor in any event.
    scanf() and sscanf() share much of the same code, the only difference is where they get chars from.
    Besides, making the code work properly is your first consideration - buggy code is useless no matter how fast it is.

    pinch of salt
    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.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    49
    Thanks for such a complete answer Salem. Great help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  2. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. Char variable can't store numbers?
    By Kespoosh in forum C++ Programming
    Replies: 11
    Last Post: 03-15-2003, 07:24 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM