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

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    3

    C project- main.c file question


    I was writing a program that includes books as structures, and the user must type in the name/author of the book to find it. The typing is done with gets(), which needs <string.h>

    The <string.h> and <stdio.h> and "file_name" are present in the implementation file, as it should be.

    IN THE MAIN.C FILE it seems that <string.h> doesn't have to be included, because when i don't include it the program runs just the same; Even though i have a gets() command inside main !

    However when i don't include the <stdio.h> the program doesn't work.

    How come <string.h> is NOT necessary inside main but <stdio.h> is ?

    I hope the question is clear enough :)
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    1. you shouldn't be using gets() AT ALL. See gets is the tool of the devil

    2. You don't need string.h because gets() is prototyped in stdio.h
    http://linux.die.net/man/3/gets

    Whoever told you to use gets() and look for it in string.h is plain wrong on two counts. You should ignore them from now on and find better quality sources of information.
    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
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    3
    So what command should i be using for STRING INPUT ?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    20
    Rep Power
    0
    Originally Posted by C learner
    So what command should i be using for STRING INPUT ?
    Maybe you should actually read the link Salem posted. He posted it for a reason.
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Use an accurate standard library reference source. Each function will be listed with its header file dependency. There are many such sources (including your compile's own documentation.

    This one even comments on why it is deprecated and what you should use instead.

    In other words, had you simply looked the function up, you would have found both that you were incorrect about the header and that the function in question is considered dangerous. Reading the manual is a far more efficient method of solving problems of this sort that posting to a forum.

IMN logo majestic logo threadwatch logo seochat tools logo