C Language Learning--macro definition

Source: Internet
Author: User

1. Simple macro definition

#define < macro name >  < string >
Example: #define LEN 0.5

2. Macro definition with parameters

#define < macro name >  (< parameter table >) < macro body >
Example: #define FUN (para) para*2

3. Example Analysis

1 #define VALUE2void  main ()3{4      int n=value*  VALUE; 5      printf ("%d", N); 6 }

What is the output of this code? 16? It might surprise you, as a result, 8. The reason is that the preprocessor simply replaces and expands the macro references that appear in the C source program file, and does not perform any computational processing. So the 4th line of code expands after n=2+2*2+2, the result is 8. To make the result 16, just define the macro as follows.

#define VALUE (+ +)

Similar problems can occur in macro definitions with parameters.

#define Fun (x) x*xvoid  main () {    int n=fun (2+2);    printf ("%d", N);}

The output of this code is still 8. The reason is as above.

4. Three symbols: #,##,#@

#define Conn (x, y) x# #y#define tochar (x) #@x#define ToString #x

x# #y: represents x connection Y.

such as: int n=conn (12,89); The result of N is 1289;

char* str=conn ("abc", "Der"); Str results in str= "Abcder";

#@x: Represents a single quotation mark for x, and returns a const char.

#x: Represents a double quotation mark for X, and returns a string.

C Language Learning--macro definition

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.