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
How to Use HTML to Open a Link in a New Tab
What Is a Financial Audit?
Encore Atlanta Cheer Competition
Play FETCH GAMES for Free!
Genesis Parsippany
Methstreams Boxing Stream
Caesars Rewards Loyalty Program Review [Previously Total Rewards]
1970 Chevelle Ss For Sale Craigslist
Embassy Suites Wisconsin Dells
Overzicht reviews voor 2Cheap.nl
Craigslist Phoenix Cars By Owner Only
Sunday World Northern Ireland
Unit 1 Lesson 5 Practice Problems Answer Key
Love In The Air Ep 9 Eng Sub Dailymotion
[Cheryll Glotfelty, Harold Fromm] The Ecocriticism(z-lib.org)
Craigslist Pet Phoenix
Fsga Golf
Dragger Games For The Brain
Miltank Gamepress
Doki The Banker
Hood County Buy Sell And Trade
Sandals Travel Agent Login
Snohomish Hairmasters
UCLA Study Abroad | International Education Office
'Insidious: The Red Door': Release Date, Cast, Trailer, and What to Expect
Mynahealthcare Login
Page 2383 – Christianity Today
Redding Activity Partners
Solarmovie Ma
Roch Hodech Nissan 2023
Car Crash On 5 Freeway Today
Missouri State Highway Patrol Will Utilize Acadis to Improve Curriculum and Testing Management
Ippa 番号
Hotels Near New Life Plastic Surgery
Tokyo Spa Memphis Reviews
The Transformation Of Vanessa Ray From Childhood To Blue Bloods - Looper
What Does Code 898 Mean On Irs Transcript
How to play Yahoo Fantasy Football | Yahoo Help - SLN24152
Legit Ticket Sites - Seatgeek vs Stubhub [Fees, Customer Service, Security]
Dcilottery Login
Gym Assistant Manager Salary
Smite Builds Season 9
Quaally.shop
Actress Zazie Crossword Clue
Craigslist Pet Phoenix
Campaign Blacksmith Bench
Tyrone Unblocked Games Bitlife
Unit 4 + 2 - Concrete and Clay: The Complete Recordings 1964-1969 - Album Review
Ubg98.Github.io Unblocked
Skybird_06
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.