S
Spiros Bousbouras
#include <stdio.h>
int main(void) {
unsigned int foo = 2026363600u ;
printf("off2 = %lu\n",
838237499u * foo - 2137600414u);
return 0;
}
Assuming C99 and that UINT_MAX == 1073741823 what will the above
output? I'm thinking that foo will be assigned the value
2026363600 % 1073741823 = 952621777 , the multiplication
838237499u * foo will be done in unsigned so it will give
( 838237499 * 952621777 ) % 1073741823 = 504442367 , 2137600414u
will be unsigned long because it's larger than UINT_MAX so
504442367 will also be promoted to unsigned long and the
subtraction will happen modulo ULONG_MAX so it will give the
unsigned long result ULONG_MAX - (2137600414 - 504442367) which
will be the final output. Have I got it right?
A related question: what happens if an integer constant in the
source code cannot fit in any integer type the implementation
supports? Is it UB? Is the compiler obliged to give a warning
because the constraint in p2 of 6.4.4 was violated?
int main(void) {
unsigned int foo = 2026363600u ;
printf("off2 = %lu\n",
838237499u * foo - 2137600414u);
return 0;
}
Assuming C99 and that UINT_MAX == 1073741823 what will the above
output? I'm thinking that foo will be assigned the value
2026363600 % 1073741823 = 952621777 , the multiplication
838237499u * foo will be done in unsigned so it will give
( 838237499 * 952621777 ) % 1073741823 = 504442367 , 2137600414u
will be unsigned long because it's larger than UINT_MAX so
504442367 will also be promoted to unsigned long and the
subtraction will happen modulo ULONG_MAX so it will give the
unsigned long result ULONG_MAX - (2137600414 - 504442367) which
will be the final output. Have I got it right?
A related question: what happens if an integer constant in the
source code cannot fit in any integer type the implementation
supports? Is it UB? Is the compiler obliged to give a warning
because the constraint in p2 of 6.4.4 was violated?