Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22298

Why GCC 7.4.0, C++ strcmp() function returns different answers on comparing strings in arrays and srtings given directly in the function [duplicate]

$
0
0

This question already has an answer here:

Here is my code that compares srtings given in array and strings given directly to the function.

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    char str1[]="A",str2[]="Z";
    cout<<strcmp(str1,str2)<<"\n\n";
    cout<<strcmp("A","Z");

}

and the output I received is

-25

-1

Why there is a difference in comparison of a string in array and string as it is.

What happens when string is compared using array in C++

In my terminal, gcc -v gives:

pc@pcsgeek:~$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.4.0-1ubuntu1~18.04.1' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1) 

Further experiments

Exp 1

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    char str1[]="A",str2[]="A";
    cout<<strcmp(str1,str2)<<"\n\n";
    cout<<strcmp("A","A");
}

Exp 1 Output is

0

0

Exp 2

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    char str1[]="Z",str2[]="A";
    cout<<strcmp(str1,str2)<<"\n\n";
    cout<<strcmp("Z","A");

}

Exp 2 Output is

25 

1

Here I observed a trend that

In Direct string comparison strcmp() retruns:
 => 0 if first string equal to second string
 => -1 if first string is alphabetically lower than second string
 => 1 if first string is alphabetically higher than second string

In string comparison via ARRAYS strcmp() retruns:
 => 0 if first string equal to second string
 => -ve if first string is alphabetically lower than second string
 => +ve if first string is alphabetically higher than second string

In string comparison via ARRAYS strcmp() strcmp satisfy the following statement that is given in most of the programming tutorials and websites

strcmp() compares 2 string's characters with their ASCII CODE, and if a unmatched character is found (the first unmatched character), the function finds their ASCII CODE difference and gives that difference as output of the function (That will be like ASCII CODE of string 1- ASCII CODE of string 2)

Why is it so????? Any answer?


Viewing all articles
Browse latest Browse all 22298

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>