Do Negative Numbers Return False in C/C++

Do negative numbers return false in C/C++?

All non-zero values will be converted to true, and zero values to false. With negative numbers being non-zero, they are converted to true.

Quoting from the C++11 standard (emphasis mine):

4.12 Boolean conversions [conv.bool]

1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
to member type can be converted to a prvalue of type bool. A zero
value, null pointer value, or null member pointer value is converted
to false; any other value is converted to true.
A prvalue of type
std::nullptr_t can be converted to a prvalue of type bool; the
resulting value is false.


Are they always true/false regardless of compilers?

You will only get the above guarantee when your compiler is standards-compliant, or at least, complies with this specific part of the standard. In practice, all compilers have this standard behavior, so there isn't much to worry about.

Does if (!(-1)) evaluate to true or false in C?

The evaluation of if (!(-1)) can be worked out by thinking about the operator precedences.

Firstly the unary - is applied to 1 which produces an integral -1. Then this value is logically negated by the !. This involves collapsing -1 into a logical value. The rule for this in C is nice and simple for integral types: 0 is falsy and everything else is truthy.

Therefore -1 is truthy and when the logical negation happens we get false.

Therefore this statement is portably false.

If statement with negative values

In C++, the if statement treats any nonzero value as true. A negative value is not zero, so it will be considered true. The following two statements are equivalent:

if (strcmp(str1, str2))

if (strcmp(str1, str2) != 0)

Are negative boolean values defined?

This is defined behavior. I'll look for the C99 standard paragraph stating as such

§ 6.3.1.2
When any scalar value is converted to _Bool, the result is 0 if
the value compares equal to 0;
otherwise, the result is 1.

Is !x equal to true when x is negative?

For any non-zero x, !x will be zero. So, for x == -1, !x is false.

From cppreference:

The logical NOT operator has type int. Its value is ​0​ if expression
evaluates to a value that compares unequal to zero. Its value is 1 if
expression evaluates to a value that compares equal to zero.

How to return negative value from function?

Well:

return (sub * -1);

Sub will be -321 at this time. And -321 * -1 = 321.

Can someone thoroughly explain this IsItPrime Function?

if(n <= 3) {
return n > 1;
}

Is clever if not the easiest thing to reason out. First you enter the if body if you have a number of 3 or less. This covers the first 2 prime numbers plus all negative numbers, 0, and 1. Then it goes on to return n > 1; this means that if n is greater than 1 you will return true, otherwise false. So, if n is 2 or 3 the function will return true. It is is less than 2 then it returns false. It would be the same as

 if (n <= 1) return false;
else if (n == 2 || n == 3) return true
else if ...

But as you can see that is more typing and adds an if statement.

memory address positive or negative value in c?

Memory addresses should not be interpreted as signed integers in that way. The sign of the number depends on the highest bit set (assuming two's complement representation, which is used by the vast majority of systems currently in use), so a memory address above 0x80000000 on a 32-bit system will be negative, and a memory address below 0x80000000 will be positive. There's no real significance to that.

You should be printing memory addresses using the %p modifier; alternatively, some people use %08x for printing memory addresses (or %016llx on 64-bit systems). This will always print out as an unsigned integer in hexadecimal, which is far more useful than a signed decimal integer.

int a;
printf("%p\n", &a);

Negating largest possible negative value in C

The standard (n2176 draft) says explictely at 6.5 Expressions § 5:

If an exceptional condition occurs during the evaluation of an expression (that is, if the result is not
mathematically defined or not in the range of representable values for its type), the behavior is
undefined.

We are exactly there: the result is not is the range of representable values for the type, so it is explicitely UB.

That being said, most implementation use 2'complement for negative values and process operations on signed types as the operation on the unsigned type values having the same representation. Which is perfectly defined.

So getting -2^(31) again can be expected on common implementations. But as the standard says that it is UB, it cannot be relied on.



Related Topics



Leave a reply



Submit