1

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.

asked Jan 26, 2017 at 15:05
6
  • 3
    single quotes identify a single character, while double quotes create a string literal Commented Jan 26, 2017 at 15:28
  • 1
    Could you please explain what you want to achieve rather than your likely misguided idea about how to achieve it (i.e. removing quotes)? Commented Jan 26, 2017 at 15:29
  • I am making an extensions for a program that requires Arduino code inside the program, but if I write the quotation marks, the debugger reports an error, because there are quotation marks inside quotation marks. Commented Jan 26, 2017 at 15:35
  • Then you need to update your extension to properly escape the arduino code so that reserved/special characters in the code are not interpreted by the non-arduino debugger. How to do that depends greatly on the program you are extending, which you have not named. Commented Jan 26, 2017 at 15:38
  • 1
    It's a bit unclear to me what you're trying to do, but perhaps just escaping the "s will achieve what you want. In C, the way to escape something is with a ,円 so for example char myString[] ="\"\""; would give you a string containing two "s. Commented Jan 26, 2017 at 15:57

2 Answers 2

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:

  1. Escape the " with \, as in: char foo[] = "He said \"Hello there\".";
  2. 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 ".";
answered Jan 27, 2017 at 12:05
6

No, you cannot. This rules comes from the basic C++ syntax on which the Arduino platform is built.

answered Jan 26, 2017 at 15:21
3
  • Is there a way to define it without the qoutation marks? Commented Jan 26, 2017 at 15:27
  • 1
    Only very convoluted ways. Commented 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. Commented Jan 26, 2017 at 23:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.