Why is 050 not equal to 50 in the following snippet?
#include <stdio.h>
int main(void) {
int x=050,y=50;
int ans;
ans= x==y ? x+3 : y+7;
printf("%d\n",ans);
}
Because 050 is considered octal and 50 is considered decimal.
So x = 050 basically means x = 40.
6.4.4.1/3
A decimal constant begins with a nonzero digit and consists of a sequence of decimal digits. An octal constant consists of the prefix 0 optionally followed by a sequence of the digits 0 through 7 only.
050 is interpreted as octal, with 8 instead of 10 as the number base.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With