Thread: gets and scanf

  1. #1
    Registered User
    Join Date
    Feb 2006

    gets and scanf


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

    here's the code:

    #include <stdio.h>

    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
    Join Date
    Jan 2002
    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:

    Make sure you have a look at the the posting guidelines:
    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:
    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:
    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

    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:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed