Dark Horse programmer------C Language Learning Notes---macro definitions in C

Source: Internet
Author: User

1.5 C language Program running process

01 source program: Written by high-level or assembly language, C language source program extension. C

02 Target Program: The source program is translated by the "compiler" binary code is the target program, its extension is. obj

03 Executables: The target program is connected to the library function to form an executable program. Out

#include <stdio.h>intMain () {#definePI 3.14DoubleR,len,area; printf ("Please enter radius: \ n");//Prompt user to enter RADIUSscanf ("%LF", &r);//Accept DataLen=2*pi*R; Area= pi*r*R; printf ("len=%lf,area=%lf\n", Len,area); return 0;}

1.6 Macro definitions in the C language

01 macro Definition without parameters

#define Identifier string

L #代表这是一条预处理命令

The l identifier is the defined macro name, usually in uppercase letters.

L strings can be constants, expressions, format strings, etc.

L macro definition is not a statement, then the end of the line without a semicolon, if you add a semicolon with a semicolon to replace

The scope of the macro definition starts at the beginning of the macro definition command to the end of the source program, and you can use #undef to terminate its scope

The macro definition uses a macro name to represent a string that, when expanded, replaces the macro name with the string, but simply replaces

The macro name is not expanded in the source program if it is enclosed in double quotation marks.

L macro definition does not allocate memory, only character substitution

L macro definition can be nested

#include <stdio.h>#define PI 3.14#define R 3.0#define L 2*pi*r#define AR pi*r *rint  main () {    double  len;         Double Area ;         = L;                 = AR;            printf ("len=%lf,area=%lf\n", Len,area);                     return 0 ;}

02 macro Definition with Parameters

#define Macro Name (formal parameter list) string

No spaces between macro name and formal parameter list

L usually enclose the formal parameter in parentheses, and enclose the string in parentheses as well.

//Enter a radius to calculate the circumference and area of the circle#include <stdio.h>#definePI 3.14#defineL (R) 2*pi*r#defineS (R) pi*r*rintMain () {DoubleX,len,area; printf ("input radius: \ n"); scanf ("%LF",&x); Len=L (x); Area=S (x); printf ("len=%lf,area=%lf\n", Len,area); return 0;}

Dark Horse programmer------C Language Learning Notes---macro definitions in C

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.