< C++ Programming | Code/Standard C Library | Functions
strcmp[edit]
Syntax |
- Compares the C string str1 to the C string str2. This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached. This function performs a binary comparison of the characters.
- Jun 19, 2017 strcmp in C/C strcmp is a built-in library function and is declared in header file. This function takes two strings as arguments and compare these two strings lexicographically.
The function strcmp() compares str1 and str2, then returns:
Return value | Explanation |
---|---|
less than 0 | str1 is less than str2 |
equal to 0 | str1 is equal to str2 |
greater than 0 | str1 is greater than str2 |
For example:
C library function - strcmp - The C library function int strcmp(const char.str1, const char.str2) compares the string pointed to, by str1 to the string pointed to. Compares the C string str1 to the C string str2. This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached. This function performs a binary comparison of the characters. I am just trying to learn how to use c and one thing that I am trying to make is a yes or no choice 'choose y to continue choose n to exit' kinda thing I.
Note that if str1 or str2 are missing a null-termination character, then strcmp() may not produce valid results. For a similar (and safer) function that includes explicit bounds checking, see strncmp().
- Related topics
- memcmp - strcat - strchr - strcoll - strcpy - strlen - strncmp - strxfrm
Retrieved from 'https://en.wikibooks.org/w/index.php?title=C%2B%2B_Programming/Code/Standard_C_Library/Functions/strcmp&oldid=3676192'
- The C Standard Library
- C Standard Library Resources
- C Programming Resources
- Selected Reading
Function Strcmp En Dev C Vs
Description
The C library function int strcmp(const char *str1, const char *str2) compares the string pointed to, by str1 to the string pointed to by str2.
Declaration
Following is the declaration for strcmp() function.
Parameters
- str1 − This is the first string to be compared.
- str2 − This is the second string to be compared.
Strcmp In C
Return Value
This function return values that are as follows −
- if Return value < 0 then it indicates str1 is less than str2.
- if Return value > 0 then it indicates str2 is less than str1.
- if Return value = 0 then it indicates str1 is equal to str2.
Example
The following example shows the usage of strcmp() function.
Let us compile and run the above program that will produce the following result −
Function Strcmp En Dev C S En Dev C++ Ejemplos
string_h.htm