“char inChar = (char)Serial.read();” why is the “char” in brackets? and what is it called

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I am sure this is a very simple answer, but I don't know to look it up on google



In the example "SerialEvent" in the arduino IDE (v1.8.7), there is the following function



 void serialEvent() 
while (Serial.available())
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == 'n')
stringComplete = true;





On the third line "char inChar = (char)Serial.read();" the (char) is given in brackets before the serial read.
Why is this? and more specifically how is called ?










share|improve this question

















  • 5




    This is actually more a C/C++ language question. The hint is "Please check the return value type for Serial.read()".
    – Mikael Patel
    Sep 24 at 21:04










  • So it basically forces the value returned from serial.read() to be a char? Do you know how this is named, so I can look up the documentation?
    – will.mendil
    Sep 24 at 21:17











  • In C, why are there parentheses around (int) in this example?, Type in parentheses in C variable definition
    – phuclv
    Sep 25 at 1:58










  • What topics can I ask about here? On topic: Specific questions about Arduino boards, code… arduino.stackexchange.com/help/on-topic. Is this question not a specific question about Arduino code? Voting to not close.
    – VE7JRO
    Sep 25 at 5:33










  • @will.mendil This is called "Explicit type conversion" or "Casting". en.cppreference.com/w/cpp/language/explicit_cast. In some cases it ends up as some extra code but in other cases it is just a hint to the compiler and no extra code is generated.
    – Mikael Patel
    Sep 25 at 10:34














up vote
2
down vote

favorite












I am sure this is a very simple answer, but I don't know to look it up on google



In the example "SerialEvent" in the arduino IDE (v1.8.7), there is the following function



 void serialEvent() 
while (Serial.available())
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == 'n')
stringComplete = true;





On the third line "char inChar = (char)Serial.read();" the (char) is given in brackets before the serial read.
Why is this? and more specifically how is called ?










share|improve this question

















  • 5




    This is actually more a C/C++ language question. The hint is "Please check the return value type for Serial.read()".
    – Mikael Patel
    Sep 24 at 21:04










  • So it basically forces the value returned from serial.read() to be a char? Do you know how this is named, so I can look up the documentation?
    – will.mendil
    Sep 24 at 21:17











  • In C, why are there parentheses around (int) in this example?, Type in parentheses in C variable definition
    – phuclv
    Sep 25 at 1:58










  • What topics can I ask about here? On topic: Specific questions about Arduino boards, code… arduino.stackexchange.com/help/on-topic. Is this question not a specific question about Arduino code? Voting to not close.
    – VE7JRO
    Sep 25 at 5:33










  • @will.mendil This is called "Explicit type conversion" or "Casting". en.cppreference.com/w/cpp/language/explicit_cast. In some cases it ends up as some extra code but in other cases it is just a hint to the compiler and no extra code is generated.
    – Mikael Patel
    Sep 25 at 10:34












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I am sure this is a very simple answer, but I don't know to look it up on google



In the example "SerialEvent" in the arduino IDE (v1.8.7), there is the following function



 void serialEvent() 
while (Serial.available())
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == 'n')
stringComplete = true;





On the third line "char inChar = (char)Serial.read();" the (char) is given in brackets before the serial read.
Why is this? and more specifically how is called ?










share|improve this question













I am sure this is a very simple answer, but I don't know to look it up on google



In the example "SerialEvent" in the arduino IDE (v1.8.7), there is the following function



 void serialEvent() 
while (Serial.available())
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == 'n')
stringComplete = true;





On the third line "char inChar = (char)Serial.read();" the (char) is given in brackets before the serial read.
Why is this? and more specifically how is called ?







serial






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 24 at 20:56









will.mendil

132




132







  • 5




    This is actually more a C/C++ language question. The hint is "Please check the return value type for Serial.read()".
    – Mikael Patel
    Sep 24 at 21:04










  • So it basically forces the value returned from serial.read() to be a char? Do you know how this is named, so I can look up the documentation?
    – will.mendil
    Sep 24 at 21:17











  • In C, why are there parentheses around (int) in this example?, Type in parentheses in C variable definition
    – phuclv
    Sep 25 at 1:58










  • What topics can I ask about here? On topic: Specific questions about Arduino boards, code… arduino.stackexchange.com/help/on-topic. Is this question not a specific question about Arduino code? Voting to not close.
    – VE7JRO
    Sep 25 at 5:33










  • @will.mendil This is called "Explicit type conversion" or "Casting". en.cppreference.com/w/cpp/language/explicit_cast. In some cases it ends up as some extra code but in other cases it is just a hint to the compiler and no extra code is generated.
    – Mikael Patel
    Sep 25 at 10:34












  • 5




    This is actually more a C/C++ language question. The hint is "Please check the return value type for Serial.read()".
    – Mikael Patel
    Sep 24 at 21:04










  • So it basically forces the value returned from serial.read() to be a char? Do you know how this is named, so I can look up the documentation?
    – will.mendil
    Sep 24 at 21:17











  • In C, why are there parentheses around (int) in this example?, Type in parentheses in C variable definition
    – phuclv
    Sep 25 at 1:58










  • What topics can I ask about here? On topic: Specific questions about Arduino boards, code… arduino.stackexchange.com/help/on-topic. Is this question not a specific question about Arduino code? Voting to not close.
    – VE7JRO
    Sep 25 at 5:33










  • @will.mendil This is called "Explicit type conversion" or "Casting". en.cppreference.com/w/cpp/language/explicit_cast. In some cases it ends up as some extra code but in other cases it is just a hint to the compiler and no extra code is generated.
    – Mikael Patel
    Sep 25 at 10:34







5




5




This is actually more a C/C++ language question. The hint is "Please check the return value type for Serial.read()".
– Mikael Patel
Sep 24 at 21:04




This is actually more a C/C++ language question. The hint is "Please check the return value type for Serial.read()".
– Mikael Patel
Sep 24 at 21:04












So it basically forces the value returned from serial.read() to be a char? Do you know how this is named, so I can look up the documentation?
– will.mendil
Sep 24 at 21:17





So it basically forces the value returned from serial.read() to be a char? Do you know how this is named, so I can look up the documentation?
– will.mendil
Sep 24 at 21:17













In C, why are there parentheses around (int) in this example?, Type in parentheses in C variable definition
– phuclv
Sep 25 at 1:58




In C, why are there parentheses around (int) in this example?, Type in parentheses in C variable definition
– phuclv
Sep 25 at 1:58












What topics can I ask about here? On topic: Specific questions about Arduino boards, code… arduino.stackexchange.com/help/on-topic. Is this question not a specific question about Arduino code? Voting to not close.
– VE7JRO
Sep 25 at 5:33




What topics can I ask about here? On topic: Specific questions about Arduino boards, code… arduino.stackexchange.com/help/on-topic. Is this question not a specific question about Arduino code? Voting to not close.
– VE7JRO
Sep 25 at 5:33












@will.mendil This is called "Explicit type conversion" or "Casting". en.cppreference.com/w/cpp/language/explicit_cast. In some cases it ends up as some extra code but in other cases it is just a hint to the compiler and no extra code is generated.
– Mikael Patel
Sep 25 at 10:34




@will.mendil This is called "Explicit type conversion" or "Casting". en.cppreference.com/w/cpp/language/explicit_cast. In some cases it ends up as some extra code but in other cases it is just a hint to the compiler and no extra code is generated.
– Mikael Patel
Sep 25 at 10:34










1 Answer
1






active

oldest

votes

















up vote
5
down vote



accepted










It is called type casting and will convert one type to another. The return type of Serial.read() is int, because it will return -1, when you try to read without any bytes being available. The casting with (char) will drop the high byte of the (2-byte long) integer, leaving only the lower byte. This byte will contain the actual read data, in which you are interested here.



Casting is a complex thing, because there can be different ways to convert different types (for example maintaining the bit-content vs. maintaining the decimal value). The casting, that you are seeing here, should only be done, if you really know, what you are doing. The bits will not be changed to maintain the correct value, but the bit-content will be preserved, unless the new type is smaller than the former. In this case the rest is lost. Also, if you try to cast a float value to an int value with this, you will get a totally different decimal value out of it, because the numbers are encoded in the bits in a totally different way. Here it is known to be OK, especially, if you are checking for available bytes before reading with Serial.available(), because the library puts the ASCII data in the lower byte.



For further explanations you should consult C/C++ guides.






share|improve this answer
















  • 1




    Thank you ever so much !
    – will.mendil
    Sep 24 at 21:40










Your Answer





StackExchange.ifUsing("editor", function ()
return StackExchange.using("schematics", function ()
StackExchange.schematics.init();
);
, "cicuitlab");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "540"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f56359%2fchar-inchar-charserial-read-why-is-the-char-in-brackets-and-what-is%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
5
down vote



accepted










It is called type casting and will convert one type to another. The return type of Serial.read() is int, because it will return -1, when you try to read without any bytes being available. The casting with (char) will drop the high byte of the (2-byte long) integer, leaving only the lower byte. This byte will contain the actual read data, in which you are interested here.



Casting is a complex thing, because there can be different ways to convert different types (for example maintaining the bit-content vs. maintaining the decimal value). The casting, that you are seeing here, should only be done, if you really know, what you are doing. The bits will not be changed to maintain the correct value, but the bit-content will be preserved, unless the new type is smaller than the former. In this case the rest is lost. Also, if you try to cast a float value to an int value with this, you will get a totally different decimal value out of it, because the numbers are encoded in the bits in a totally different way. Here it is known to be OK, especially, if you are checking for available bytes before reading with Serial.available(), because the library puts the ASCII data in the lower byte.



For further explanations you should consult C/C++ guides.






share|improve this answer
















  • 1




    Thank you ever so much !
    – will.mendil
    Sep 24 at 21:40














up vote
5
down vote



accepted










It is called type casting and will convert one type to another. The return type of Serial.read() is int, because it will return -1, when you try to read without any bytes being available. The casting with (char) will drop the high byte of the (2-byte long) integer, leaving only the lower byte. This byte will contain the actual read data, in which you are interested here.



Casting is a complex thing, because there can be different ways to convert different types (for example maintaining the bit-content vs. maintaining the decimal value). The casting, that you are seeing here, should only be done, if you really know, what you are doing. The bits will not be changed to maintain the correct value, but the bit-content will be preserved, unless the new type is smaller than the former. In this case the rest is lost. Also, if you try to cast a float value to an int value with this, you will get a totally different decimal value out of it, because the numbers are encoded in the bits in a totally different way. Here it is known to be OK, especially, if you are checking for available bytes before reading with Serial.available(), because the library puts the ASCII data in the lower byte.



For further explanations you should consult C/C++ guides.






share|improve this answer
















  • 1




    Thank you ever so much !
    – will.mendil
    Sep 24 at 21:40












up vote
5
down vote



accepted







up vote
5
down vote



accepted






It is called type casting and will convert one type to another. The return type of Serial.read() is int, because it will return -1, when you try to read without any bytes being available. The casting with (char) will drop the high byte of the (2-byte long) integer, leaving only the lower byte. This byte will contain the actual read data, in which you are interested here.



Casting is a complex thing, because there can be different ways to convert different types (for example maintaining the bit-content vs. maintaining the decimal value). The casting, that you are seeing here, should only be done, if you really know, what you are doing. The bits will not be changed to maintain the correct value, but the bit-content will be preserved, unless the new type is smaller than the former. In this case the rest is lost. Also, if you try to cast a float value to an int value with this, you will get a totally different decimal value out of it, because the numbers are encoded in the bits in a totally different way. Here it is known to be OK, especially, if you are checking for available bytes before reading with Serial.available(), because the library puts the ASCII data in the lower byte.



For further explanations you should consult C/C++ guides.






share|improve this answer












It is called type casting and will convert one type to another. The return type of Serial.read() is int, because it will return -1, when you try to read without any bytes being available. The casting with (char) will drop the high byte of the (2-byte long) integer, leaving only the lower byte. This byte will contain the actual read data, in which you are interested here.



Casting is a complex thing, because there can be different ways to convert different types (for example maintaining the bit-content vs. maintaining the decimal value). The casting, that you are seeing here, should only be done, if you really know, what you are doing. The bits will not be changed to maintain the correct value, but the bit-content will be preserved, unless the new type is smaller than the former. In this case the rest is lost. Also, if you try to cast a float value to an int value with this, you will get a totally different decimal value out of it, because the numbers are encoded in the bits in a totally different way. Here it is known to be OK, especially, if you are checking for available bytes before reading with Serial.available(), because the library puts the ASCII data in the lower byte.



For further explanations you should consult C/C++ guides.







share|improve this answer












share|improve this answer



share|improve this answer










answered Sep 24 at 21:32









chrisl

2,1212311




2,1212311







  • 1




    Thank you ever so much !
    – will.mendil
    Sep 24 at 21:40












  • 1




    Thank you ever so much !
    – will.mendil
    Sep 24 at 21:40







1




1




Thank you ever so much !
– will.mendil
Sep 24 at 21:40




Thank you ever so much !
– will.mendil
Sep 24 at 21:40

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f56359%2fchar-inchar-charserial-read-why-is-the-char-in-brackets-and-what-is%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Peggy Mitchell

The Forum (Inglewood, California)

Palaiologos