In C , if you start an integer with zero ( 0 ), then it is considered as an octal representation of the number. In the octal representation, there are only 8 valid digits. They are 0, 1, 2, 3, 4, 5, 6, and 7.
So you can see that printf working correctly. 034 is the number of octal , which is 28 in decimal value. So printf prints the correct value of int a , which is 34 in octal, which is equivalent to 28 in decimal.
However, the error - when using 038 - is not related to printf . You get an error when trying to use a number, for example 038 , because you are using the octal representation - by starting the number with 0 - but you are using an invalid digit, 8 , which simply does not exist in the octal number system.
PS: If you start a number with 0x or 0x , then this is considered as a hexadecimal representation of the number.
PS: If you want to print an integer in octal format, the format specifier is %o . For example: printf("num = %o \n", num);
PS: If you want to print an integer in hexadecimal format, the format specifiers are %x or %x . For example: printf("num1 = %x, num2 = %X \n", num1, num2);
source share