Multiple macro string concatenation in C

Source: Internet
Author: User

Before making a concatenation of two strings, the STRCAT function is generally used, even if it is a macro-defined string.

For example, the definition of macro A, B, the first reaction will use A "other" such splicing method, but will not be unresponsive to a B can also do stitching.

The foundation is not solid, good two good two.

The macro string can be spliced like this:

#define a "xxx"
#define B "YYY"

void Main ()
{
    char* str = a b;
    printf ("Str:  %s\n", str);
}

Compiled to run, the output is xxxyyy.

The reason is that C has this syntax: the string of quotes, separated by spaces in the middle, the compiler (preprocessor) will automatically help you into one.

That is, if there are macros A, B, C, D, that char* Str=a b c D; This is also possible.

This principle can also be applied to long string definitions, such as:

#include <stdio.h>

void Main ()
{
    char* sql = "SELECT name"
                "from table1"
                "where xxx=111"; c12/>printf ("sql:  %s\n", SQL);

Compilation runs, the output sql:select name from table1 where xxx=111.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.