Home > other >  "find" function in C is giving different results while associating it with variables and
"find" function in C is giving different results while associating it with variables and

Time:08-25

Code 1 : -

#include<iostream>
using namespace std;
int main()
{
    string name = "For all the times";
    cout << "Line 6 output :  " << name.find("ball",0) << endl;
    int num = name.find("ball",0);
    cout << "Line 8 output :  " << num;
    return 0;
}

Output :-

[Running] cd "h:\C\" && g   new.cpp -o new && "h:\C\"new
Line 6 output :  18446744073709551615
Line 8 output :  -1
[Done] exited with code=0 in 8.008 seconds

I wonder why line 6,8 gave me different outputs. I was expecting "-1" from both the lines. However when I tried "all" instead of "ball" in the same code, this is the output.

[Running] cd "h:\C\" && g   new.cpp -o new && "h:\C\"new
Line 6 output :  4
Line 8 output :  4
[Done] exited with code=0 in 9.88 seconds

Unlike the above case, here, both line 6,8 gave the same output. Does the negative value -1, which is the output in the 1st case has something to do with it ? or is something wrong with my code ?

CodePudding user response:

What you are seeing is the same number represented as two different types. std::string::find will return std::string::npos if no string is found. std::string::npos is an unsigned integer with the value of -1. Since unsigned integer are never negative that value wraps around to the maximal value the unsigned integer can hold, which for 64 bits is 18446744073709551616. When you initialize an int with that value since it is a signed integer it can express negative numbers so you see -1 instead.

CodePudding user response:

This is all about types (an important part of C )

The return type of std::string::find() is std::size_t See: doc.

The std::size_t is an unsigned integer (i.e. no negative numbers). So this code:

cout << "Line 6 output :  " << name.find("ball",0) << endl;

Will never generate a negative number. Hence you get the output:

Line 6 output :  18446744073709551615
Line 6 output :  4

The second bit of code. Has an interesting conversion.

// Here you are converting from a `std::size_t` into an `int`
// The compiler is happy to do this.
// Not sure it is totally legal (I forget the exact place in the standatd)
// But it is so common I would be surprised if it returned anything other than -1.
int num = name.find("ball",0);

So why does 18446744073709551615 convert to -1. Have you looked at the bit pattern for that number.

 18446744073709551615  => 0xFFFFFFFFFFFFFFFF

Notice all the bits are set. When you out that in an integer variable. This has the same value as -1 in 2's complement representation.

Thus this line:

cout << "Line 8 output :  " << num;

Will generated a signed number. And you will get:

Line 8 output :  -1
Line 8 output :  4

CodePudding user response:

The difference is because find returns a size_t not an int. So in the line int num = name.find("ball",0); you are making an implicit cast to int. he code below will give the same result:

std::string name = "For all the times";
size_t num = name.find("ball", 0);
std::cout << num << std::endl;
std::cout << name.find("ball", 0);

See the documentation of std::string::find at C reference

Please note that if the string is not found the return value is std::string::npos which casted to an int gives -1

  • Related