How to split a string literal across multiple lines in C / Objective-C?

I have a pretty long sqlite query:

const char *sql_query = "SELECT statuses.word_id FROM lang1_words, statuses WHERE statuses.word_id = lang1_words.word_id ORDER BY lang1_words.word ASC";

How can I break it in a number of lines to make it easier to read?
If I do the following:

  • Tint UIButton's image from interface builder with UIImageRenderingModeAlwaysTemplate
  • Live555 framework generation error
  • Using multiple storyboards with a TabBarController
  • iOS - detect when user copy to clipboard -
  • Convert Seconds Integer To HH:MM, iPhone
  • Core Data memory usage and Memory Warning
  • const char *sql_query = "SELECT word_id
                            FROM table1, table2
                            WHERE table2.word_id = table1.word_id
                            ORDER BY table1.word ASC";
    

    I am getting a error.

    Is there a way to write queries in multiple lines?

    7 Solutions Collect From Internet About “How to split a string literal across multiple lines in C / Objective-C?”

    There are two ways to split strings over multiple lines:

    Using \

    All lines in C can be split into multiple lines using \.

    Plain C:

    char *my_string = "Line 1 \
                       Line 2";
    

    Objective-C:

    NSString *my_string = @"Line1 \
                            Line2";
    

    Better approach

    There’s a better approach that works just for strings.

    Plain C:

    char *my_string = "Line 1 "
                      "Line 2";
    

    Objective-C:

    NSString *my_string = @"Line1 "
                           "Line2";    // the second @ is optional
    

    The second approach is better, because there isn’t a lot of whitespace included. For a SQL query however, both are possible.

    NOTE: With a #define, you have to add an extra ‘\’ to concatenate the two strings:

    Plain C:

    #define kMyString "Line 1"\
                      "Line 2"
    

    There’s a trick you can do with the pre-processor.
    It has the potential down sides that it will collapse white-space, and could be confusing for people reading the code.
    But, it has the up side that you don’t need to escape quote characters inside it.

    #define QUOTE(...) #__VA_ARGS__
    const char *sql_query = QUOTE(
        SELECT word_id
        FROM table1, table2
        WHERE table2.word_id = table1.word_id
        ORDER BY table1.word ASC
    );
    

    the preprocessor turns this into:

    const char *sql_query = "SELECT word_id FROM table1, table2 WHERE table2.word_id = table1.word_id ORDER BY table1.word ASC";
    

    I’ve used this trick when I was writing some unit tests that had large literal strings containing JSON. It meant that I didn’t have to escape every quote character \”.

    You could also go into XCode -> Preferences, select the Indentation tab, and turn on Line Wrapping.

    That way, you won’t have to type anything extra, and it will work for the stuff you already wrote. 🙂

    One annoying thing though is…

    if (you're long on indentation
        && short on windows) {
                then your code will
                    end up squished
                         against th
                             e side
                                 li
                                  k
                                  e
    
                                  t
                                  h
                                  i
                                  s
    }
    

    I am having this problem all the time, so I made a tiny tool to convert text to an escaped multi-line Objective-C string:

    http://multilineobjc.herokuapp.com/

    Hope this saves you some time.

    Extending the Quote idea for Objective-C:

    #define NSStringMultiline(...) [[NSString alloc] initWithCString:#__VA_ARGS__ encoding:NSUTF8StringEncoding]
    
    NSString *sql = NSStringMultiline(
        SELECT name, age
        FROM users
        WHERE loggedin = true
    );
    

    You can also do:

    NSString * query = @"SELECT * FROM foo "
                       @"WHERE "
                         @"bar = 42 "
                         @"AND baz = datetime() "
                       @"ORDER BY fizbit ASC";
    

    One more solution for the pile, change your .m file to .mm so that it becomes Objective-C++ and use C++ raw literals, like this:

    const char *sql_query = R"(SELECT word_id
                        FROM table1, table2
                        WHERE table2.word_id = table1.word_id
                        ORDER BY table1.word ASC)";
    

    Raw literals ignore everything until the termination sequence, which in the default case is parenthesis-quote. If the parenthesis-quote sequence has to appear in the string somewhere, you can easily specify custom terminators too, look up C++ raw literals to find details on doing that (it’s easy).