I am experimenting with both unsigned int data types and main method parameters in simple C programs. As an experiment I wrote a program that takes an int number from the command line as a parameter to the main method, and sums every integer between that number and 0.
E.g. The program calculates f(n) = (1 + 2 + 3... + n) valid when n > 0
#include <stdio.h>
#include <stdlib.h>
const unsigned int MAX_NUM = 92681; //Max input that will avoid int overflow later on
unsigned int sum(unsigned int x);
int main(int argc, char *argv[]) {
unsigned int input = atoi(argv[1]);
if (input < 0 || input > MAX_NUM) {
printf("Invalid input! Input must be less than 92682\n");
exit(0); //If input > MAX_NUM, quit program
}
unsigned int result = sum(input);
printf("Sum to %d = %d\n", input, result);
return 0;
}
unsigned int sum(unsigned int x) {
unsigned int sum = 0;
unsigned int y;
for (y = 0; y <= x; y++) {
sum += y;
printf("Current sum:\t%u\n",sum);
}
return sum;
}
The first thing I began to notice was integer overflow when f(n) > 2147483648 - aka the maximum value for a signed int.
I found the maximum values mathematically by hand for which the results generated by my program would be valid (e.g. before integer overflow) to be 65535 for signed ints and 92681 for unsigned ints.
Running the program for signed ints produced the expected results - at 65535 the very large positive number became a very large negative number as the integer overflowed.
I then went through and changed every "int" to "unsigned int". Despite this integer overflow occurs as if the ints were signed and not unsigned.
My question is a) Why is this? b) How can I make it so that my answer can use the whole range of unsigned int i.e. 0 through to (2^32) - 1 (as I dont need negative values!).
Thanks very much!