Macro definition in C # define usage Summary

Source: Internet
Author: User

1. Simple macro definition # define identifier replacement list (the replacement list can be a number, string literal, punctuation, operator, identifier, keyword, and character constant. Note: The replacement list can be empty.) typical error: # define N = 100int a [N];/* will become int a [= 100]. it will be processed as an identifier mark */# define N 100; int a [N];/* the definition with a semicolon will become int a [100;], this is a common error */# define pin (int *); pin a, B; int * a, B;/* indicates that both a and B are int pointers, but actually it turns into int * a, B; a is an int pointer, and B is an int variable. This should use typedef instead of define, so that both a and B are int type pointers. */Typical usage: using macro definition, we can define or even change the syntax habits of C language based on our own habits, for example: # define BEGIN {# define END} int main () BEGINprintf ("DEFINE ---- \ n"); END defines a LOOP # define LOOP for (;) redefinition of the Data Type # define IT int 2. macro with parameters # define identifier (x1, x2, x3... xn) replacement list (Note: x1, x2, x3 .. is a macro parameter, and there cannot be spaces between the identifier and Its left arc.) usage: # define MAX (x, y) (x)> (y )? (X) :( y) I = MAX (j + k, m-n); replace with: I = MAX (j + k)> (m-n )? (J + k) :( m-n); # define SI_EX (n) % 2 = 0) if (SI_EX (I) I ++; replace it with: if (SI_EX (I) % 2 = 0) We can see from the above example that the identifier has parameters (X1, X2, X3 .....) the replacement list will be replaced (Y1, Y2, Y3 ....) but it is irrelevant to the order. 3. macro special single row definition # define A (x) T _ # x # define B (x) # @ x # define C (x) # xx = 1: A (1) ------> T_1 (T _ # x, # Is A sign that sticks to the mark and sticks the character before and after .) B (1) ------> '1' (# @ x, # @: the macro parameter is quantified literally by adding the ''sign) C (1) ------> "1" (# x, # indicates that macro parameters are literally quantified and "is added) 4. define's multi-line definition define can replace multi-line code, and add "\" # define MAX (X, Y) do {\ Statement 1 to each line feed; \ Statement 2; \/* Comment Writing Method */\} while (0)/* (no trailing;) */\ 5. in large-scale development, especially for cross-platform and system software, Conditional compilation is the most important function of define. # Ifdef WINDOWS ............ # endif # ifdef LINUX ............ # endif you can use # define to set the compiling environment during compilation. 6. cancel macro # undef identifier 7. conditional compilation # ifdef XXX... (# Else )... # Endif 8. predefined macros include some useful Macros in the C language, which provide the current compilation information. _ LINE _ number of lines of the compiled FILE (integer type) _ FILE _ name of the compiled FILE (character type) _ DATE _ compile DATE (character type) _ TIME _ compile TIME _ STDC _ If the compiler Accepts standard C, the value is 1. (integer) during compilation, we can use these predefined macros for troubleshooting. For example, in general, the compiler does not prompt when division by zero occurs, we can customize a solution. # Define CHECKZERO (divisor) \ if (divisor = 0) \ printf ("*** attempt to divide by zero in line % d of file % s *** \ n" ,__ LINE __,__ FILE __); \ reference method CHECKZERO (j); k = I/j;

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.