Is a type cast necessary while converting between signed int and unsigned int?

Lone Learner

I tried assigning a signed int to unsigned int.

#include <stdio.h>

int main()
{
  int a;
  unsigned int b;
  scanf("%d", &a);
  b = a;
  printf("%d %u\n", a, b);
  return 0;
}

I was hoping that compiling this would cause a warning that I am assigning an int value to unsigned int variable. But I did not get any warning.

$ gcc -std=c99 -Wall -Wextra -pedantic foo.c
$ echo -1 | ./a.out
-1 4294967295

Next I tried to assigning an unsigned int to signed int.

#include <stdio.h>

int main()
{
  int a;
  unsigned int b;
  scanf("%u", &b);
  a = b;
  printf("%d %u\n", a, b);
  return 0;
}

Still no warning.

$ gcc -std=c99 -Wall -Wextra -pedantic bar.c
$ echo 4294967295 | ./a.out
-1 4294967295

Two questions:

  1. Why are no warnings generated in these cases even though the input gets modified during the conversions?
  2. Is a type cast necessary in either of the cases?
M.M

Code 1: This conversion is well-defined. If the int is out of range of unsigned int, then UINT_MAX + 1 is added to bring it in range.

Since the code is correct and normal, there should be no warning. However you could try the gcc switch -Wconversion which does produce a warning for some correct conversions, particularly signed-unsigned conversion.

Code 2: This conversion is implementation-defined if the input is larger than INT_MAX. Most likely the implementation you are on defines it to be the inverse of the conversion in Code 1.

Typically, compilers don't warn for implementation-defined code which is well-defined on that implementation. Again you can use -Wconversion.

A cast is not necessary and as a general principle, casts should be avoided as they can hide error messages.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is a type cast necessary while converting between signed int and unsigned int?

From Dev

Typecasted signed int converting like unsigned int

From Dev

Cast signed char to unsigned int in C

From Dev

Cast signed char to unsigned int in C

From Dev

Is resolving char between signed and unsigned int unspecified?

From Dev

Is resolving char between signed and unsigned int unspecified?

From Dev

make_signed<unsigned long>::type is int?

From Java

Type conversion - unsigned to signed int/char

From Dev

Type conversion: signed int to unsigned long in C

From Dev

Does cast between signed and unsigned int maintain exact bit pattern of variable in memory?

From Dev

Adding signed and unsigned int

From Dev

Range of Unsigned & Signed int

From Dev

what is the difference between "unsigned int *ptr" and signed int *ptr"?

From Dev

find the max between 0 and a sum of an unsigned int with a signed int

From Dev

C: Converting unsigned char array to signed int (vice versa)

From Dev

When converting a char to a int in JAVA, why is no cast necessary?

From Dev

Comparison between unsigned int and int without using cast operator

From Dev

Swift converting signed array of Int [int8] to unsigned array of Int [UInt8]

From Dev

Converting unsigned short* to int*

From Dev

Displaying a signed int as unsigned in WPF

From Dev

signed or unsigned int in c++

From Dev

signed or unsigned int in c++

From Dev

Converting 2 bytes to signed int

From Dev

converting unsigned char > 255 to int

From Dev

Converting unsigned char to int and short

From Dev

Converting a string of a byte to an unsigned int

From Dev

Converting 4 bytes to unsigned int

From Dev

Converting unsigned char array to int

From Dev

Can I cast an unsigned char* to an unsigned int*?

Related Related

  1. 1

    Is a type cast necessary while converting between signed int and unsigned int?

  2. 2

    Typecasted signed int converting like unsigned int

  3. 3

    Cast signed char to unsigned int in C

  4. 4

    Cast signed char to unsigned int in C

  5. 5

    Is resolving char between signed and unsigned int unspecified?

  6. 6

    Is resolving char between signed and unsigned int unspecified?

  7. 7

    make_signed<unsigned long>::type is int?

  8. 8

    Type conversion - unsigned to signed int/char

  9. 9

    Type conversion: signed int to unsigned long in C

  10. 10

    Does cast between signed and unsigned int maintain exact bit pattern of variable in memory?

  11. 11

    Adding signed and unsigned int

  12. 12

    Range of Unsigned & Signed int

  13. 13

    what is the difference between "unsigned int *ptr" and signed int *ptr"?

  14. 14

    find the max between 0 and a sum of an unsigned int with a signed int

  15. 15

    C: Converting unsigned char array to signed int (vice versa)

  16. 16

    When converting a char to a int in JAVA, why is no cast necessary?

  17. 17

    Comparison between unsigned int and int without using cast operator

  18. 18

    Swift converting signed array of Int [int8] to unsigned array of Int [UInt8]

  19. 19

    Converting unsigned short* to int*

  20. 20

    Displaying a signed int as unsigned in WPF

  21. 21

    signed or unsigned int in c++

  22. 22

    signed or unsigned int in c++

  23. 23

    Converting 2 bytes to signed int

  24. 24

    converting unsigned char > 255 to int

  25. 25

    Converting unsigned char to int and short

  26. 26

    Converting a string of a byte to an unsigned int

  27. 27

    Converting 4 bytes to unsigned int

  28. 28

    Converting unsigned char array to int

  29. 29

    Can I cast an unsigned char* to an unsigned int*?

HotTag

Archive