- Dec 17, 2001
- 3,566
- 3
- 81
Question: Suppose that i is a variable of type int, l is a variable of type long int, and u is a variable of type unsigned int. What is the type of the expression i + (int) l * u?
I figured that l gets cast to int first, then multiplied with u, which promotes l to unsigned int. Then the product (an unsigned int) is added to i, which promotes i to an unsigned int. So the expression as a whole is an unsigned int. I've got no answers and - as I'm on my own - no prof or TA, so I put together the following code and ran it through splint to check:
#include <stdio.h>
#define TESTTYPE unsigned int
#define TESTEXPR (i + (int) l * u)
int main()
{
TESTTYPE t = 0;
int i = 100;
long l = 1000;
unsigned int u = 3000000000;
if ( t == TESTEXPR )
printf("Yippee!\n");
return 0;
}
Splint, however, claims that TESTEXPR is an int, not an unsigned int. Assuming my method of checking is valid (?), why does the expression end up as an int?
I figured that l gets cast to int first, then multiplied with u, which promotes l to unsigned int. Then the product (an unsigned int) is added to i, which promotes i to an unsigned int. So the expression as a whole is an unsigned int. I've got no answers and - as I'm on my own - no prof or TA, so I put together the following code and ran it through splint to check:
#include <stdio.h>
#define TESTTYPE unsigned int
#define TESTEXPR (i + (int) l * u)
int main()
{
TESTTYPE t = 0;
int i = 100;
long l = 1000;
unsigned int u = 3000000000;
if ( t == TESTEXPR )
printf("Yippee!\n");
return 0;
}
Splint, however, claims that TESTEXPR is an int, not an unsigned int. Assuming my method of checking is valid (?), why does the expression end up as an int?
