How does the printf operator work here when printing an integer?

#include<stdio.h> int main() { int a=034; printf("%d",a); return 0; } 

If I give the value as 034, the output is 28. if it is 028, it gives an error saying "invalid digit" 8 "in octal constant."

+5
source share
7 answers

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);

+14
source

Integers can be expressed in decimal, octal, or hexadecimal.

  • No leading zeros indicating a decimal number (base10 => 0-9)
  • Leading 0 indicates an octal number (base8 => 0-7)
  • The leading 0x or 0X indicates a hexadecimal number. (base16 => 0-9, A / aF / f)

For example, 32, 040, and 0x20 are decimal, octal, and hexadecimal representations of the same value.

So if

 int a=028; 

gives you:

 error: invalid digit "8" in octal constant 

The reason is obvious: "8" is not a valid octal digit.

+9
source

printf() will only perform a normal operation. Integer constants that start with 0, not a hexadecimal constant, are an octal constant, as the message suggests.

034 = 3 * 8 + 4 = 28

And by the time printf sees it at run time, the number is in binary format, as follows:

 011100 (binary) 

That is, sixteen plus eight plus four or 28 (decimal)

+6
source

This is the printf function, here we are talking about an integer value, more precisely, an integer value of base 8, i.e. octal numbers.

When we root the input to an integer variable as follows:

 int n = 34; 

It just means that we put the decimal number (base 10) in the variable n. But when we add an extra zero before the digit, for example:

 int n = 034; 

The compiler interprets it as if we want to put the value of base 8, Octal number, in the variable n. Therefore, instead of putting an equal decimal number 34, it processes the input constant as Octal ((3 * 8) + 4 = 28) and puts the corresponding binary value for it in n.

In such notes, we can also pass the hexadecimal value to the plain integer variable, for example:

 int n = 0x34; 

Here, the compiler will simply put the binary equivalent of the hexadecimal value 0x34 ((3 * 16) + 4 = 52) into the variable. The note. in "0x" the first character is zero 0, not the character "o" or "O"

An example will summarize:

 #include<stdio.h> int main(){ int n; //feeding in a Decimal Value n = 34; printf("\n\nDecimal value with\t%%d : %d", n); //feeding in an Octal Value n = 034; printf("\n\nOctal Value with\t%%o : %o", n); printf("\nOctal Value with\t%%d : %d", n); //feeding in a Hexa-decimal Value n = 0x34; printf("\n\nHexadecimal Value with\t%%d : %d", n); printf("\nHexadecimal Value with\t%%x : %x", n); return 0; } /* Output: Decimal value with %d : 34 Octal Value with %o : 34 Octal Value with %d : 28 Hexadecimal Value with %d : 52 Hexadecimal Value with %x : 34 */ 

Hope this all explains.

+2
source

Quote from N1570, 6.4.4.1 Integer constants : (<- See this link for more information.)

 integer-constant: ...... octal-constant integer-suffixopt ...... ...... octal-constant: 0 octal-constant octal-digit octal-digit: one of 0 1 2 3 4 5 6 7 

If I give the value as 034, the output is 28.

Because of this leading zero, 034 treated as an “octal constant” by the compiler, and 34 in octal as 28 in decimal.

if it is 028, it gives an error saying "invalid digit" 8 "in octal constant."

Again, leading zero makes 028 "octal constant." However, as you can see above, 8 is not a legal "octal digit", so the compiler gives you an error.

+2
source

The problem is that the C language identifies numbers written with 0 leading as octal numbers in the Integer data type. Therefore, if you want to print zero at the beginning, do not use zero at the beginning of an integer. Try this code. It gives zero at the beginning.

 #include<stdio.h> int main() { int a=34; char ds[80]; sprintf(ds,"%03d",a); puts(ds); return 0; } 

You should use sprintf, which gives formatted output. Here ds means a pointer to a char array. Here the value of the int variable is stored in a char array.

+1
source
 int a = 34; printf("0%i\n", a); 

The number cannot have zero in front! If you need to print leading zeros like these 034 or 00345 or 007 , you need to print these zeros as characters.

 printf("00%i", 7); 

When a number starts from zero in C / C ++ / JavaScript and some other languages, it just means some kind of special rarely used type of number called Octal.

-3
source

All Articles