The scanf() function

Discussion in 'C' started by Player, Aug 3, 2007.

  1. Player

    Player New Member

    Joined:
    Aug 3, 2007
    Messages:
    37
    Likes Received:
    0
    Trophy Points:
    0
    Hi all, i'm new on this site and have some what i think are simple questions. I have had a good look on here and i feel a bit silly asking these basic questions. I am a complete beginner and would like to know a few things to help me on my way. If i need to go to another site which deals with novices then please point me in the right direction. I think this site id for experts only?

    Why use the gets() function when theres the scanf() function?

    Also when using the gets() function, if you want to have numbers inputed you have to use the atoi() and atof() to convert it to an int number. Is this because the input from the keyboard is text, even the numbers?

    Hope this makes sense, like i say i am a complete novice so please excuse my dumb questions. I want to understand all of what i'm learning fully.

    Thanks
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Not really. At least I learn from here some of the things which I have never thought of. So its all about learning.

    First of all any thing in a programming language can be done in more than one way and scanf provide the general functionality but gets provide some special functionality for string.

    Because gets takes in strings and so when you need the input to be converted to some other data type you need to make the conversion using the functions.
     
  4. Player

    Player New Member

    Joined:
    Aug 3, 2007
    Messages:
    37
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for the reply, i was starting to think i had asked a really dumb question.

    I understand about the conversion when using the gets function but if the scanf function automatically converts it why bother with gets? I have also read that the gets function can receive more input than you tell it to making the excess input being dumped into memory which can cause problems, crashing etc. So again, why use gets?

    Thanks in advance

    P.S. Please tell me if i'm asking really dumb questions :)
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Scanf function does not do it automatically. You need to tell the scanf what you are inputting using the % symbol.

    No questions is dumb and if you ask such question it brush ups our knowledge as well and helps in taking some interview with the questions. I am definitely going to add this to my list of questions in the interview.
     
  6. Player

    Player New Member

    Joined:
    Aug 3, 2007
    Messages:
    37
    Likes Received:
    0
    Trophy Points:
    0
    I think i understand now. I have read that input from the keyboard is text. Does punching a number in come as text too. Is this why the % symbol is used for. To convert text to int for example? Could you have a look at the following and tell me if these do the same things please?

    char name
    gets(name) is the same as

    scanf("%s",&name)

    and

    char input
    int result
    gets(input)
    result=atoi(input) is the same as

    scanf("%d",&result)

    Thanks for your patience :)
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    As far as the user of the functions are considered its the same but the internal implementations may be different or same depending on the compiler.
     
  8. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    First, and very important, do not use gets. If you point gets at a string and the user enters more information than the string can hold, you are looking at a buffer overflow. This can cause complete failure of your program. Use fgets. With fgets you can set a maximum on the number of characters that can be accepted.

    Secondly, input devices are entirely implementation dependent. They are hardware. Consider a machine running Windows. Each key produces a code which has no independent meaning, other than to distinguish one key position from another. Windows will take those codes and do things with them, one of which is to interpret them as Virtual Key Codes (this pair of codes represents an ENTER key, that represents the F12 key, etc.). The language library for C (in this instance) will apply further transformations and organizational measures and present the information to you as described in the language documentation.

    As you can see, the difference between a 'number' and a 'character' (or a 'function') is strictly one of interpretation. If you mess with your C, you will discover that 1 and '1' and "1" are not the same thing.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice