Skip to content

Fix Serial.print((char)value) printing a number instead of a character - #114

Merged
praeclarum merged 2 commits into
masterfrom
copilot/fix-char-overload-resolution
Mar 31, 2026
Merged

Fix Serial.print((char)value) printing a number instead of a character#114
praeclarum merged 2 commits into
masterfrom
copilot/fix-char-overload-resolution

Conversation

Copilot AI commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

Serial.print((char)value) dispatched to the int overload (or fell through to const char*) because the char and int overloads for Serial.print were disabled, and CIntType.ScoreCastTo didn't differentiate same-name types from cross-type matches.

Changes

  • Enable Serial.print char/int overloads — Uncommented print(char) and print(int) in SerialClass header and registered their internal function handlers (SerialPrintC, SerialPrintI)
  • Improve CIntType.ScoreCastTo precision — Added score tier 950 for same-name/same-size but different-signedness matches (e.g. signed charunsigned char), separating them from cross-type matches like charint (900)
// Before: char→unsigned char and char→int both scored 900
if (Size == it.Size) return 900;

// After: char→unsigned char scores 950, char→int scores 900
if (Name == it.Name && Size == it.Size) return 950;
if (Size == it.Size) return 900;
  • Tests — Added SerialPrintCharCast, SerialPrintlnCharCast, GlobalCharVsIntCast, MemberCharVsIntCast verifying correct dispatch when casting int variables to char
int value = 65;
Serial.print((char)value);  // now prints "A", not "65"

Copilot AI linked an issue Mar 31, 2026 that may be closed by this pull request
- Enable Serial.print char and int overloads (previously commented out)
- Add SerialPrintC and SerialPrintI implementations in test machine info
- Add SerialPrintCharCast and SerialPrintlnCharCast Arduino tests
- Add GlobalCharVsIntCast and MemberCharVsIntCast overload tests
- Improve CIntType.ScoreCastTo to better differentiate same-name types

Agent-Logs-Url: https://github.com/praeclarum/CLanguage/sessions/99d0761f-0418-459c-b0ac-a85ab523c0f0

Co-authored-by: praeclarum <323548+praeclarum@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix overload resolution for char print in Serial Fix Serial.print((char)value) printing a number instead of a character Mar 31, 2026
Copilot AI requested a review from praeclarum March 31, 2026 20:51
@praeclarum
praeclarum marked this pull request as ready for review March 31, 2026 20:58
@praeclarum
praeclarum merged commit 39d170c into master Mar 31, 2026
1 check passed
@praeclarum
praeclarum deleted the copilot/fix-char-overload-resolution branch March 31, 2026 21:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Serial.print((char)value) prints a number

2 participants