Reputation: 57
i have this small piece of code that has an integer array containing some elements with a leading 0.
int arr[]={012,234,071};
cout<<arr[0]<<endl; //output 10
cout<<arr[1]<<endl; //output 234
cout<<arr[2]<<endl; //output 57
for some reason am i getting different output for values with leading zeros,can someone explain this to me why is this happening?
Upvotes: 2
Views: 69
Reputation: 2791
integer array element with leading 0 showing garbage output
You do not get "garbage" values; the compiler mainly interprets the values in your array that have leading zeros, like 012, as octal values. The compiler then converts it into a decimal value (for 012, it is 10) and outputs that.
Don't use leading zeroes if you are working with the decimal system.
Upvotes: 0
Reputation:
Numeric literals with leading zeros are assumed to be octal numbers. Don't use leading zeros.
Upvotes: 4