C Programming/string.h/strcmp - Wikibooks, open books for an open world (2024)

Table of Contents
Example See also External links

In POSIX and in the programming language C, strcmp is a function in the C standard library (declared in string.h) that compares two C strings.

The prototype according ISO/IEC 9899:1999, 7.21.4.2

int strcmp(const char *s1, const char *s2);

strcmp returns 0 when the strings are equal, a negative integer when s1 is less than s2, or a positive integer if s1 is greater than s2, according to the lexicographical order.

A variant of strcmp exists called strncmp that only compares the strings up to a certain offset.

Another variant, strcasecmp, conforming to POSIX.1-2001, works like strcmp, but is case-insensitive. Some systems instead provide this functionality with functions named stricmp or strcmpi. To compare a subset of both strings with case-insensitivity, various systems may provide strncasecmp, strnicmp or strncmpi.

Example

[edit | edit source]

#include <stdio.h>#include <stdlib.h>#include <string.h>int main (int argc, char **argv){ int v; if (argc < 3) { fprintf (stderr, "Compares two strings\nUsage: %s string1 string2\n",argv[0]); return EXIT_FAILURE; } v = strcmp (argv[1], argv[2]); if (v < 0) printf ("'%s' is less than '%s'.\n", argv[1], argv[2]); else if (v == 0) printf ("'%s' equals '%s'.\n", argv[1], argv[2]); else if (v > 0) printf ("'%s' is greater than '%s'.\n", argv[1], argv[2]); return 0;}

The above code is a working sample that prints whether the first argument is less than, equal to or greater than the second.

A possible implementation is (P.J. Plauger, The Standard C Library, 1992):

int strcmp (const char * s1, const char * s2){ for(; *s1 == *s2; ++s1, ++s2) if(*s1 == 0) return 0; return *(unsigned char *)s1 < *(unsigned char *)s2 ? -1 : 1;}

However, most real-world implementations will have various optimization tricks to reduce the execution time of the function. One will notice, that strcmp not only returns -1, 0 and +1, but also other negative or positive values, resulting from optimizing away the branching introduced by the ?: operator:

return *(const unsigned char *)s1 - *(const unsigned char *)s2;

See also

[edit | edit source]

External links

[edit | edit source]

Template:Compu-prog-stub

C Programming/string.h/strcmp - Wikibooks, open books for an open world (2024)
Top Articles
Ahead of the Bitcoin Halving, Are Bitcoin Mining Stocks a Buy? | The Motley Fool
Could Bitcoin climb to more than $1 million before 2030? Cathie Wood says yes.
Rosy Boa Snake — Turtle Bay
Durr Burger Inflatable
Dairy Queen Lobby Hours
Cold Air Intake - High-flow, Roto-mold Tube - TOYOTA TACOMA V6-4.0
Www.craigslist Virginia
Valley Fair Tickets Costco
Nwi Police Blotter
Wells Fargo Careers Log In
Cumberland Maryland Craigslist
How To Get Free Credits On Smartjailmail
What's New on Hulu in October 2023
Hijab Hookup Trendy
Gma Deals And Steals Today 2022
Truth Of God Schedule 2023
Ostateillustrated Com Message Boards
How pharmacies can help
Bank Of America Financial Center Irvington Photos
Teen Vogue Video Series
Johnnie Walker Double Black Costco
Mandy Rose - WWE News, Rumors, & Updates
Malluvilla In Malayalam Movies Download
Medline Industries, LP hiring Warehouse Operator - Salt Lake City in Salt Lake City, UT | LinkedIn
Gunsmoke Tv Series Wiki
Lcsc Skyward
Publix Christmas Dinner 2022
Till The End Of The Moon Ep 13 Eng Sub
Sports Clips Flowood Ms
Mg Char Grill
Palmadise Rv Lot
Boondock Eddie's Menu
How to Watch the X Trilogy Starring Mia Goth in Chronological Order
The Best Carry-On Suitcases 2024, Tested and Reviewed by Travel Editors | SmarterTravel
Toonily The Carry
Vivek Flowers Chantilly
Crazy Balls 3D Racing . Online Games . BrightestGames.com
Barber Gym Quantico Hours
D-Day: Learn about the D-Day Invasion
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
18 terrible things that happened on Friday the 13th
Infinite Campus Farmingdale
“To be able to” and “to be allowed to” – Ersatzformen von “can” | sofatutor.com
Ezpawn Online Payment
Best GoMovies Alternatives
Walmart 24 Hrs Pharmacy
Oklahoma City Farm & Garden Craigslist
Okta Login Nordstrom
Unpleasant Realities Nyt
Convert Celsius to Kelvin
How Did Natalie Earnheart Lose Weight
Cataz.net Android Movies Apk
Latest Posts
Article information

Author: Errol Quitzon

Last Updated:

Views: 5867

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.