Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Last Updated: Mar 27, 2024
Difficulty: Easy

wcscmp() in C++

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM


Everyone in the world must have done comparison at least once or I say everyday. Similar to that the wcscmp() function does it purpose is comparison of two strings if you want to learn how then checkout the article which covers the implementation of wcscmp() method In C++, The wcscmp() function in C++ compares two null terminating wide strings. The comparison is done lexicographically. It is defined within <cwchar> header file.

There is a popular quote “Don’t talk, just show me the code!!” abiding by the words let see the implementation of the wcscmp() in C++.

Also see, Literals in C, Fibonacci Series in C++

BreakDown of wcscmp() method

The section discusses the detailed breakdown of the wcscmp() method in C++.  It includes discussion of declaring the prototype, parameters of wcscmp() and return value of the wcscat() method.

Prototype declaration wcscmp()

int wcscmp( const wchar_t* lhs, const wchar_t* rhs );


The lhs and rhs inputs are sent to the wcscmp() method. It lexicographically compares the contents of lhs and rhs. The difference between the first pairs of characters that differ in lhs and rhs determines the result's sign.

If either lhs or rhs do not point to null terminated wide strings, the behaviour of wcscmp() is unknown.

Parameters of wcscmp()

lhs: Pointer to the wide string to compare that is null terminated.
rhs: Pointer to the wide string to compare that is null ended.

Return value of wcscmp()

The method wcscmp() returns:

1. If the first difference character in lhs is bigger than the equivalent character in rhs, the value is positive.
2. If the first dissimilar character in lhs is smaller than the corresponding character in rhs, the value is negative.
3. If the lhs and rhs are equal, the result is 0.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Program Implementation

Let us see the implementation of the method in code to get the better understanding of the subject:


#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;

void compare(wchar_t *lhs, wchar_t *rhs)
   int result;
   result = wcscmp(lhs, rhs);

   if (result > 0)
       wcout << rhs << " precedes " << lhs << endl;
   else if (result < 0)
       wcout << lhs << " precedes " << rhs << endl;
       wcout << lhs << " and " << rhs << " are same" << endl;

int main()
   // setting the format to US ENG to utf8 file format
   setlocale(LC_ALL, "en_US.utf8");

   // situation 1: comparision result > 0
   wchar_t str1[] = L"\u0102\u0070ple";

   // situation 2: comparision result < 0
   wchar_t str2[] = L"\u00c4\u01f7ple";

   // situation 3: comparision result == 0
   wchar_t str3[] = L"\u00c4\u01a4ple";

   compare(str1, str2);
   compare(str2, str3);

   return 0;


After compiling and running the article in the terminal:


Try and compile with online c++ compiler.

Must Read Dynamic Binding in C++

Frequently Asked Questions

Which is faster memcpy or Memmove?

"Memcpy is more efficient than memmove," says the author. You're probably not performing the same thing when running the two functions in your situation. In general, only use memmove when absolutely necessary. When there's a good likelihood that the source and destination zones will overlap, use it.

What is the use of Memmove?

The method memmove() copies count bytes from source to dest. As if src were first put into a temporary array, this method permits copying across objects that may overlap.

What is the difference between memcpy and Memmove?

The memcpy() method copies a certain number of bytes from one memory to another. The memmove() method copies a given number of bytes from one memory to another or overlaps two memories.


The article extensively covers the implementation of wcscmp() method in c++, we see the working of code. You might be interested in article such as Inroduction to c++ or many article related to c++ headout to our c++ section which has an array of articles such as Copy Elision in C++Algorithm in C++ STL and many more.

Do upvote our blog to help other ninjas grow, and head over to our practice platform Coding Ninjas Studio to practise top problems, attempt mock tests, read interview experiences, and much more.

Happy Learning!

Topics covered
BreakDown of wcscmp() method
Prototype declaration wcscmp()
Parameters of wcscmp()
Return value of wcscmp()
Program Implementation
Frequently Asked Questions
Which is faster memcpy or Memmove?
What is the use of Memmove?
What is the difference between memcpy and Memmove?