Comparing the given two strings using memcmp() C function

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server Standard Edition

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info:

To do: Comparing two strings using memcmp() C function

To show: How to use the C function memcmp() to compare two given strings

 

// Using memcmp()

#include <stdio.h>

#include <string.h>

 

int main(void)

{

char s1[] = "ABCDEFGHIJK", s2[] = "ABCDXYZPQR";

 

printf("Using memcmp()\n");

printf("--------------\n");

printf("s1 = %s\n", s1);

printf("s2 = %s\n", s2);

printf("\nmemcmp(s1, s2, 4) = %2d\n", memcmp(s1, s2, 4));

printf("memcmp(s1, s2, 7) = %2d\n", memcmp(s1, s2, 7));

printf("memcmp(s2, s1, 7) = %2d\n", memcmp(s2, s1, 7));

return 0;

}

 

Output example:

 

Using memcmp()

--------------

s1 = ABCDEFGHIJK

s2 = ABCDXYZPQR

memcmp(s1, s2, 4) = 0

memcmp(s1, s2, 7) = -1

memcmp(s2, s1, 7) = 1

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index