I have code that loops through numbers and creates the character array representation of that integer. So for a number like 1234 I get an array that looks like {‘1’, ‘2’, ‘3’, ‘4’}
Part of the code is shown below:
do {
//print here
c[i++] = (char)(((int)'0')+(num - (num/10)*10 ));
} while ((num = num/10) != 0);
I am having an issue when it comes to large data types like long long int: 18446612134627563776
I printed the values in the loop are:
18446612134627563776
18446730879801352832
18446742754318731738
...
18446744073709551615
The values should be
18446612134627563776
1844661213462756377
184466121346275637
...
18
1
The strange thing is that the loop terminates. The last printed value is 18446744073709551615 != 0, so not sure why it terminated there. I think its some issue with the data type that i am not doing right.
This is the print statement:
printk("long=%llu sec=%llu , char=%c\n", num, (num/10)*10, (char)(((int)'0')+((num - (num/10)*10 ))));
long long intis a signed type, usually 64 bits wide, with the maximal representable numberYour value is larger than that, and overflows, probably to
The printed values are
Change the type to
unsigned long longto get the expected results.