Thread: gets and scanf

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    2

    gets and scanf

    Hi

    I'm new to c and i got a problem using gets and scanf

    here's the code:

    #include <stdio.h>

    Code:
    int main()
    {
        int input1;
        char input2[5];
       
        printf( "Enter input : " );
        scanf( "%d", &input1 );
        printf( "Enter input : " );
        gets( input2 );
       
        printf( "%d, %s\ n", input1, input2 );
       
        return 0;
    }
    after i enter the first input I cannot enter the second input. Is it the gets behaviour or something wrong with my code?

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    FAQ > Explanations of... > Why gets() is bad / Buffer Overflows

    Quote Originally Posted by kermi3
    Welcome to the boards. If you haven't already done so then please take some time to familiarise yourself with the faq:
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

    Make sure you have a look at the the posting guidelines:
    http://cboard.cprogramming.com/annou...ouncementid=51
    Following the rules will ensure you get a prompt answer to your question.

    Remember, too, that the board has a search facility, a link is at the top of your screen:
    http://cboard.cprogramming.com/search.php
    It will often get you a quicker answer to your questions than waiting for a response to one you have posted.


    If you have any questions about this you may ask or you can contact one of our forum leaders:

    http://cboard.cprogramming.com/showgroups.php
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed