The strncmp function is a string comparison function found in languages like C and C++. In contrast with strcmp, the strncmp function takes a third argument denoting the maximum length of the strings. This is used to prevent out of bounds errors.
Questions tagged [strncmp]
85 questions
49
votes
2 answers
Why is string comparison so fast in python?
I became curious to understand the internals of how string comparison works in python when I was solving the following example algorithm problem:
Given two strings, return the length of the longest common prefix
Solution 1: charByChar
My intuition…

david_adler
- 9,690
- 6
- 57
- 97
45
votes
2 answers
Is it legal to pass a non-null-terminated string to strncmp in C?
I have an array of 16 bytes that holds the name of an executable's segment.
char segname[16];
If the segment name length is less than 16 bytes, then the rest is padded with null bytes. Otherwise, there is no terminating null byte.
I want to compare…

Bilow
- 2,194
- 1
- 19
- 34
22
votes
2 answers
Is strncmp(NULL, "foo", 0) well defined?
Is it safe to put NULL pointer as parameter of strncmp if the third parameter is zero? I.e. an invocation like:
strncmp(NULL, "foo", 0);

Marian
- 7,402
- 2
- 22
- 34
13
votes
3 answers
Does this usage of strncmp contain an out of bounds read?
Fortify indicates that this is an out of bounds read:
if (strncmp("test string", "less than 32 char", 32) == 0)
{
...
}
It says that the function reads data from outside the bounds of less than 32 char.
Is there really a finding if strncmp goes…

Engineer2021
- 3,288
- 6
- 29
- 51
7
votes
2 answers
Is it undefined behavior what strncmp(s1, s2, 0) returns (i.e. the last argument is zero)?
It is not immediately clear from the standard what strncmp (from string.h)
int strncmp(const char *s1, const char *s2, size_t n);
should return if its 3rd argument n is 0.
According to the C17 standard draft, 7.24.4.4:
The strncmp function…

Lover of Structure
- 1,561
- 3
- 11
- 27
7
votes
6 answers
Does strlen() in a strncmp() expression defeat the purpose of using strncmp() over strcmp()?
By my understanding, strcmp() (no 'n'), upon seeing a null character in either argument, immediately stops processing and returns a result.
Therefore, if one of the arguments is known with 100% certainty to be null-terminated (e.g. it is a string…

cvp
- 73
- 1
- 3
6
votes
2 answers
When returning the difference between pointers of char strings, how important is the order of casting and dereferencing?
For educational purposes (yes 42 yes) I'm rewriting strncmp and a classmate just came up to me asking why I was casting my returnvalues in such a way. My suggestion was to typecast first and dereference afterwards. My logic was that I wanted to…

wandawata
- 63
- 3
6
votes
1 answer
c/c++ what is the return value of strncmp when size is 0
The c/c++ strncmp signature is like the following:
int strncmp ( const char * str1, const char * str2, size_t num );
My question is what's the return value if num is 0? How the standard says? Don't find an answer from some online…

nowgains nowpains
- 185
- 2
- 10
4
votes
2 answers
Why do these two programs give different outputs in VC++2008?
Why do these two programs give different outputs in VC++2008?
After all, the same strings are compared.
strcmp__usage.c
#include
#include
main()
{
char targetString[] = "klmnop";
printf ("Compare = %d\n", strcmp(targetString,…

user366312
- 16,949
- 65
- 235
- 452
3
votes
2 answers
Why does the compiler optimize away shared memory reads due to strncmp() even if volatile keyword is used?
Here is a program foo.c that writes data to shared memory.
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
key_t key;
…

Lone Learner
- 18,088
- 20
- 102
- 200
3
votes
2 answers
rs232 string comparison in C
I am trying to make a programm which can read commands from a RS232 port and use them for the next action.
I am using a string compare command to compare the desired 'action' string with the RS232 string. Something goes wrong with the string…

user1442205
- 31
- 3
2
votes
2 answers
Strcmp generate a core dump
So i have a std::unordered_map, i want to acces to strings stored intro this map. I want to search intro intro all words inside the map and compare with a given word. If the strings are same then continue execution of the if statement.
{
public: …

Armin
- 33
- 3
2
votes
1 answer
strncmp with too big of an n gives weird output
I have 2 strings to compare, and I thought using strncmp would be better than using strcmp because I know one of the strings length.
char * a = "hel";
char * b = "he"; // in my real code this is scanned so it user dependent
for(size_t i = 0; i < 5;…

CIsForCookies
- 12,097
- 11
- 59
- 124
2
votes
5 answers
Checking the first letter of a string in c
I am writing a very simple function in C to check if a string is an absolute path or relative path. No matter what I try it is always returning false.
Here is what I have tried:
int isAbsolute(char *str){
if(strcmp(str,"/")){
return 1;
…

CodySig
- 174
- 1
- 4
- 15
2
votes
3 answers
The use of strncmp and memcmp
Does
if(strncmp(buf, buf2, 7) == 0)
do the same thing as
if(memcmp(buf, buf2, 7) == 0)
buf and buf2 are char* arrays or similar.
I was going to append this to another question but then decided perhaps it was better to post it separately.…

FreelanceConsultant
- 13,167
- 27
- 115
- 225