Thread: strcmp();

  1. #16
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    OK, I'll justify myself.
    Code:
    scanf("%s%s",string1,string2);
    is the slightly slower equivalent* of:
    Code:
    gets(string1);
    gets(string2);
    * Actually, gets() stops at a newline while scanf stops at whitespace.

    However, gets() is less dangerous because:
    1. Some compilers will warn about the danger of gets().
    2. gets() will be picked up by other coders while the _scanf bug will often be missed. This is demonstrated by this forum. Use of gets() is commented on immediately while the _scanf bug regularly goes without comment, even when highly experienced programmers have posted comment on the code.
    3. Code and security reviews are more likely to miss the improper scanf usage.
    4. Employers are more likely to employ people who can't use scanf than people who use gets().
    5. Documentation for gets() usually explicitly points out the risk. This is not the case for _scanf, although MSDN does get it right.
    6. The dangers of gets() are in the faq, as far as I can tell, _scanf does not get the same treatment.


    To use _scanf safely with strings, you must specify a width, one less than the size of the buffer.
    Code:
    scanf("%98s%98s",string1,string2);
    If you don't, you should use gets() as it is safer.
    Last edited by anonytmouse; 05-02-2004 at 01:30 AM.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I believe the point was, that with the addition of the letter f, you could have saved yourself the hassle of explaining why you think gets is a good choice.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    Just to kill the argument,
    I used fgets and sscanf in the actual program.
    I wanted to use strcmp(); but it was faulty for what I wanted.
    I needed to compare the full string instead of a sequential testing.
    It would kick out of the comparison as soon as the string differed.
    I needed it to test a double value that was stored in a string array.
    Instead of using strcmp I converted the strings to doubles with atof().
    After tuesday I will post my final code for people to critique.
    Last edited by xviddivxoggmp3; 05-02-2004 at 06:37 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  4. #19
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by anonytmouse
    OK, I'll justify myself.
    ...
    However, gets() is less dangerous because:
    ...
    So the reason gets() is better is because more people will jump down your throat faster and tell you why it's wrong?!??

    Save the aggravation and start with fgets() in the first place! Sheesh, justification...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #20

    Arrow

    Well,

    Personally sure use gets() if you want, but now if you want to be open to hackers using the best advantage of it to overflow your buffer or write whatever they want to it etc... sure use it.

    scanf() also has its ups and downs thats why its best you flush the stdin stream and use fgets() allowing YOU to set the size the max number of bytes to read.

    Only reason why experienced programmers have concerns is either because 1) it happened to them before, or 2) They know what can happen, or lastly 3) If someone writes a poor code, why keep using it?

    I do understand how some programmers are trying to help the beginners, but sometimes the way they deliver the statement sounds more like a "better do or die" situation. If you look beyond that and see what they are really trying to say you shouldn't have to worry to much about the front cover "WARNING: DON'T USE" scare.

    Likewise in my opinion, I say use fgets() as much as you can:

    char buffer[256];

    fflush(stdin);
    fgets(buffer, sizeof(buffer), stdin);


    scanf() and gets() are not in my recommendation, though scanf() isn't that bad if you are dealing with an integer cast type.

    Remember this is just my opinion, so feel free to post yours just don't blast mine


    Hope this helps,
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  6. #21
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    fflush(stdin); is undefined behavior stack overflow...maybe you should read the faq yourself?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 06:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  4. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 08:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM