-1

How i can print for example?

int a = 0145236

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

Miko 65
  • 29
  • 1
  • 3
  • 1
    What is `Int`? How do you print it? Do you realize that `0145236` in C is an octal integer constant that stands for decimal `51870`? – AnT stands with Russia Oct 24 '17 at 17:08
  • I don't think there's any way to change that, it's just part of C syntax. Don't put leading zeroes in your source code if you don't want that. – Barmar Oct 24 '17 at 17:09
  • You can convert it to string and add `0` at the beginig of the string and than print it – Ivan Sheihets Oct 24 '17 at 17:10
  • It seems that you're mixing data with presentation. An integer is an abstract concept, it's not the same as what gets printed on the screen. Put the zero in only when you're printing it. – JJJ Oct 24 '17 at 17:10
  • @Barmar i must have int number = 01431676 and do some operations with that number. I cant use number = 1431676 – Miko 65 Oct 24 '17 at 17:11
  • Why must you have that? – Barmar Oct 24 '17 at 17:14
  • You can use `printf("Int a = 0145236\n");` to get the output you say you want. Since that's unlikely to be what you mean, you should show compilable code that produces the value you claim. If you have `int a = 0145236;`, you can use `printf("int a = %07o\n", a);` to get a reasonable representation of what you're after. You might need to use `printf("int a = %#o\n", a);` for the more general case. But you need to show what you're trying and what's going wrong, and to read the manual page for printf. Reading the number is another bag'o'worms. Beware `scanf()` and using `%i` (better: `%o`). – Jonathan Leffler Oct 24 '17 at 17:18
  • @Barmar becouse I must from my Studentnumber this do. And number is 01431676 – Miko 65 Oct 24 '17 at 17:21
  • 1
    Don't use an `int` for that, use a string. – Barmar Oct 24 '17 at 17:21
  • Or remove leading zeroes when you get the input, before parsing it as a number. – Barmar Oct 24 '17 at 17:23
  • Do student numbers ever contain digits 8 or 9? If so, then you cannot use octal constants in the code for 03948326. You either have to treat it as a string or remove the leading zero in the source, or read it from file with `%d` (and not `%i`) and print it with `%.8d` (8 digits decimal, leading zeros if needed). – Jonathan Leffler Oct 24 '17 at 17:25
  • Use `strtoul(string, NULL, 10)` This will force base 10. – stark Oct 24 '17 at 17:25
  • Sound like an XY Problem. What Problem are you __actually__ trying to resolve? Give us some context. – Jabberwocky Oct 24 '17 at 17:26

1 Answers1

0

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?

Prashanthv
  • 109
  • 7