We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › int constants with leading zero are OCTAL
Page Index Toggle Pages: 1
int constants with leading zero are OCTAL (Read 612 times)
int constants with leading zero are OCTAL
Jan 24th, 2009, 10:24pm
 
I can't find this documented/mentioned anywhere on the site so here we go...

I just had a bit of a run-around trying to work out why this array intialisation wouldn't compile:

Code:
int[] numbers = { 090, 150, 30 }; 



(The error message is Expecting DOT, found 'numbers')

It turns out that integer constants with a leading zero are assumed to be octal, and so "090" is illegal.  (I had a big pile of constants I'd pasted in from a spreadsheet that had leading zeros...)

Could this be added to the documentation for int maybe?  

This demonstrates the octal interpretation:

Quote:
int[] n1 = { 040, 245, 010 };

void setup() {
  for (int i = 0; i < n1.length; i++) {
    println("n1[" + i + "] = " + n1[i]);
  }
}



running it produces...

n1[0] = 32
n1[1] = 245
n1[2] = 8




Chris
Re: int constants with leading zero are OCTAL
Reply #1 - Jan 24th, 2009, 10:36pm
 
It is something going back to the C language (at least), kept in C++ and still in Java (and even JavaFX, I think).
Octal notation isn't much used today, in favor of hexadecimal notation. That's probably why it wasn't mentioned in the Processing reference...
You were lucky you had illegal digits in the octal number, otherwise you would have had a subtle bug instead... Smiley
Page Index Toggle Pages: 1