Sir please consider the following question and code.
1) printing a integer variable 'a' having value less than 1000.
putchar( (a/100) + ' 0 ' );
a=a%100;
putchar( (a/10) + ' 0 ' );
a=a%10;
putchar(a + ' 0 ' );
Could you please explain the above.
Thank you.
Hi. putchar takes an integer as a parameter and prints the ascii character for that value. a/100 is an integer division and will be 1 for all numbers of 100 and above. It will be 0 for numbers below 100.
+ '0' will be an integer addition of the value of the char '0' which is 48 and the division a/100.
Therefore, if a is 100 or greater the result is 1 + 48 or 49 which is the character '1'
If, for example, a == 123 then the next part is a modulus of a % 100 which is 23
23 / 10 is 2 and 2 + '0' is 50 which is the char 2
a % 10 or 23 % 10 is 3
3 + '0' or 3 + 48 is 51 which is the character 3