Why is 050 not equal to 50 in the next snippet?

Why is it 050not equal 50in the next fragment?

#include <stdio.h>
int main(void) {
    int x=050,y=50;
    int ans;
    ans= x==y ? x+3 : y+7;
    printf("%d\n",ans);
}
+5
source share
5 answers

Because it 050is considered octal, but 50considered decimal.

So x = 050basically means x = 40.

6.4.4.1/3

A decimal constant begins with a non-zero digit and consists of a sequence of decimal digits. The octal constant consists of pre fi x 0 optionally followed by only a sequence of digits from 0 to 7.

+7
source

050 is interpreted as octal, and 8 instead of 10 as the base of numbers.

+3
source

, 0x50 50 050

  • 50 - 10
  • 0x50 - 16
  • 050 - base 8 (never seen the need to use this, ever)
+1
source

Because it 050is an octal constant.

+1
source

050 is 40 in octal. 0 turns a number into an octal literal.

+1
source

All Articles