C String strncmp difficulty

Pages: 123
Sep 3, 2013 at 7:24pm
The following code is designed to create a simple exit condition. If the user types 'exit', the program exits. But before I get to that, I need to use strncmp to evaluate the character array to determine that this is what the user entered.

I type 'exit' into the terminal, but strncmp doesn't evaluate to 'true'. Why? What am I missing? Thanks!

1
2
3
4
5
6
7
8
9
10
char selection[256];     
if (fgets(selection, sizeof selection, stdin)) {
    fprintf(stdout, "Your selection is: %s\n", selection);
    if (strncmp(selection, "exit", sizeof("exit")) == 0) {
        fprintf(stdout, "bye\n\n");
        return 0;
    } else {
        fprintf(stdout, "%s\n", selection);
    }
}
Sep 3, 2013 at 7:28pm
Replace sizeof("exit") with sizeof("exit") - 1. This is because of the implicitly-appended, null-terminating character '\0'.
Last edited on Sep 3, 2013 at 7:31pm
Sep 3, 2013 at 7:36pm
To add to Josue's answer, string literals such as "exit" contain an unseen NUL character to signify their end.

This means that for example "car" contains four characters: 'c', 'a', 'r' and NUL.
Therefore sizeof "car" equals four, not three as you may expect.

It is therefore more sane to use strlen() instead of sizeof.
http://www.cplusplus.com/reference/cstring/strlen/

And if you want to write code as a proficient C++ programmer, you will use std::string instead of char arrays.
http://www.cplusplus.com/reference/string/string/
Sep 3, 2013 at 7:40pm
Oh yeah. I forgot about that. Thanks!
Sep 3, 2013 at 8:47pm
There is no any need to use function strncmp. It would be much better to use simply strcmp.
Sep 3, 2013 at 8:54pm
strcmp is deprecated, and allows the ability for malicious entities to overrun the buffer and force scripts to run through it.
Sep 3, 2013 at 9:08pm
strcmp is deprecated,

No, it's not. And in this particular instance it is perfectly safe to use.
Sep 3, 2013 at 9:10pm
@ciphermagi

strcmp is deprecated, and allows the ability for malicious entities to overrun the buffer and force scripts to run through it.


Do not read bad books!:)
Sep 3, 2013 at 9:26pm
In this thread, there's only one thing that's deprecated.
Sep 3, 2013 at 10:40pm
That didn't come from a book, vlad. I've done that to people.

It's a highly amusing trick.
Sep 3, 2013 at 10:49pm
strcmp is deprecated by Microsoft.

You might not agree with them, but that doesn't alter the fact that they have marked strcmp, along with a lot of other calls, as deprecated.

warning C4996: 'sprintf': This function or variable may be unsafe. Consider using
sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online
help for details.
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\stdio.h(371) :
see declaration of 'sprintf'


Andy


It's strcpy, etc (and sprintf) I was mis-thinking about...

Last edited on Sep 4, 2013 at 12:21am
Sep 3, 2013 at 10:55pm
It is only the problem of Microsoft. As for others then that stupidy of Microsoft only annoys programmers.
By the way I would like to remind you that the Microsoft compiler is very bad and has numerous bugs.
Sep 3, 2013 at 10:57pm
It's ironic that you're complaining about bugs while you encourage someone new to use them.
Sep 3, 2013 at 10:59pm
@ciphermagi

It's ironic that you're complaining about bugs while you encourage someone new to use them.


You are saying a stupidy. Where did you find a bug? @cire already explained you that you are wrong.
Sep 3, 2013 at 11:02pm
You're encouraging someone to use strcmp, which is buggy. That's the reason why strncmp was written in the first place. I've seen, and used, buffer overrun attacks, and they're highly amusing.

Why should we teach someone to start using bad habits when it can get them in trouble later? That's like encouraging someone to use system() calls instead of cin.ignore().
Sep 3, 2013 at 11:05pm
Unsafe and buggy are not synonyms.
Sep 3, 2013 at 11:09pm
@ciphermagi

You're encouraging someone to use strcmp, which is buggy


It is you who is buggy.

Using strncmp in this example is simply a stupidy.
Sep 3, 2013 at 11:10pm
Cause and effect, stupid. First you don't know what calculus even IS, now you're saying that it's impossible for bugs to cause a function to be unsafe?
Sep 3, 2013 at 11:11pm
strcmp is deprecated by Microsoft.

You might not agree with them, but that doesn't alter the fact that they have marked strcmp, along with a lot of other calls, as deprecated.


It doesn't matter what Microsoft says (or I do). These functions are not deprecated in or by the standard, therefore they are not deprecated. The warning is useful. It is a shame it is misleading.
Sep 3, 2013 at 11:15pm
@ciphermagi


Moreover using strncmp is this example is indeed a bug!:) The user could enter some word that has initial letters as exit but the whole word entered by the user is not equal to exit. For example the user could enter "exit poll".:)
Last edited on Sep 3, 2013 at 11:16pm
Pages: 123