简体   繁体   中英

Print number with zero on first place

How i can print for example?

int a = 0145236

When I want to use it, it always gives me a octal number.

First, this does not seem to be C -- unless you use a typedef, the integer type in C is int , case-sensitive.

In ISO 9899 6.4.4.1 Integer constants -- Syntax , it is written the Backus-Naur form for the constants:

octal-constant:
  0
  octal-constant  octal-digit

octal-digit: one of
  0 1 2 3 4 5 6 7

So when you write an initializer constant starting with 0<octal-digit> the lexer will give it some integer type and it will initialize it in base 8.

Whenever you put zero in front of your number it is printed in octal only. That is the way c works. However, if you take input from the user and supposing user entered 0123456 it is stored in your variable as 123456 so just don't add 0 in the beginning of your integer number when hard coding. In case you need to add leading zeros in your number this may help Printing leading 0's in C?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM