C String strncmp difficulty

Pages: 123
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);
    }
}
Replace sizeof("exit") with sizeof("exit") - 1. This is because of the implicitly-appended, null-terminating character '\0'.
Last edited on
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/
Oh yeah. I forgot about that. Thanks!
There is no any need to use function strncmp. It would be much better to use simply strcmp.
strcmp is deprecated, and allows the ability for malicious entities to overrun the buffer and force scripts to run through it.
strcmp is deprecated,

No, it's not. And in this particular instance it is perfectly safe to use.
@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!:)
In this thread, there's only one thing that's deprecated.
That didn't come from a book, vlad. I've done that to people.

It's a highly amusing trick.
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
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.
It's ironic that you're complaining about bugs while you encourage someone new to use them.
@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.
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().
Unsafe and buggy are not synonyms.
@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.
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?
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.
@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
Pages: 123