User Tools

Site Tools


Sidebar

Programming Reference/Librarys

Question & Answer

Q&A is closed







c:string.h:strncmp

strncmp

#include <stdlib.h>
 
int strncmp(const char *str1, const char *str2, size_t n);

The strcmp() function compares the two strings s1 and s2. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2. The strncmp() function is similar, except it only compares the first (at most) n characters of s1 and s2.

strncmp C Sourcecode Example

/* 
 * strncmp example code
 * http://code-reference.com/c/string.h/strncmp
 */
#include <stdio.h>
#include <string.h>
 
int main( void )
{
   const char str1[] = "42 is a bad answer";
   const char str2[] = "42 is a realy good answer";
   int i;
 
   i = strlen(str1);
 
   for(i ; i > 0; i--) {
         if(strncmp( str1, str2, i) == 0) {
               printf("The first %d chars of str1 and str2 are the same\n", i);
	       break;
            }
   }
 
  return 0;
}
 
}

output of strncmp c example

  The first 8 chars of str1 and str2 are the same

on the occasion of the current invasion of Russia in Ukraine

Russian Stop this War
c/string.h/strncmp.txt · Last modified: 2024/02/16 01:06 (external edit)

Impressum Datenschutz