Pages: [1]   Go Down
Author Topic: Forcing Data Types  (Read 565 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry if this seems too simple, but why do I get different results when passing '040' and '40' as an argument?
I'm programming a lathe and arguments are in 1/1000th inches, so '040' is much easier to read.
Thanks,
  redmiata

Quote

void a_function(int variable_1, int variable_2) {
  Serial.print("variable_1 = ");Serial.println(variable_1);
  Serial.print("variable_2 = ");Serial.println(variable_2);
}

void setup (){
  Serial.begin(9600);
}

void loop(){ 
  a_function(040,40);
  a_function(40,040);
  delay(10000);
}

Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Numbers with a leading zero, like 040, are read by the compiler as octal (base 8 ).
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'm programming a lathe and arguments are in 1/1000th inches, so '040' is much easier to read.
Much easier than what? " 40" looks better to me. And the compiler, if that's important. (Without the quotes, of course.)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I understand the programming error now thanks to dxw00d...
In machining referring to a measurement as "040" (oh-four-oh) is understood to mean 40/1000 or forty thousandths.
Logged

Pages: [1]   Go Up
Jump to: