2

I have the following code:

int main()
{ 
    int i;
    unsigned int j;

    printf("Type a number:  ");
    scanf("%d", &i);

    j = i;

    if (________)
    {
        printf("Negative!\n");
    }

    return 0;
}

What would I put in the if statement in order to check whether the unsigned variable "j" contains a negative number?

nalzok
  • 14,965
  • 21
  • 72
  • 139
readytotaste
  • 199
  • 2
  • 4
  • 17
  • 8
    By definition, an `unsigned int` does not contain negative numbers. – Dolda2000 Feb 16 '16 at 05:41
  • 1
    http://stackoverflow.com/q/9045436/3779597 – timthez Feb 16 '16 at 05:42
  • 2
    http://stackoverflow.com/questions/5169692/assigning-negative-numbers-to-an-unsigned-int – Kai Wu Toh Feb 16 '16 at 05:44
  • Check for an equivalent negative `int` value only requires checking the highest bit, if it is `1`, then that unsigned number would be represented as a negative `int` value -- but you would have to further check that the value does not exceed `INT_MIN` before proceeding with any such conversion, otherwise you need to extend to a long value. – David C. Rankin Feb 16 '16 at 06:12
  • 2
    Your answer is `if (i >> 31 & 1) printf ("value is negative\n");` – David C. Rankin Feb 16 '16 at 06:24
  • @DavidC.Rankin: That makes some assumptions about how integers are represented. Those assumptions are *nearly* 100% valid, but they're unnecessary. Just test the value of `i`. – Keith Thompson Dec 06 '17 at 02:47

4 Answers4

3

You can do simply this:

  if((int)j < 0) 
  {
   printf("Negative!\n");
  }
SH7890
  • 545
  • 2
  • 7
  • 20
c-smile
  • 26,734
  • 7
  • 59
  • 86
  • That's likely, but not guaranteed, to work. Converting a value greater than `INT_MAX` to `int` yields an implementation-defined result. – Keith Thompson Dec 06 '17 at 02:48
2

You could just check the variable i as for example

if ( i < 0 )
{
    //...
}

or you could write for example

if ( j > INT_MAX )
{
    //...
}

because the internal representation of non-negative values of type int coincides with the internal representation of the same values of type unsigned int. At least it will work for the 2 complement representation of integer numbers.

And at last you could check the sign bit.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

This is one way of doing it :

#include<stdio.h>
/* This is in easier to verify example.
 * I have used char to represent integers that fits in one byte.
 * char is one byte in my system
 * In case of an overflow, the numbers are wrapped around.
 * You could check with bigger types as you please.
 */

int main(void)
{
unsigned char x;
char y;
        printf("Size of char is %zd\n",sizeof(char));
        printf("Enter an integer : ");
        scanf("%d",&x);
        y=x;
        printf("Number is %s when converted to signed\n",\
                             (y>=0?"positive":"negative"));
return 0;
}

Checking the same with INT_MIN or INT_MAX requires you to include limits.h header file. Bit manipulation as pointed out by David is yet another option.

sjsam
  • 21,411
  • 5
  • 55
  • 102
0
int i;
unsigned int j;

// store a value in i

j = i;

To test whether j contains a negative number:

if (0)
{
    printf("Negative!\n");
}

An unsigned integer can never hold a negative value.

If the value of i is negative, then it will be converted to some positive value when you assign it to j. If you want to know whether the value of i is negative, you should test i, not j.

if (i < 0) {
    puts("i is negative");
}
else {
    puts("i is not negative");
}
puts("j is not negative");

Note that converting the value of j back to int is not reliable. Conversion of a value exceeding INT_MAX to type int yields an implementation-defined result (or raises an implementation-defined signal). It's very likely you'll get back the original value of i, but why bother? If you want to test the value of i, test the value of i.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631