The bycmpr() function

The bycmpr() function compares two groups of contiguous bytes for a given length. It returns the result of the comparison as its value.

Syntax

mint bycmpr(byte1, byte2, length)
   char *byte1;
   char *byte2;
   mint length;
byte1
A pointer to the location at which the first group of contiguous bytes starts.
byte2
A pointer to the location at which the second group of contiguous bytes starts.
length
The number of bytes that you want bycmpr() to compare.

Usage

The bycmpr() function performs a byte-by-byte comparison of the two groups of contiguous bytes until it finds a difference or until it compares length number of bytes. The bycmpr() function returns an integer whose value (0, -1, or +1) indicates the result of the comparison between the two groups of bytes.

The bycmpr() function subtracts the bytes of the byte2 group from those of the byte1 group to accomplish the comparison.

Return codes

0
The two groups are identical.
-1
The byte1 group is less than the byte2 group.
+1
The byte1 group is greater than the byte2 group.

Example

This sample program is in the bycmpr.ec file in the demo directory.
/*
   * bycmpr.ec *

   The following program performs three different byte comparisons with
   bycmpr() and displays the results.
*/

#include <stdio.h>

main()
{
    mint x;

    static char string1[] = "abcdef";
    static char string2[] = "abcdeg";

    static mint number1 = 12345;
    static mint number2 = 12367;

    static char string3[] = {0x00, 0x07, 0x45, 0x32, 0x00};
    static char string4[] = {0x00, 0x07, 0x45, 0x31, 0x00};

    printf("BYCMPR Sample ESQL Program running.\n\n");

                     /* strings */
    printf("Comparing strings: String 1=%s\tString 2=%s\n", string1, string2);
    printf("  Executing: bycmpr(string1, string2, sizeof(string1))\n");
    x = bycmpr(string1, string2, sizeof(string1));
    printf("  Result = %d\n", x);

                  /* ints */
    printf("Comparing numbers: Number 1=%d\tNumber 2=%d\n", number1, number2);
    printf("  Executing: bycmpr( (char *) &number1, (char *) &number2, ");
    printf("sizeof(number1))\n");
    x = bycmpr( (char *) &number1, (char *) &number2, sizeof(number1));
    printf("  Result = %d\n", x);

                  /* non printable */
    printf("Comparing strings with non-printable characters:\n");
    printf("  Octal string 1=%o\tOctal string 2=%o\n", string3, string4);
    printf("  Executing: bycmpr(string3, string4, sizeof(string3))\n");
    x = bycmpr(string3, string4, sizeof(string3));
    printf("  Result = %d\n", x);

               /* bytes */
    printf("Comparing bytes: Byte string 1=%c%c\tByte string 2=%c%c\n");
    printf("  Executing: bycmpr(&string1[2], &string2[2], 2)\n");
    x = bycmpr(&string1[2], &string2[2], 2);   
    printf("  Result = %d\n", x);

    printf("\nBYCMPR Sample ESQL Program over.\n\n");
}

Output

BYCMPR Sample ESQL Program running.

Comparing strings: String1=abcdef      String 2=abcdeg
  Executing: bycmpr(string1, string2, sizeof(string1))
  Result = -1
Comparing numbers: Number 1=12345      Number 2=12367
  Executing: bycmpr( (char *) &number1,  (char *) &number2, sizeof(number1)
  Result = -1
Comparing strings with non-printable characters:
  Octal string 1=40300    Octal string 2=40310
  Executing: bycmpr(string3, string4, sizeof(string3))
  Result = 1
Comparing bytes: Byte string 1=cd      Byte string 2=cd
  Executing: bycmpr(&string1[2], &string2[2], 2)
  Result = 0

BYCMPR Sample ESQL Program over.