C strncmp()

« Previous Chapter Next Function »


The strncmp() function lexicographically compares not more than count characters from the two null-terminated strings and returns an integer based on the outcome, as shown here in the following table :

Value Meaning
Less than zero str1 is less than str2
Zero str1 is equal to str2
Greater than zero str1 is greater than str2

If there are less than count characters in either string, the comparison ends when the first null is encountered.

strncmp() Syntax

Following is the syntax of the strncmp() function :

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

strncmp() Example

Following function compares the first eight characters of two command line arguments :

int main(int argc, char *argv[])
		printf("Incorrect number of arguments.");
	if(!strncmp(argv[1], argv[2], 8))
		printf("The strings are the same.\n");
	return 0;

« Previous Chapter Next Function »


Quick Links
Signup - Login - Give Online Test