Click here to Skip to main content
15,891,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<iostream>
#include<string.h>
using namespace std;

int main()
{
int a =0;
char s[]="WAITING";
if(a < strlen(s))  //Typecasting of strlen can prevent from warning like[if(a < ((int)strlen(s)))]
cout<<"having string\n";
else
cout<<"Not Having string\n";
}



is it fine? because am getting warning message like [
warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
], i need to reduce the warning message while typecasting in front of strlen my coding not showing any warning but i do not know it is right or wrong?

What I have tried:

please check what am doing is right or wrong?
Posted
Updated 3-Feb-22 5:02am
v2

If you cast strlen to an integer, you are potentially losing information: you are casting a bigger value to a smaller one.

If you cast an int to a unsigned char, you "throw away) everything above the eighth bit: 255 is the largest value an unsigned char can hold. Since the definition of strlen is that it returns a size_t value, and definition of size_t is implementation (and system) dependant, it could be much larger than the size of an integer, and casting could give you the wring value.

Instead of casting, define a as size_t instead of integer.

When you are starting to learn coding, you should generally consider all warnings as errors as the compiler really does know better than you! :laugh:
In fact, my standard compiler commands are set to "treat all warnings as errors" even now so my code can't compile with a warning.
 
Share this answer
 
Comments
CPallini 3-Feb-22 2:11am    
5.
Instead of casting, use the proper data type, i.e.
C++
#include<iostream>
#include<cstring>
using namespace std;

int main()
{
  size_t zero = 0;
  char s[]="WAITING";
  if(zero < strlen(s))
    cout<<"having string\n";
  else
    cout<<"Not Having string\n";
}


You may also use C++ strings

C++
#include<iostream>
using namespace std;

int main()
{
  string s = "WAITING";
  if( s.empty() )
    cout<<"Not having string\n";
  else
    cout<<"Having string\n";
}
 
Share this answer
 
The previous solutions are correct. I will add one small thing. Unless you know exactly the size of the data types you are using and the ranges of your data then it is best NOT to cast. As someone with a lot of experience, this specific case is one where I nearly always cast the result. That is because ints are 32-bits so they have a range of up to 2GB and the chances of my code seeing a string bigger than that are remote, at best. Nothing up-stream would allow one of that size so it just will not happen. However, your best option is to take the advice given previously: use the same type returned by the function.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900