Monday, 15 April 2013

initialize a C string with multiple quoted strings -


I thought that a single string can be started with one and only one quoted string. I just wonder how this is right?

  four const help_message [] = "Usage:% s [Option] Files ... \ n" "\ n" "Options include: \ n" "--Babos -Yy Become a verbose \ n "" --help -h Print this help message \ n "" - Specify the output -o output file \ n "" \ n "; Printf (help_message, argv [0]);  

The adjacent string literal is added, and it is useful in two ways: Macros Which is like the wire and visualization of multi-line string literals , compare why this code will look otherwise:

  Four constellations Help_meas [] = "Usage:% s [Options] Files ... \ n \ nAdditional options include: \ n --Webows -VB verbose \ n --help -h print this help message \ n - Output -o Specify output file \ n \ n ";  

Instead try to maintain it. In addition, if you use a multi-line string, you must either save it from Newlines or deal with the source code that it uses, which will be '\ n' It can not be, and you should carefully consider your indentation, which makes all the code better in your example

Here is an example of a macro case:.

  #define STRINGIZE_ (v) #V #define STRINGIZE (v) STRINGIZE_ (v) #define location __FILE__ ":" STRINGIZE (__ LINE__) #define MY_ASSERT (expr) {\ If \ Some_function (expr)!) (Location ": claim failed" \ __PRETTY_FUNCTION__ ":" #expr); \} (0)  

(There are alternatives for this to provide different parameters, and using GCC special __ PRETTY_FUNCTION __ It's like this, but everything else is easy and this is a decent "real" example, IMHO.)

Other issues have been brought into the code that should be known about:

  • Single # Preprocessor Stretch Operator ( ## Other special preprocessor to paste tokens
  • Without the second stratch macro, you do not break when using "filename.c: __ line __"
  • and the macro Need to be used as a statement instead of expression
    • Preventing use as an expression is not always helpful, but this is what you need to do, such as macros

Breaking if-else example:

  if (cond) MY_ASSERT (blah); other ();  

Its extension:

  if (cond) do {...} while (0); other (); Instead of  

:

  if (cond) if (...) ...; other ();  

Which is incorrect and surprising:

  if (cond) {if (...) {...; } and others (); }}  

No comments:

Post a Comment