How does the Serial monitor display '12594' when we write Serial.print('12');?I have used single quotes intentionally.
[code]
void setup()
{
Serial.begin(9600);
Serial.print('12');
}
void loop()
{
}
[/code]
How does the Serial monitor display '12594' when we write Serial.print('12');?I have used single quotes intentionally.
[code]
void setup()
{
Serial.begin(9600);
Serial.print('12');
}
void loop()
{
}
[/code]
Why are you using single quotes?
If we write Serial.print('6'); then its ASCII value get printed.I wanted to see what happens when we write Serial.print('12');.........You may even try Serial.print('10'); or Serial.print('11');
The values in that case comes out t o be '12592' and '12593' respectively.So I want to know what is actually happening
Single quotes are typically used to declare character constants (that means only on character is permitted within the quotes).
The compiler should give you warning if you try to include more than one character.
sketch_dec17a.ino:4:14: warning: multi-character character constant [-Wmultichar]
In practice, what you are seeing is down to either of the following:
The value of 'ab' is likely to be either 'a' * 256 + 'b'
or 'b' * 256 + 'a'. It will vary from one compiler to another.
In his case (where ASCII 1=49 and ASCII 2=50 you are likely seeing:
(49*256)+50 = 12594
when you output '12' your outputting 2 characters, a '1' and then a '2', this results in hex 31 or dec 49 being transmitted followed by a 32hex / 50dec, if you concatinate the two into a single integer then you get 12594, sending the 10 works out to be 12592, 11=12593
the print statement is interpreting this as an integer (16bits) instead of two 8 bit characters because in C the character literal is defined as an int and your providing just the right number of 8 bit characters to make an int
if you want to send the string literal "12" then you need the double quotes, this will also terminate the string correctly in the code so the actual memory used in this case would be 3 bytes '1','2','/0'
hope that helps
according to C99 this is implementation specefic so for the Arduino compiler it apears to be interpreting the character literal as an int which is perfectly valid
go figure
lol, were on it at the same time