2

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?

0

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

" values in your array that have trailing zeros as octal values." - That would be leading zeros.
@NeilButterworth Sorry for the vocab confusion. I'll fix it, give me a sec :)
@NeilButterworth I've adjusted my answer, could you please remove your above comment? Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.