codescracker


c

C strncmp()



« Previous Chapter Next Function »


strncmp()

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 :

#include<string.h>
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 :

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
	if(argc!=3)
	{
		printf("Incorrect number of arguments.");
		exit(1);
	}
	if(!strncmp(argv[1], argv[2], 8))
	{
		printf("The strings are the same.\n");
	}
	return 0;
}

« Previous Chapter Next Function »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test