what (char) should do ?

(char)Serial.read();

What should this execute ? as i searched ic c language and arduino language and dose`t get that

That coerces the return integer value to char type. If that's the complete expression the "(char)" part is useless and should be avoided.

It's called a cast. Serial.read returns an integer and this will cast it to a character.

It's basically redundant but that might slightly depend on the context.

@pylon, why should it be avoided?

@sterretje

what i get that if

Serial.read ()

gets an integer after adding (char) it will be read as char right ?

sterretje: It's called a cast. Serial.read returns an integer and this will cast it to a character.

It's basically redundant but that might slightly depend on the context.

@pylon, why should it be avoided?

It's not at all redundant, depending on the context:

Serial.print(Serial.read());

The above will NOT print a character, it will print an integer, so a space character will print as the decimal number 32.

Serial.print((char)Serial.read());

In the above the cast to char is REQUIRED to print as a char, rather than an int. Regards, Ray L.

@pylon, why should it be avoided?

I wrote, if that line is the complete expression (so the return value is not used), it's useless and should be avoided for clarity reasons.

@RayLivingston
Serial.print() [usually prints string not integer](http://usually prints string not integer)

the data read by Serial.read() is String type
so i don`t get what (char) do

Ma-Mi: the data read by Serial.read() is String type so i don`t get what (char) do

No it isnt!

see the reference

Ma-Mi: @RayLivingston

Serial.print()

usually prints string not integer

the data read by

Serial.read()

is String type so i don`t get what (char) do

Wrong on both points. Serial.read() returns an int. What Serial.print prints depends on the type of the argument passed to it. Pass an int, and it prints the numeric value of that int. Pass a char, and it prints ASCII character corresponding to that char value. Don't believe me? Just try it. Regards, Ray L.

Example Code

int incomingByte = 0; // for incoming serial data

So if you enter a byte size number (128), incomingByte contains 0x0080 ?