char vs string

Hello!

I'm new to arduino and I have a question.

I have read that a char data type have 1byte memory so that we can initialize a char with a single character. Unlike a string is a char array data type with a null character in the end of data, with this way we can initialize a string with several characters. When I declarate a char data type such as, ( char example;) and I try to read some datas from serial with the example opetate for instance example = Serial.read()
and then I try to print this data, why can I print more than one digit. I understand that the result of this is my operate behave such as a string, but I don't understand why

Thank you in advance

Is it because you’re printing the numeric value of the char in the code you didn’t post?

No I print the operate char and I write letters in the serial terminal

and then I try to print this data, why can I print more than one digit

If I get you right, you are asking why do you see a sting although the char can hold only 1 piece of the string ?

–>because you likely print them sequentially in the loop as they come one by one. So on the Serial monitor it looks like you got a string.

void setup() {Serial.begin(115200);}

void loop() {
  if (Serial.available()) {
    char example = Serial.read();
    Serial.print(example); // <<-- print only 1 char but the loop loops, so if you send more data, it prints more
  }
}

if you post some code, we can explain better…

nuh, I feel a little bit dummy,

and if I want to work with string I have to write a code like:

char c, str[10];
int i;

if(Serial.available() > 0)
{
c = Serial.print();
if(c != '\n')
str[i++] = c
}
else
{
str = '\0';
With this way I fill tha array, one by one position for a digit, and if I want to print the array I have to write
Serial.print(str);
right?

Hello harisv, can you please tell me your code with what you are having problems. It would be better if you do and I can tell you a solution.

Hopefully now you can see that when you do eventually post code, you should use code tags.

Much better to terminate the string each time you write a new character to the array.

You should also make sure you do not write off the end of the array.

harisv:
char c, str[10];
int i;

if(Serial.available() > 0)
{
c = Serial.print();
if(c != '\n')
str[i++] = c
}
else
{
str = '\0';
[/quote]
not quite right, but yes
```
*char c, str[10];
int   i  = 0;

if(Serial.available() > 0) {
   c = Serial.read ();
   if(c != '\n')
       str[i++] = c
   else
       str[i] = '\0';
}
[/quote]*

```

ideally you would want to check for overflow.

const uint8_t maxMessageLength = 10;
char message[maxMessageLength + 1]; // + 1 for the trailing null char.
uint8_t index;

void setup() {
  Serial.begin(115200);
  // this is not necessary as global variables are initialized to 0 but for sake of clarity
  index = 0;
  message[0] = '\0'; // this way we know we always have a well formed cString
}

void loop() {
  if (Serial.available()) {
    char c = Serial.read();
    switch (c) {
      case '\r': break; // ignored, not memorized

      case '\n':
        Serial.print(F("Your message was "));
        Serial.println(message);
        index = 0;
        message[0] = '\0'; // this way we know we always have a well formed cString
        break;

      default:
        if (index < maxMessageLength) { // store only if there is space left
          message[index++] = c;
          message[index] = '\0';  // this way we know we always have a well formed cString
        }
    }
  }
}

I would suggest to study Serial Input Basics if you want to explore more on how to deal with Serial input.

harisv:
When I declarate a char data type such as, ( char example;) and I try to read some datas from serial with the example opetate for instance example = Serial.read()
and then I try to print this data, why can I print more than one digit.

You entered A (for example) in the InputBox of the Serial Monitor (Fig-1) and then click on the Send button. After that you have observed 41 to appear in the OutputBox of the Serial Monitor. Is it correct? Now, your query is why your are seeing two digits/characters (41) on the Serial Monitor instead of one character (A). This situation can be reproduced by uploading the following sketch (select 'No line ending' option in the 'Line ending tab' of Serial Monitor):

char example;

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  byte n = Serial.available();
  if(n != 0)
  {
    example  = Serial.read();
    Serial.print(example, HEX);
  }
}


Figure-1:

ok, I want to thank you all, I don't write any specific code but I make some experiments about c programming in arduino and I was a little bit confused, I understand my mistakes because of you, your information are useful for me. Any suggestion or advice is good for me

yours sincerely,
Haris