3.2.38. strncmp

Performs a limited comparison of two strings.

Syntax

int strncmp (str1, str2, count)
char *str1;
char *str2;
int count;

where:

str1

Variable of type pointer to char. Specifies the location in memory of the first byte of a character string.

str2

Variable of type pointer to char. Specifies the location in memory of the first byte of a character string.

count

Variable of integer type. Specifies the length of characters in each string to compare, unless the NUL character is encountered in either string first.

Description

This macro performs limited comparison of two strings. The strncmp macro is used to compare strings in ASCII sequence, except that the comparison stops after count characters, or when the first NUL character is encountered, whichever comes first.

This macro behaves like the strncmp function in the ANSI C string library.

Return value

int

One of the following:

<0

Indicates that the second argument string value comes after the first argument string value in the machine collating sequences, str1 < str2.

0

Indicates that the two strings are identical in content.

>0

Indicates that the first argument string value comes after the second argument string value in the machine collating sequences, str2 < str1.

Rules

  • Strings do not have to be NUL terminated or fit within the array boundaries because the comparison is limited to the number of stated characters.

  • Less than count characters are compared if one of the strings is smaller than count characters.

  • The comparison is always signed, regardless of how the string is declared.

Example

This example shows how to use strncmp in a macro:

define /R void checkfile(filename)
  char *filename;
{
  int retval;
  retval = strncmp(filename, "dhrystone", 4);
  if (retval == 0)
    $printf "%s belongs to the Dhrystone project\n",filename$;
  else
    $printf "%s belongs to another project\n",filename$;
}
.

See Also

Copyright © 2002-2009 ARM Limited. All rights reserved.ARM DUI 0175K
Non-Confidential