About the use of connectors in C language # # # macro Stringize

Source: Internet
Author: User

First, demand-oriented, small projects, code is not many, version is quite a lot ...

You plan to use a macro to control the switch options, and then pass a long string

#if

#elif

...

#endif

To control the build version number, but also according to compatibility and minor changes to do the level three version number ....

So we need a version number splicing, obviously, it should be the compile time can be done.

--------Demand is complete--------

The C-language macro definition should be complete and even more problematic during code execution.

Although string.h can also be used to complete stitching.

Uncomfortable, Skinner.


Preliminary find using # and # #, can be completed, further find out what a #@ is ... Some compilers do not support

After a preliminary trial # will directly transfer the macro directly into the string,#@ in theory is to achieve the function I need, but does not support

---------------Magical Split-line-------------------------

further search stringize site:gcc.gnu.org

Find page

3.4 stringification

Https://gcc.gnu.org/onlinedocs/cpp/Stringification.html

In view of the blog, it is not affixed to the original, but also need to see their own.


Conclusion:


If you want to stringify the result of expansion of a macro argument, you had to use both levels of macros.


#define XSTR (s) str (s)     #define STR (s) #s     #define FOO 4     str (foo)          ==> "foo"     xstr (foo)          ==> XSTR (4)          ==> str (4)          ==> "4"




The combination # # infinite possibility comes out


#define XSTR (s) str (s)      //copy from Https://gcc.gnu.org/onlinedocs/cpp/Stringification.html#define str (s) #s # Define _STRING_ (_x_)           xstr (_x_)    //convert from macro to STRING #define Bsp_hard_reversion 1.0.0#define      sw_ First_version        8#define sw_mid_version          800#define sw_last_version         021#define sw_full_vsersion        SW_ first_version##.# #SW_MID_VERSION ##.# #SW_LAST_VERSION # define hw_version_string       _string_ (bsp_hard_reversion) # Define sw_version_string       _string_ (sw_full_vsersion) static const char hw_rev[] = hw_version_string;      Hw_rec Config by boards.hstatic const char fw_rev[] = "8.0.0"; static const char sw_rev[] = sw_version_string; #undef str# undef xstr#undef _string_



OK to here I need the function has been done. Everybody just take a debugging tool to see it, it's OK.


Careful study of the implementation of the principle, feel their own understanding of the definition of the macro into a level ... It's not that complicated, actually.

S was stringified when it was used in Str, so it was not macro-expanded first. But s are an ordinary argument to Xstr, so it's completely macro-expanded before Xstr itself is Expanded ( see Argument prescan). Therefore, by the time str gets to its argument, it has already been macro-expanded.

Original address

http://my.oschina.net/mummy108/blog/510439


About the use of connectors in C language # # # macro Stringize

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.