# Use of macro-defined printf

Source: Internet
Author: User

# Macro string connection. The macro parameter is interpreted as a string and cannot be directly used in statements. In macro definition

Printf ("% s;/n", # S) will be interpreted

Printf ("% s;/n", "S ")

For example, the following code


# Define TRACE (S) (printf ("% s \ n", # S), S)/* use commas instead of semicolons */
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
Int main ()
{
Int a = 5;
Int B = TRACE ();
Const char * str = "hello ";
Char des [50];
Strcpy (des, TRACE (str ));
Puts (des );
System ("pause ");
Return 0;
}

 

The macro definition is a comma expression, so the value copied to des is the value of S, that is, str.

Note:

Int a = printf ("% s; \ n", "S"), a is equal to the return value of printf, that is, the number of characters printed by printf.

From he Hao column programmer interview 500

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.