using char

Hi,

i dont' undestand why the variable X is not write on the screen

void setup()

{

Serial.begin(9600);

char x= 1;
Serial.println(x);
Serial.println("fin");

}

void loop() {}

et on the screen

fin

I don't understand why "1" missed but when i write

void setup()

{

Serial.begin(9600);

unsigned char x = 1;
Serial.println(x);
Serial.println("fin");

}

void loop() {}

i gain

1
fin

I don't know if i 'm clear but for me it's a mystery
Thanks you

char x= 1;

The value 1 is not a printable character. The value '1' IS.

.println() comes in a wide variety of versions:

    size_t println(const __FlashStringHelper *);
    size_t println(const String &s);
    size_t println(const char[]);
    size_t println(char);
    size_t println(unsigned char, int = DEC);
    size_t println(int, int = DEC);
    size_t println(unsigned int, int = DEC);
    size_t println(long, int = DEC);
    size_t println(unsigned long, int = DEC);
    size_t println(double, int = 2);
    size_t println(const Printable&);
    size_t println(void);

The version that takes a single 'char' does a .write() of that char. The version that take a single UNSIGNED char prints the numeric value of the unsigned char. Since the character code 1 is not printable but the value 1 is printable that explains why you see one and not the other.

If you want to print the value of the char, use:

.println((int)x);

That tells the compiler to convert the 'char' to 'int'.

Normally you would write:

char x = '1' ;

Since its meant to be a character, not an integer. However C/C++ confuses the notion
of integer and character together, hence the confusion.

thanks you all for yours explications.
It's more clear.
Bests attention.