#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Location
    Vadodara ,India
    Posts
    3
    Rep Power
    0

    Difference between gets() and scanf() function


    struct stu
    {
    char name[15];
    int age;
    };

    typedef struct stu st;

    void main()
    {
    FILE *f,*t;
    st s;
    char a;
    clrscr();


    f=fopen("record.txt","w");

    while(1)
    {
    printf("\nDo you want to enter new records\n");
    fflush(stdin);
    scanf("%c",&a);
    if(a=='n')
    break;


    puts("Please enter your name:\n");
    gets(s.name);
    printf("\nPlease enter your age\n");
    scanf("%d",&s.age);
    fprintf(f,"%s\t%d\n",s.name,s.age);

    }
    fclose(f);
    }


    I tried to run this program in turbo C but it is not accepting the
    string 's.name'. please tell me whether i should use gets()
    or scanf function to input the string.

    Comments on this post

    • jwdonahue disagrees : Read [url]http://forums.devshed.com/c-programming-42/difference-between-gets-and-scanf-function-653905.html?goto=lastpost[/url]. Use code tags and proper indentation.
    • Arty Ziff agrees : Balance
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    337
    Rep Power
    46
    Beginner, Read How to Post Question first and Commonly Asked Question.
    Read Documentation of gets and scanf.
    Edit: fflush(stdin) is not portable.
  4. #3
  5. Hats off to Mr. Joseph donahue
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2009
    Posts
    752
    Rep Power
    1106
    Let me echo whatever I have learned till date from this forum:
    1. You shouldn't use gets or scanf as both of them may lead to unexpected results in your program

    2. Instead use fgets to take any input.

    3. as mentioned by Nyan, fflush(stdin) is not standard, if you have to use scanf- try
    Code:
    scanf(" %c",&a); /*notice the space after " and %c, this removes any extraneous characters
     to be treated as input from the input buffer.
    4.Throw out the turbo C compiler and use microsoft visual c++.


    5. and add the code tags around your code(read How to post a question)
    Last edited by RAJ_55555; November 15th, 2009 at 07:18 AM.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Location
    Vadodara ,India
    Posts
    3
    Rep Power
    0
    Thanks.
    i tried to use fgets() but its not giving me the correct results.
    And also tell me the use of putting a space in the scanf() function.

    Code:
    while(1)
    {
    printf("\nDo you want to enter new records\n");
    scanf(" %c",&a);
    if(a=='n')
    break;  
    
    
     puts("Please enter your name:\n");
     //scanf(" %s",s.name); Its a comment
    
     fgets(s.name,sizeof(s.name),stdin);
     printf("\nPlease enter your age\n");
     scanf("%d",&s.age);
     fprintf(f,"%s\t%d\n",s.name,s.age);
     
    }
  8. #5
  9. Hats off to Mr. Joseph donahue
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2009
    Posts
    752
    Rep Power
    1106
    Originally Posted by Aditya Oza
    Thanks.
    And also tell me the use of putting a space in the scanf() function.
    The reason I believe why you used fflush(stdin) in the first place was because the new line character was being taken as a character input after you hit enter for previous input. If you still don't get it:
    the enter character is a part of the ASCII symbols, it has a ASCII value of 13 in decimal and is also treated as a character. The scanf terminates when it finds a new line and %c treats it as a valid input. what happens in this case is when the first scanf is completed and you hit the enter key, that particular scanf value is taken, but the enter hit stays in the input buffer unless it is cleared(which you try to do using fflush), and the next scanf for character input, expecting a character value, takes the new line character as an input and goes for the next input.
    If you print it it will print a new line.
    The blank space preceding %c prevents any previously entered extraneous characters from being assigned to the variable.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Posts
    3,417
    Rep Power
    887
    Use fgets() and sscanf() instead of scanf(). Read the docs on both of those functions. fgets() will not read/store the '\n' character in your input buffer if it doesn't hit it before n-1 inputs because it always reserves the last element of the array for the nul character ('\0'). Use fgets() to get input into a large buffer and then use sscanf() to parse that buffer into whatever you like.
    I no longer wish to be associated with this site.
  12. #7
  13. Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jun 2005
    Posts
    5,964
    Rep Power
    4852
    Learn how to limit the number of characters f/s/scanf will take so that you don't overflow your buffer. Gets doesn't give you that opportunity.
    Write no code whose complexity leaves you wondering what the hell you did.
    Politically Incorrect DaWei on Pointers Grumpy on Exceptions
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Location
    Vadodara ,India
    Posts
    3
    Rep Power
    0
    Thanks, all of you.
    it worked

IMN logo majestic logo threadwatch logo seochat tools logo