C strcmp()

The strcmp() function lexicographically compares two strings and returns an integer based on the outcome as shows 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

strcmp() Syntax

Following is the syntax of the strcmp() function :

int strcmp(const char *str1, const char *str2);

strcmp() Example

You can use the following function as a password-verification routine. It returns zero on failure and 1 on success :

int password(void)
	char s[80];
	printf("Enter password: ");
	if(strcmp(s, "pass"))
		printf("Invalid Password\n");
		return 0;
	return 1;

C Online Test

« C Examples Next Function »

Follow/Like Us on Facebook

Subscribe Us on YouTube