rbl651
rbl651

Reputation: 57

integer array element with leading 0 showing garbage output

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

Answers (2)

BusyProgrammer
BusyProgrammer

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

user2100815
user2100815

Reputation:

Numeric literals with leading zeros are assumed to be octal numbers. Don't use leading zeros.

Upvotes: 4

Related Questions