Serial.print a character array

The following won't compile (even if I include the size in the 'print' line)
Message "call of overloaded 'print(uint8_t [3])' is ambiguous"

void setup() {
// put your setup code here, to run once:
uint8_t C = "AT";

Serial.begin(9600);
Serial.print(C);
}

How can I print a character array?

Try this:

void setup() {
  // put your setup code here, to run once:
  char C[] = "AT";
 
  Serial.begin(9600);
  Serial.print(C);
}

It works with 'char' but not with 'unsigned char', 'uint8_t' or 'int8_t'.

So, OK, what is the difference between 'char' and 'int8_t'?

[EDIT] It won't compile with either 'signed char' or 'unsigned char'.

AlbertHall:
It works with 'char' but not with 'unsigned char', 'uint8_t' or 'int8_t'.

So, OK, what is the difference between 'char' and 'int8_t'?

[EDIT] It won't compile with either 'signed char' or 'unsigned char'.

What is "it"?

AWOL:
What is “it”?

‘It’ is the code in the first post.

void setup() {
  // put your setup code here, to run once:
  int8_t C[] = "AT";
  
  Serial.begin(9600);
  Serial.print(C);
}

AWOL:

void setup() {

// put your setup code here, to run once:
  int8_t C = "AT";
 
  Serial.begin(9600);
  Serial.print(C);
}

This doesn't compile.

AlbertHall:
This doesn't compile.

There is no "loop()"

void setup() {
  // put your setup code here, to run once:
  int8_t C[] = "AT";
 
  Serial.begin(9600);
  Serial.print((char*)C);
}

void loop () {}

Serial.write(C,strlen(C));

[Restart:]

The program below will only compile if the variable is declared 'char'. None of the other variants shown in the code will compile without error. Both .print and .write behave the same.

What is special about 'char', especially as both 'unsigned char' and 'signed char' produce the error?

Also how do I put code in the nice scrolling box?

void setup() {

//uint8_t C = "AT"; // Won't compile
//int8_t C = "AT"; // Won't compile
//unsigned char C = "AT"; // Won't compile
//signed char C = "AT"; // Won't compile
char C = "AT"; // This is the only version which will compile

Serial.begin(9600);
Serial.write(C,2); // Both .write and .print behave the same
Serial.print(C);
}

void loop() {
}

[EDIT] Because I know someone will ask, the error is:
"call of overloaded 'print(unsigned char [3])' is ambiguous"

Also how do I put code in the nice scrolling box?

Select the code in the forum editor and click the Code icon (</>) top/left of the editor window and code tags will be added before and after the code.

Thanks

AlbertHall:
[Restart:]
The program below will only compile if the variable is declared 'char'. None of the other variants shown in the code will compile without error. Both .print and .write behave the same.

I imagine it's because the prototype for the overloaded function in question (from Print class in Print.h) is:

size_t print(const char[]);

There is no corresponding overload prototype for uint8_t, unsigned char, etc.

try typecasting it to a char

 Serial.print((char)C);

z

1 Like

zhomeslice:
try typecasting it to a char

 Serial.print((char)C);

z

Cast an array to a single char?

Perhaps a (char *).

gfvalvo:
Perhaps a (char *).

....and back to reply #7

Didn’t see that. Maybe OP didn’t either. Else we wouldn’t be on Reply #17.