2

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

Sometime I have to write several SQL statements pointed by char variable like:

const char* sql="CREATE TABLE GenInf ( EmpName TEXT NOT NULL, Pyrll TEXT PRIMARY KEY,  DtBrth TEXT,  PlBrth TEXT, CID TEXT,  ContNo  TEXT,   JnDt TEXT,   Postn TEXT, Dept TEXT, AnnLv TEXT, EmrgLv TEXT, Irr TEXT, HmTwn TEXT, F1 TEXT, F2 TEXT ); CREATE TABLE Training( Pyrll TEXT NOT NULL, Crs TEXT, Dt TEXT, Plc TEXT, Cst TEXT);"; 

But it is very long, How can I put it as two lines?

Community
  • 1
  • 1
Aan
  • 12,247
  • 36
  • 89
  • 150

3 Answers3

7

String literals in C and C++ can be put side to side, so this is exactly the same as your code:

const char* sql="CREATE TABLE "
    "GenInf ( EmpName TEXT NOT NULL, Pyrll TEXT PRIMARY KEY,  "
    "DtBrth TEXT,  PlBrth TEXT, CID TEXT,  ContNo  TEXT,   "
    "JnDt TEXT,   Postn TEXT, Dept TEXT, AnnLv TEXT, "
    "EmrgLv TEXT, Irr TEXT, HmTwn TEXT, F1 TEXT, F2 TEXT ); "
    "CREATE TABLE Training( Pyrll TEXT NOT NULL, Crs TEXT, "
    "Dt TEXT, Plc TEXT, Cst TEXT);";

I prefer this to the \ solution because the whitespaces at the line breaks are more easily visible, and you can indent at will.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
2

You can write it like this:

const char * sql =
    "CREATE TABLE GenInf ( EmpName TEXT NOT NULL, Pyrll TEXT PRIMARY KEY,"
    "DtBrth TEXT,  PlBrth TEXT, CID TEXT,  ContNo  TEXT,   JnDt TEXT,   " 
    "Postn TEXT, Dept TEXT, AnnLv TEXT, EmrgLv TEXT, Irr TEXT, HmTwn TEXT, "
    "F1 TEXT, F2 TEXT ); "
    "CREATE TABLE Training( Pyrll TEXT NOT NULL, Crs TEXT, Dt TEXT, Plc "
    "TEXT, Cst TEXT);";
Vikdor
  • 23,934
  • 10
  • 61
  • 84
1

There is 2 way:

1) Just write multiple string and C++ link them together as one string:

char* c =
    "1234567890"
    "ABCDEFGHIJK";
assert( strcmp(c, "1234567890ABCDEFGHIJK") == 0 );

2) terminate each line with a \ that followed by a \n

char* c = "0123456789\
ABCDEFGHIJK";
assert( strcmp(c, "1234567890ABCDEFGHIJK") == 0 );

Note that in second case spaces before second line will be merge in the string!

CL.
  • 173,858
  • 17
  • 217
  • 259
BigBoss
  • 6,904
  • 2
  • 23
  • 38