Do most people store numbers in char?

This is a discussion on Do most people store numbers in char? within the C Programming forums, part of the General Programming Boards category; Hi, I am still new to c so this might sound like a basic question. Up till now i have ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 06:24 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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