ATOF produces only 0.00

Before I include a function into my sketch I am trying it in a sep. sketch to test it. So I wrote this to test the atof function:

char X = "123.45\0"; // String1
char Y = "0.67\0";  // String2

void setup() {
  Serial.begin(9600);
  delay(500);
  float x = atof(X);
  float y = atof(Y);
  int sX = sizeof(X);
  int sY = sizeof(Y);

  Serial.println("Ready");
  Serial.print("x = "); Serial.println(x);
  Serial.print("y = "); Serial.println(y);
  Serial.print("Sx= "); Serial.println(sX);
  Serial.print("Sy= "); Serial.println(sY);
}

void loop() {
}

But all it produces are zero's, why??

Don't you trust the compiler to terminate the string for you?
Didn't you get any warnings?

Define the character arrays properly. Note the square brackets.

char X[] = "123.45";

Hi, try this code:

char X[] = "123.45"; // String1
char Y[] = "0.67";  // String2
//String K = "123.45";
void setup() {
  Serial.begin(9600);
  delay(500);
  float x = atof(X);
  float y = atof(Y);
  int sX = sizeof(X);
  int sY = sizeof(Y);

  Serial.println("Ready");
  Serial.print("x = "); Serial.println(x);
  Serial.print("y = "); Serial.println(y);
  Serial.print("Sx= "); Serial.println(sX);
  Serial.print("Sy= "); Serial.println(sY);
}

void loop() {
}

ref: C library function - atof()

I'm surprised this produces zeroes.

X is char
sizeof(X) is 1

I’m turning my phone upside down, moving it far and close and that 1 does not look like a 0 no matter what I do, help!

Didn't you notice the dire warnings?

If you ever get unexpected results from a sketch, set "Verbose messages during: [X] compilation" in Preferences and look for compiler warnings.

/Users/john/Documents/Arduino/sketch_jan06b/sketch_jan06b.ino:1:10: warning: invalid conversion from 'const char*' to 'char' [-fpermissive]
 char X = "123.45\0"; // String1
          ^~~~~~~~~~
/Users/john/Documents/Arduino/sketch_jan06b/sketch_jan06b.ino:2:10: warning: invalid conversion from 'const char*' to 'char' [-fpermissive]
 char Y = "0.67\0";  // String2
          ^~~~~~~~
/Users/john/Documents/Arduino/sketch_jan06b/sketch_jan06b.ino: In function 'void setup()':
/Users/john/Documents/Arduino/sketch_jan06b/sketch_jan06b.ino:8:19: warning: invalid conversion from 'char' to 'const char*' [-fpermissive]
   float x = atof(X);
                   ^
In file included from /Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.8.4/cores/arduino/Arduino.h:23:0,
                 from sketch/sketch_jan06b.ino.cpp:1:
/Users/john/Library/Arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/avr/include/stdlib.h:361:15: note:   initializing argument 1 of 'double atof(const char*)'
 extern double atof(const char *__nptr);
               ^~~~
/Users/john/Documents/Arduino/sketch_jan06b/sketch_jan06b.ino:9:19: warning: invalid conversion from 'char' to 'const char*' [-fpermissive]
   float y = atof(Y);
                   ^
In file included from /Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.8.4/cores/arduino/Arduino.h:23:0,
                 from sketch/sketch_jan06b.ino.cpp:1:
/Users/john/Library/Arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/avr/include/stdlib.h:361:15: note:   initializing argument 1 of 'double atof(const char*)'
 extern double atof(const char *__nptr);
               ^~~~
1 Like

Oops! I meant "Compiler warnings: [All]". That is something you should turn on and leave on. The "Show verbose output" options are better for upload errors.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.