0

My WIN32 (C++) code has a UINT lets call it number. The value of this UINT (or INT doesn't matter) start with a 0 and is recognized as an octal value. It's possible to use the standart operators and the value will keep the octal-system. The same is possible with hex (with foregoing 0x). The problem is I have to use the Value of number in a buffer to calculate with it without changing the value of number. I can assign a value like 07777 to buffer on declaration line but if use an operation like buffer = number the value in buffer is recognized on decimal base.

Anybody has a solution for me?

  • 1
    @juanchopanza: You should post that as an answer. – Keith Thompson Jun 17 '15 at 20:17
  • @KeithThompson I can't figure out what the question is. Feel free to post an answer. – juanchopanza Jun 17 '15 at 20:18
  • why can I use the operation number++ and it counts like ...6 -> 7 -> 10 -> 11 -> 12... ? If there is no difference how is it possible? Could you give me a hint, please? *edit* to complete... if I assign buffer = number with a value of 77 and use the operation buffer++ it count 77 -> 78 -> 79... – stunner2002 Jun 17 '15 at 20:21
  • "the value will keep the octal-system" is just baloney. Have you configured your debugger to display certain variables in octal? – Ben Voigt Jun 17 '15 at 20:44

1 Answers1

7

There's no such thing in C as an "octal value". Integers are stored in binary.

For example, these three constants:

  • 10
  • 012
  • 0xA

all have exactly the same type and value. They're just different notations -- and the difference exists only in your source code, not at run time. Assigning an octal constant to a variable doesn't make the variable octal.

For example, this:

int n = 012;

stores the value ten in n. You can print that value in any of several formats:

printf("%d\n", n);
printf("0%o\n", n);
printf("0x%x\n", n);

In all three cases, the stored value is converted to a human-readable sequence of characters, in decimal, octal, or hexadecimal.

Anybody has a solution for me?

No, because there is no actual problem.

(Credit goes to juanchopanza for mentioning this in a comment.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631