Skip to main content
Open topic with navigation CAPL Functions » General » Function Overview » strncmp

strncmp

Valid for: CANoe DE • CANoe:lite DE • CANoe4SW DE • CANoe4SW:lite DE

Function Syntax

long strncmp(char s1[], char s2[], long len); // form 1
long strncmp(char s1[], char s2[], long s2offset, long len); // form 2

Description

This function compares s1 with s2 for a maximum of len bytes. If they are identical the functional result is 0.
If s1 is less than s2 the result is -1, else +1. Form 2 starts the comparison in s2 at the specified offset.
Note: A character may need several bytes depending on the string encoding, e.g. Japanese characters in Windows ANSI (932) encoding or any special characters in UTF-8 encoding. In that case, you should use mbstrncmp instead.

Parameters

  • s1: First string
  • s2: Second string
  • s2offset: Offset in s2 in bytes
  • len: Maximum number of bytes to compare

Return Values

  • -1: if s1 is less than s2
  • 1: if s2 is less than s1
  • 0: if the strings are equal

Example

on key 's'
{
    char s1[7] = "Vector";
    char s2[7] = "Vector";
    if(strncmp(s1,s2,strlen(s1)))
        write("not equal");
    else
        write("equal");
}
strlenstrncatstrncpystrncmp_off