This is kind of a short question. All I want to know is, if I can define a string using single quotation marks ('), instead of the regular ones("). I want to use it in a function like this: print('Hello world');
I unfortunately do not have an Arduino nearby so I cannot test it myself.
2 Answers 2
No, you can't. 'x'
defines a single character or integer (note: 'ab'
is a two-byte single value [integer] made up from the ASCII values of both characters).
To include "
within a string you have two options:
- Escape the
"
with\
, as in:char foo[] = "He said \"Hello there\".";
- Use "raw strings" if the compiler is configured to support it:
char foo[] = R"(He said "Hello there".)";
Option 1 is the most portable since it doesn't require compiler support for modern C++ standards. However it's the hardest to read, especially when you get lots of escapes in the same string.
You can make it more readable by defining QUOTE:
#define QUOTE "\""
char foo[] = "He said " QUOTE "Hello there" QUOTE ".";
No, you cannot. This rules comes from the basic C++ syntax on which the Arduino platform is built.
-
Is there a way to define it without the qoutation marks?user29519– user295192017年01月26日 15:27:42 +00:00Commented Jan 26, 2017 at 15:27
-
1Only very convoluted ways.Edgar Bonet– Edgar Bonet2017年01月26日 15:28:37 +00:00Commented Jan 26, 2017 at 15:28
-
Actually, C++ raw string literals are easy to use (ref 1, 2); the only convoluted bit is adding a
-std=c++11
or-std=c++0x
switch to IDE compiler options.James Waldby - jwpat7– James Waldby - jwpat72017年01月26日 23:41:50 +00:00Commented Jan 26, 2017 at 23:41
"
s will achieve what you want. In C, the way to escape something is with a ,円 so for examplechar myString[] ="\"\"";
would give you a string containing two"
s.