getc() and putc() functions of class Serial

09 Nov 2010

I am a bit confused about the getc() and putc() functions of class Serial. In the usage notes it seems like they return a value of type int and that putc() accepts an int. But in many of the usages it seems like the variable assigned is declared as type char. So what is the type for these functions, both of the input parameter and the returned value.

I am writint a small code to read a value from analogIn and then write it on the terminal. All I get is zero. I think its because I am maybe passing a float to the putc().

09 Nov 2010

int is the C convention (see standard functions fgetc/fputc). The actual value read/written is 8 bits wide (char).

Note that putc/getc operate with raw ASCII character values (e.g. 33 is '!'). If you want to print an integer or float as a string, you should use printf or similar function:

int int_val = 1234;
printf("Int value: %d\n", int_val);
float float_val = 12.34;
printf("Float value: %g\n", float_val);

See the function reference for more details.

09 Nov 2010

Thanks Igor.