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
2
votes
4 answers
Why is fgets() and strncmp() not working in this C code for string comparison?
This is a very fun problem I am running into. I did a lot of searching on stack overflow and found others had some similar problems. So I wrote my code accordingly. I originally had fscan() and strcmp(), but that completely bombed on me. So other…

GeekyOmega
- 1,235
- 6
- 16
- 34
1
vote
2 answers
Why `strncmp()` return a fault value in a special case?
I wrote a small code piece of code in C to test strncmp(), memcmp() functions. Here is the code.
#include
#include
int main()
{
const char s1[] = "abcd\0\0\0\0";
const char s2[] = "abcd\0abc";
…

mengxinayan
- 11
- 3
1
vote
4 answers
How to copy the string that remains after using strncpy
I am learning C and want to learn how I can copy the remaining characters leftover in the string after using strncpy. I would like to have the string Hello World broken down into two separate lines.
For example:
int main() {
char someString[13]…

user14377467
- 31
- 6
1
vote
2 answers
strncmp gives 0 even when strings are NOT equal - C
I am having a situation with strncmp function in C, it is returning 0 even when the words do not match, in the example below, I am testing it with the letter 'R' and when running the code it returns 0 even when the compared word in the txt document…

Logan Cortés
- 15
- 3
1
vote
2 answers
How to compare char* to string literal in C?
I need to compare some char * (which I know the length of) with some string literals. Right now I am doing it like this:
void do_something(char * str, int len) {
if (len == 2 && str[0] == 'O' && str[1] == 'K' && str[2] == '\0') {
// do…
user11000993
1
vote
1 answer
Security Impact of using strncmp()?
I've been doing a little research on the security of strncmp and I understand it's not null terminated. But I've also seen how some people are saying it is "not a secure replacement for strcmp()." Could anyone explain this to me please? I've been…

Puma Pants
- 11
- 1
1
vote
6 answers
In C how to strcmp just the beginning 2 characters and then concatenate?
In C how do I strcmp just the beginning 2 characters? Then concatenate with another string? Something like this:
char s[10];
scanf("%s",s);
/* if i input "cs332" or "cs234", anything start with cs */
if (strcmp("cs",???)==0)
strcat(s,"by…

jenifer
- 21
- 1
- 1
- 4
1
vote
2 answers
Error in Arduino IDE using strncmp function
I am using the following code to compare two strings and get the error:
> #define _EXFUN(name, proto) name proto
>
> ^
>
> exit status 1 invalid conversion from 'char' to 'const char*'
> [-fpermissive]
As I…

Shota Kvaratskhelia
- 11
- 1
1
vote
1 answer
Segmentation error handling strcmp
I have the following code:
#include
#include
#include
#define RCVBUFSIZE 1024
void insertarOperacion(char *op){
char operacion[RCVBUFSIZE], *retrString = "RETR "; //<
bool operacionError = false;
…

boludo kid
- 154
- 1
- 12
1
vote
3 answers
C++ invalid conversion from 'char' to 'const char'
I am having trouble with a two-dimensional array comparison. I need to create a pseudo login system that asks the user for a username and password and then compares the input to a predefined list of usernames.
In the function, the predefined…

Yavor Lulchev
- 75
- 1
- 11
1
vote
1 answer
Difference between strncmp_P and strncmp_PF on Arduino?
What is the difference between strncmp_P and strncmp_PF on Arduino? I understand strncmp_F, but what is purpose of strncmp_PF?

BrankoH
- 57
- 1
- 9
1
vote
1 answer
Finding words with the same first character
I've made an array and now I'm trying to compare first symbols of two strings and if it's true to print that word. But I got a problem:
Incompatible types in assignmentof "int" to "char"[20]"
Here is the code:
for ( wordmas= 0; i < character; i++…

user3370335
- 43
- 1
- 1
- 4
1
vote
4 answers
"a " equal to "exit"?
Just messing around with a program I'm currently working on and I stumble upon this. The program is of the client-server type so the client asks the user for commands and the server performs all the executions. If the "exit" command is entered the…

user2929779
- 359
- 5
- 13
1
vote
1 answer
strange behavior of strncmp
this code has a strange behavior in the "check_connected" procedure.
The parameter is converted to char [30] before use the strncmp function, if not, the result is "stack overflow". The problem arises in the result of the compare of two strings,
"Le…

user2999404
- 21
- 2
1
vote
2 answers
Crashing C Program
I am trying to write an algorithm that searches a document for a particular chunk. If the chunk is found, a 1 will be returned, if not, a 0. Basically if the chunk is "abcd" and a particular length is 2, the chunk would be split into "ab" and "cd."…

JaxJags
- 41
- 1
- 5