1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// From office hours
int main(int argc, char* argv[]) {
int n = INT_MIN;
unsigned int n_unsigned; // not initialized because it will be done in one of the two if/else branches below
if(n >= 0) {
n_unsigned = (unsigned int) n; // Without the cast, gcc would complain about assigning a (signed) int to an unsigned int, since if it were negative it would cause errors.
}
else {
n_unsigned = (unsigned int) -n;
}
printf("%u\n", n_unsigned);
// … or better yet
unsigned int n_unsigned = (unsigned int)(n >= 0 ? n : -n);
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2019 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.