Language%20in%20C%20Interview%20Questions%20and%20Answers
Question: The functions memcmp( ) and memicmp( )
Answer: The functions memcmp( ) and memicmp( ) compares first n bytes of given two blocks of memory or strings. However, memcmp( ) performs comparison as unsigned chars whereas memicmp( ) performs comparison as chars but ignores case (i.e. upper or lower case). Both the functions return an integer value where 0 indicates that two memory buffers compared are identical. If the value returned is greater than 0 then it indicates that the first buffer is bigger than the second one. The value less than 0 indicate that the first buffer is less than the second buffer. The following code snippet demonstrates use of both#include <stdio.h> #include <mem.h> main( ) { char str1[] = "This string contains some characters" ; char str2[] = "this string contains" ; int result ; result = memcmp ( str1, str2, strlen ( str2 ) ) ; printf ( "nResult after comapring buffer using memcmp( )" ) ; show ( result ) ; result = memicmp ( str1, str2, strlen ( str2 ) ) ; printf ( "nResult after comapring buffer using memicmp( )" ) ; show ( result ) ; } show ( int r ) { if ( r == 0 ) printf ( "nThe buffer str1 and str2 hold identical data" ) ; if ( r > 0 ) printf ( "nThe buffer str1 is bigger than buffer str2" ) ; if ( r < 0 ) printf ( "nThe buffer str1 is less than buffer str2" ) ; } |
Is it helpful?
Yes
No
Most helpful rated by users:
- What will be the output of the following code?
void main ()
{ int i = 0 , a[3] ;
a[i] = i++;
printf ("%d",a[i]) ;
} - Why doesn't the following code give the desired result?
int x = 3000, y = 2000 ;
long int z = x * y ; - Why doesn't the following statement work?
char str[ ] = "Hello" ;
strcat ( str, '!' ) ; - How do I know how many elements an array can hold?
- How do I compare character data stored at two different memory locations?