Divya Sharvani
Divya Sharvani

Reputation: 83

How does printf statement work here, when printing integer number?

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

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

Upvotes: 5

Views: 3452

Answers (7)

dulaj sanjaya
dulaj sanjaya

Reputation: 1340

The problem here is C language identify numbers written with 0 leading as octal numbers in Integer data type.So if you want to print the zero at the beginning don't use a zero at the beginning of the 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 have to use sprintf which gives formatted outputs.Here ds means Pointer to an char array.Here value of int variable is stored at char array.

Upvotes: 1

MikeCAT
MikeCAT

Reputation: 75062

printf() will do just a normal operation. Integer constants that begin with 0 and not hexadecimal constant are octal constant as the message suggests.

034 = 3 * 8 + 4 = 28

And by the time the printf sees it at execution time, the number is in binary, looking like this:

011100 (binary)

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

Upvotes: 6

sjsam
sjsam

Reputation: 21965

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

  • No leading zeros indicate a decimal number(base10 => 0-9)
  • A leading 0 indicates an octal number(base8 => 0-7)
  • A leading 0x or 0X indicates a hexadecimal number.(base16 => 0-9,A/a-F/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'.

Upvotes: 9

nalzok
nalzok

Reputation: 16107

Quoted 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 a value as 034, the output is 28.

Due to that leading zero, 034 is considered as an "octal-constant" by the compiler, and 34 in octal is 28 in decimal.

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

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

Upvotes: 2

Amit Kumar
Amit Kumar

Reputation: 59

It isint the printf function, the concern here is of integer value, to be more precise, integer value of base 8, i.e. Octal numbers.

When we feed an input in integer variable like this :

int n = 34;

It simply means we are putting in a Decimal number (of base 10) in variable n. But when we put an additional zero in front of digit, like:

int n = 034;

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

On similar notes, we can also feed a hexadecimal value into a plain integer variable, like :

int n = 0x34;

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

An example will sum it up :

#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 explains everything..

Upvotes: 2

sps
sps

Reputation: 2720

In C if you start an integer with a zero (0) then it is treated as an octal representation of the number. In 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 is working correctly here. 034 is an octal number, which is 28 in decimal. So printf is printing correct value of int variable a which is 34 in octal, which is equivalent to 28 in decimal.

However, the error - while using 038 - is not related to the printf. You get error when you try to use a number like 038 because, you are using an octal representation - by starting the number with 0 - but you are using an invalid digit, 8 , which simply does not exist in octal number system.

P.S: If you start a number with 0x or 0X then that is treated as a hexadecimal representation of the number.

P.S: If you want to print an integer in octal format, the format specifier is %o. For example: printf("num = %o \n", num);

P.S: 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);

Upvotes: 14

exebook
exebook

Reputation: 33900

int a = 34;

printf("0%i\n", a);

Number cannot have zero in front! If you need to print a leading zeros like this 034 or 00345 or 007, you need to print those zeros as characters.

printf("00%i", 7);

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

Upvotes: -3

Related Questions