difference between double quote and single quote?

Hi,

I have a project with the line

receivedChars[0] = "\0";

I have never had an issue with the use of double quotes in this way (Windows PC), however, I get reports that some people have compilation errors due to the use of the double quotes and they have to change them to single quotes, not many people but enough for it to be an issue.

Why would some people would have an issue and others not?

What is the type of "receivedChars"?

TheMemberFormerlyKnownAsAWOL:
What is the type of "receivedChars"?

it's a char array.

You should be using single quotes there. Double quotes gives a null-terminated char array, while single quotes gives a single char. You are comparing a single char from a char array, so you need to use single quotes. I'm surprised that it ever works with double quotes!

Demo code

char receivedChars[32];

void setup()
{
  receivedChars[0] = "\0";
}

void loop()
{
}

Compiler output

C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_723509\sketch_oct12a.ino: In function 'void setup()':

C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_723509\sketch_oct12a.ino:5:20: warning: invalid conversion from 'const char*' to 'char' [-fpermissive]

   receivedChars[0] = "\0";

To get this output, set file -> preferences -> compiler warnings to 'ALL'.

The reason why on some system it compiles and on other systems it does not is due to the -fpermissive flag that is passed to the compiler; from man avr-gcc

-fpermissive
Downgrade some diagnostics about nonconformant code from errors to warnings. Thus, using -fpermissive will allow some nonconforming code to compile.

There might be other flags that have an influence; for you to read up on all possible flags that can affect the compile result :wink:

@sterretje

Thank you.

I use the portable version of the IDE and it looks like the default setting for compiler warnings is None. I have various IDEs set up for different things and all say None. Have now updated the setting and I now get the warning.

DrAzzy:
You should be using single quotes there. Double quotes gives a null-terminated char array, while single quotes gives a single char. You are comparing a single char from a char array, so you need to use single quotes. I'm surprised that it ever works with double quotes!

DrAzzy,
I have come to regard you as one of the people on here who really knows what he's talking about, so, either I have misunderstood that or, I think, it's wrong. If it's right then I need more explanation please. Do you mean that:

char test[2] = 'I'
Will result in test[0] containing 0x49 and test[1] being unspecified

char test[2] = "I"
Will result in test[0] containing 0x49 and test[1] containing 0x00 ?

Thank you.

@PerryBebbington
The first one does not compile

sketch_oct13a:1: error: array must be initialized with a brace-enclosed initializer

 char test[2] = 'I';

                ^

The second one indeed results in what you say; you can easily test by printing the elements of the array.

I'm trying to test it but I am bumping into things that I don't know how to deal with. For example:

char test[2] = {'I'};
char test2[2] = {"I"};

uint8_t toprint;

void setup() {
  Serial.begin(9600);
  
  toprint = test[0];
  Serial.print(toprint, HEX);
  toprint = test[1];
  Serial.println(toprint, HEX);

  toprint = test2[0];
  Serial.print(toprint, HEX);
  toprint = test2[1];
  Serial.println(toprint, HEX);  
  
  Serial.print("Finished");
}

Prints

490
490
Finished

So both 'I' and "I" appear the same, but the two arrays would both have 0x00 as their second element anyway, regardless of whether a null terminator was placed there or not. I can't see how to initialise the array with something other than 0x00 in the second element, then add 'I' or "I".

To my knowledge

You can't initialize your test variable with anything else. There is an initialisation done before the call of main() in any C/C++ program; this clears all the memory with 0x00. You can read up on crt0.

So when you set the first element of test, the second element will still be 0x00.

// Edit
PS
You can try to make test a local variable in loop() in a more complicated program; local variables are not initialised when you declare them and might contain old values from other functions/ISRs.

You can try to make test a local variable in loop() in a more complicated program; local variables are not initialised when you declare them and might contain old values from other functions/ISRs.

Indeed. The problem being 'might' or, of course, 'might not'. I remember having problems with this when I first learnt C, settled on using ' rather than " then didn't think about it much until I saw this discussion and wondered if I'd learn something new.