strcmp() in C++

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

The strcmp() function is a built-in library function in C++ that compares two character arrays or null terminating strings (C-strings) lexicographically. The strcmp() function takes the two character arrays as parameters that must be compared. The strcmp() function in C++ returns an integer value calculated according to the first mismatched character among the two strings.

What is strcmp() in C++ ?

The function strcmp() is a built-in library function of C++, which is prototyped and defined in the <cstring> header file. This function takes strings as arguments and compares them to check equality. It matches strings lexicographically, which implies that it matches every character for every index in both strings. The comparison begins from the first character of strings and carries on for every character until any unmatched character or NULL character gets encountered.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax of strcmp() in C++

The syntax for using strcmp() function in C++ is:

Where s1 and s2 are the names of strings.

Parameters of strcmp() in C++

The strcmp() function contains two parameters, s1 and s2, that are pointers to the character arrays required to be compared.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Return Value of strcmp() in C++

The strcmp() function in C++ returns an integer value calculated according to the first mismatched character among the two strings. There are three types of return values generated by the strcmp() function.

  • Zero (0): The return value is equal to zero if both strings are the same. That is, elements at the same index in both strings are equivalent.

Example:

Output:

Explanation:

In the above code, two strings, s1 and s2, are declared and initialized, respectively. They are then passed through the strcmp() function. The function's return value gets stored in the variable returnvalue. A conditional statement is used to check whether strings are the same by verifying whether returnvalue is equal to zero.

  • Greater than zero (> 0): The return value is greater than zero if the ASCII value of the first unmatched character of the string on the left-hand side (s1) is greater than that of the corresponding character in the right-hand side string (s2). The resultant value is the difference between the ASCII values of the first unmatched characters of the strings. i.e., (s1-s2).

Example:

Output:

Explanation:

In the above code, two strings, s1 and s2, are declared and initialized, respectively. They are then passed through strcmp() function. The function's return value gets stored in the variable returnvalue. The first unmatched character in the strings is found at index 0, where characters in both the strings are ‘s’ and ‘S’, respectively. The ASCII values of both characters are 115 and 83, respectively. Hence, the difference in the ASCII value turns out as 32. A conditional statement is used to check whether strings are the same by verifying whether returnvalue is equal to zero.

  • Less than zero ( <0 ): The return value is less than zero if the ASCII value of the first unmatched character of the string on the left-hand side (s1) is less than that of the corresponding character in the right-hand side string (s2). The resultant value is the difference between the ASCII values of the first unmatched characters of the strings. i.e., (s1-s2).

Example:

Output:

Explanation:

In the above code, the first unmatched character in the strings is found at index 0, where characters in both the strings are S and s, respectively. The ASCII values of both characters are 83 and 115, respectively. Hence, the difference in the ASCII value turns out as -32.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

strcmp() Prototype

The prototype of the strcmp() function in C++ is described in the cstring header file is:

  • The strcmp() matches the elements of strings s1 and s2 lexicographically.
  • The return value is the difference between the first unmatched pairs of characters that occur in s1 and s2.

strcmp() Undefined Behavior

The strcmp() function in C++ shows undefined behavior or exceptions if one of the parameters does not point to C++ character arrays or null-terminated strings.

Example:

Compile Errors:

Output:

Explanation:

In the above code, two strings, not null-terminated, s1 and s2, are declared and initialized. The strings are passed as parameters to the function strcmp(). The function throws a compile-time error showing undefined behavior.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Implementation of strcmp() Function Using an User-Defined Function

Output:

Explanation: In the above code, two strings, s1 and s2, are declared and initialized, respectively. They are then passed through a user-defined function. In the user-defined function, the strcmp() function is called, and the function's return value gets stored in the variable returnvalue. A conditional statement is used to check whether strings are the same by verifying whether returnvalue is equal to zero.

Conclusion

  • The function strcmp() is a built-in library function of C++, prototyped and defined in the “string.h” header file.
  • The strcmp() function takes the two character arrays as parameters that must be compared.
  • The strcmp() function in C++ returns an integer value calculated according to the first mismatched character among the two strings.
  • The strcmp() function in C++ shows undefined behavior if one of the parameters does not point to C character arrays or null-terminated strings.

Read More: