0

I was wondering how could I print just the last 5 digits of an octal number in C programming language?

Tricky part: the number is unsigned in definition, so after some procedures it outputs something like 3777777464. What I want to print is just 77464.

I tried to look around in C formatting guides, but I only found out how to write the first 5 digits/characters, not the last 5 ones in regards to the tricky part mentioned.

It is only needed to be displayed in output (printf/fprintf in a file).

EDIT and emphasize: The code is too long to post here, It consists of many c files and headers. To make it concise: I need printf("%05o\n",arr[i]); to print only last 5 digits. It prints something like 3777777464. The numbers are correct, I just need the last 5 digits

Thank you very much for helping me

newB
  • 9
  • 4
  • "after some procedures it outputs something like 3777777464" --> post that code. – chux - Reinstate Monica Mar 28 '20 at 15:38
  • what procedures? –  Mar 28 '20 at 15:39
  • the code is too long to post here, it consists of many c files and headers. to make it concise: i need printf("%05o\n",arr[i]); to print only last 5 digits. it prints something like 3777777464. the numbers are correct, i just need the last 5 digits – newB Mar 28 '20 at 16:05
  • So use the code chux presented. Did you use it? What did it result in? Were you satisfied with the result? Doesn't it answer your question? `i need printf("%05o\n",arr[i]);` - what is `arr`? what is `i`? – KamilCuk Mar 28 '20 at 16:13

1 Answers1

4

print just the last 5 (octal) digits of a number

Derive the last 5 octal digits by anding with a mask of the 077777.

#define MASK_LAST_5_OCTAL_DIGITS 077777u

//       vv print at least 5 characters, pad with 0 as needed 
printf("%05o\n", (unsigned) (number & MASK_LAST_5_OCTAL_DIGITS));
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256