A summary of the usage of define in Linux C _c language

Source: Internet
Author: User
Tags constant mul centos

The use of define is only a pure substitution function, and the substitution of the macro definition is the replacement that the preprocessor handles.

One: Simple macro-definition usage

Formatting: Replacing content with #define Identifiers

The replacement can be numbers, characters, strings, special characters, and spaces, and what will be replaced with what is behind it.

For example:

#define N 5 effect is equivalent to int array [5];

int array[n];

The same effect:

#define N = 5

int array[n]; The effect is equivalent to int array[= 5];

The same effect:

#define N 5;

int array[n]; The effect is equivalent to int array[5;];

Common type of error:

#define PIN int*

Pin A, B; Actually the effect is int *a, b;

#define N 2+2
void main (void)
{
int a = n * n;
printf ("%d\n", a);
}

The result is 2+2*2+2=8.

Two: Use of macro definitions with parameters

Example speech: A function to find a square area

The correct notation for using #define should be:

#include <stdio.h>
#define AREA (x)  ((x) * (x))
int main (void)
{
 int s = Area (3 + 3);
 printf ("s =%d\n", s);
 return 0;
}

Run Result: (3+3) * (3+3) = 36 is the result we want

FAQ written:

#include <stdio.h>
#define AREA (x)  x*x
int main (void)
{
 int s = Area (3 + 3);
 printf ("s =%d\n", s);
 return 0;
}

Run Result: 3 + 3 * 3 + 3 = 15 Not the result we want

This is more a reflection of the previous define macro definition is a pure substitution, do is the first to replace the work of computing.

Ways to prevent this problem:

To be able to really use a good macro definition, to prevent the occurrence of the above 122 common conditions of some errors, we must remember that in the idea of the application of the macro in the program to replace all the characters it represents the string, do not take the liberty to add any other symbols, fully expanded after the corresponding calculation, will not write wrong running results. When programming with a macro substitution, when there is more than one symbol in the string, the parentheses show precedence, and if the macro is defined with parameters, enclose each argument in the macro body with parentheses over the entire macro body.

Three: Commonly used as a function of encapsulation

Example speech: Now the original a function to find the product of two numbers mult

[Linux@centos-64-min exercise]$ cat mul.c 
#include <stdio.h>
int mult (int x, int y)
{
 int  = x * y;
 return result;
}

Now you need different two functional functions, one is to find the function of the square area Square_area and a function Rectangle_area to find the area of the rectangle.

You can write this:

[Linux@centos-64-min exercise]$ cat mul.c 
#include <stdio.h>
int mult (int x, int y)
{
 int = x * y;
 return result;
}
[Linux@centos-64-min exercise]$ cat try.c 
#include <stdio.h>
int mult (int x, int y); /* Two number of functions multiplied by the declaration/* *

#define SQUARE_AREA (str, x) mult (x, x)/* encapsulated into a square area of function/
#define RECTANGLE_AREA (str, x , y) mult (x, y)/* encapsulated into a function that asks for rectangular area * * * *

above those function declarations and macro definitions are written in a more canonical form, which should have been placed in a header file, where the problem is simply put in the function. *
/INT Main (  void)
{
 int s = 0;
 s = Square_area ("This is the" square ", 3);
 printf ("This is the" the square:s =%d\n ", s);
 s = Rectangle_area ("This is the" rectangle ", 3, 4);
 printf ("This is the" the rectangle:s =%d\n ", s);
 return 0;
}

Run Result:

[Linux@centos-64-min exercise]$ gcc-o try Try.c MUL.O
[Linux@centos-64-min exercise]$./try
This is the square:s = 9
This is the rectangle:s = 12

Four: Three special symbols in define: #,##,#@

#define CONN (x,y) x# #y
#define TOCHAR (x) #@x
#define TOSTRING (x) #x

x# #y表示x连接y, for example:
int n = Conn (123,456); The result is n=123456;
char* str = Conn ("asdf", "ADF") the result is str = "ASDFADF";

#@x, in fact, is to add single quotes to x, and the result returns a const char. For example,
char a = ToChar (1); The result is a= ' 1 ';
Do a transboundary test. Char a = ToChar (123); the result is wrong;
But if your argument is more than four characters, the compiler will give you an error! Error C2015:too many characters in Constant:p

#x, to add double quotes to X
char* str = ToString (123132); it became str= "123132";

Five: summary #define MACRO definition

(1) Facilitate the modification of the program
Use a simple macro to define an available macro to replace a constant that you use frequently in your program. So when you change the constant, you don't have to modify the entire program, just modify the macro-defined string, and when the constant is longer, we can write the program with a shorter, meaningful identifier, which is more convenient.

(2) The macro definition is to be replaced at the time of precompilation. After the call to the child function in the program must be called after the child function of the scene continues to execute, so that there will be the consumption of function conversion. However, this problem does not occur with macro definitions with parameters. Because it is a macro expansion during the preprocessing phase, it does not need to be converted, that is, executed locally, but the complex operation is performed by function calls, and the macro definition occupies a relatively large object code space. So use it to decide whether to use a macro definition, depending on the circumstances.

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.