+2

What is the difference when strcmp() is used in both C and C++?

I have came across this contradiction that both languages behave indifferently to this function. How far is it true??

8/16/2019 1:41:44 PM

Master Yoda[#RIP]

4 Answers

New Answer

+4

Ryuga[#RIP] What different behavior have you observed? If it is about the return value, then the actual value returned is compiler specific. Standards only mandate to return value = 0, < 0 or > 0, so while some compiler may return 0, -1, 1 others may return the actual value(no of chars) by which one string is smaller or larger than other. If the issue is something else, kindly elobrate.

+2

As ~ swim ~ has already stated the specification does not mandate what is returned beyond 0, <0 or >0. Some will return 0, -1, 1 and others may return the character number (position) or ascii value (negated if necessary). All that you can be certain of is that if the return value !=0 then the strings do not match. See... http://www.cplusplus.com/reference/cstring/strcmp/ Note that strcmp is a C function, so you need to #include <cstring>. If you want to do things the C++ way then use the string class and use the string::compare function. See... http://www.cplusplus.com/reference/string/string/compare/

+1

in Cpp in particular I observed it returns the difference of ASCII values..... which is not the case with C I suppose

0