-3

Here i am confused with condition statement which has negative number,if in the condition only negative number is given as if(-1) then it is true but if (-1>0) then it become false please explain any one thanks in advance

if(-1) // This true why and how?
if(-1>0)//This is false why and how

Now what is impact in below code please help to understand

#include <stdio.h>
#include <string.h>

main()
{
  char a[]="he";
  char b[]="she";
  if(strlen(a)-strlen(b)>0)//how it is true ?if(2-3>0)i.e if(-3>0) which is false
    //here why it is true                   
  {
    printf("-ve greater then 0 ");
  }
  else 
  {
    printf(" not greater then 0");
  }
}
BECOOL
  • 203
  • 1
  • 3
  • 11
  • 2
    The issue with your below code is due to signed/unsigned comparison, not relevant to what you asked above. See: [“strlen(s1) - strlen(s2)” is never less than zero](http://stackoverflow.com/questions/10474769/strlens1-strlens2-is-never-less-than-zero) – interjay Jul 10 '14 at 10:14
  • 1
    Your `strlen()` question is interesting (perhaps the only interesting bit in your question that is not directly answered by any of the numerous documents for learning C). You should have asked only that so that answerers would not focus on the first two trivial questions. – Pascal Cuoq Jul 10 '14 at 10:15
  • This is an unsigned integer of type size_t return value of strlen. So will be calculated in unsigned. change to `if((int)strlen(a)-(int)strlen(b)>0)` – BLUEPIXY Jul 10 '14 at 10:15
  • `if(2-3>0)i.e if(-3>0)` -- I don't know how you come to that conclusion. `2-3 > 0` is equivalent to `-1 > 0`. – JJJ Jul 10 '14 at 10:19
  • What is wrong with `if(strlen(a) > strlen(b))`? – Mohit Jain Jan 22 '15 at 07:25

5 Answers5

4

if(-1) // This true why and how?

Any non-zero number is evaluated as true.

if(-1>0) //This is false why and how

-1 is less than 0 that's why expression -1 > 0 evaluated to false.

if(strlen(a)-strlen(b)>0) //how it is true ?if(2-3>0)i.e if(-3>0) which is false //here why it is true

strlen returns size_t type which is unsigned. The result of strlen(a)-strlen(b would be an unsigned int. But -1 in not unsigned, therefore it is converted to unsigned before comparison.strlen(a)-strlen(b)>0 will result in comparison UINT_MAX -1 > 0

haccks
  • 104,019
  • 25
  • 176
  • 264
  • thank u sir but in above code if(strlen(a)-strlen(b)>0) then this statement should be false coz it is equivalent to if(2-3>0) ie if(-3>0)but why it is true ? – BECOOL Jul 10 '14 at 10:18
2

Function strlen() returns a size_t, which is an unsigned integer type.

For this reason, strlen(a)-strlen(b) is an unsigned subtraction. It will produce a nonnegative number because it will produce a size_t and all values of size_t are nonnegative.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
  • 1
    I do not see anything wrong with this answer. Why the down vote ? – haccks Jul 10 '14 at 11:34
  • 1
    @haccks The downvoter thought that this part of the question shouldn't be answered as it was a duplicate of an existing question. I wonder why a moderator thought it useful to erase the back-and-forth that took place though. – Pascal Cuoq Jul 10 '14 at 12:11
1

Every value, which is not 0, gets evaluated as true in if sentence.

c0dehunter
  • 6,412
  • 16
  • 77
  • 139
1

Adding to existing answer:

char a[]="he";
char b[]="she";
if(strlen(a)-strlen(b)>0)

strlen returns size_t. Result of different of size_t would be size_t too. Unsigned numbers are always >= 0. In your case it is similar to

if((2U - 3U) > 0U)

See live code here.

You should rewrite the condition to:

if(strlen(a) > strlen(b))
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
0

The idea that any nonzero number is considered true is a very old concept in computer science. Think of it as treating the raw bits as inputs to a big OR gate. If any bit is a one, then the output is a one (or true). A negative number is a number where the most significant bit, the sign bit, is a one.

penguin359
  • 1,289
  • 13
  • 26