strcmp()

Updated: April 19, 2023

Compare two strings

Synopsis:

#include <string.h>

int strcmp( const char* s1, 
            const char* s2 );

Arguments:

s1, s2
The strings that you want to compare.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The strcmp() function compares the string pointed to by s1 to the string pointed to by s2.

Returns:

< 0
s1 is less than s2.
0
s1 is equal to s2.
> 0
s1 is greater than s2.

Examples:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main( void )
{
    printf( "%d\n", strcmp( "abcdef", "abcdef" ) );
    printf( "%d\n", strcmp( "abcdef", "abc" ) );
    printf( "%d\n", strcmp( "abc", "abcdef" ) );
    printf( "%d\n", strcmp( "abcdef", "mnopqr" ) );
    printf( "%d\n", strcmp( "mnopqr", "abcdef" ) );

    return EXIT_SUCCESS;
}

produces the output:

0
1
-1
-1
1

Environment variables:

LIBC_STRINGS
On certain targets, you can use this environment variable to select the implementation of strcmp(). The value is one of the strings given below.
  • for AArch64 targets:
    • aarch64_neon — optimized for AARCH64 targets using NEON
    • generic — the default
  • for ARMv7 targets:
    • cortex_a9 — optimized for the ARM Cortex-A9 processor; assumes that no unaligned access is supported
    • cortex_a9_aligned — optimized for ARM Cortex-A9; requires that unaligned memory access be enabled on the platform. If memory access is misaligned, this implementation falls back to the NEON version.
    • cortex_a9_neon — optimized for ARM Cortex-A9 using NEON
    • generic — the default
    • krait — optimized for the Qualcomm Krait CPU
    • krait_neon — optimized for Qualcomm Krait using NEON

Processes that register ISRs shouldn't use the NEON versions.

Classification:

ANSI, POSIX 1003.1

Safety:  
Cancellation point No
Interrupt handler Read the Caveats
Signal handler Yes
Thread Yes

Caveats:

Implementations of strcmp() that are optimized using SIMD instructions aren't safe to use in an interrupt handler. These include the NEON implementations on ARMv7 and AArch64.