directory
- A. Define profile
- Define a function
- 1. Define a function that takes no arguments
- 2. Define a function with parameters
- Define the function trap
- 1. Define function trap 1
- 2. Define function trap a solution
- 3. Define function trap 2
- 4. Define function trap 2 solution
- Four. Guess you like it
C/C++ learning Directory >> C language basics
A. Define profile
In C, in addition to using #define to define an identifier to represent a constant, you can also use #define to define functions, such as:
// define constant #define MAX_VALUE 100 // define integer variable MAX_VALUE 100 #define USER_NAME "huge" // define string variable USER_NAME value "huge" #define PI #define MAX(a,b) (a>b)? #define MIN(a,b) (a<b)? A :b // Take the minimum of both numbersCopy the code
In the#
Preprocessing instructions, also known as precompilation, are used at the beginning. Precompilation is not compilation, but processing before compilation. This is done automatically by the system before formal compilation.
Define a function
1. Define a function that takes no arguments
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language define function // @time :2021/06/27 08:00 // @motto: no small steps no thousands of miles, no small streams no rivers and oceans, the wonderful life of the program needs unremitting accumulation! /******************************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <string> #define RESULT (x*2+5) int main() { int x = 10; Printf (" the RESULT is: % d \ n ", RESULT); return 0; } /* RESULT is: 25 */Copy the code
2. Define a function with parameters
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language define function // @time :2021/06/27 08:00 // @motto: no small steps no thousands of miles, no small streams no rivers and oceans, the wonderful life of the program needs unremitting accumulation! /******************************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <string> #define MAX(a,b) (a>b)? #define MIN(a,b) (a<b)? Int main() {printf(" MAX: %d\n",MAX(5,100)); Printf (" minimum value is: %d\n",MIN(5,100)); return 0; } /* The maximum value is 100. The minimum value is 5 */Copy the code
Define the function trap
1. Define function trap 1
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language define function // @time :2021/06/27 08:00 // @motto: no small steps no thousands of miles, no small streams no rivers and oceans, the wonderful life of the program needs unremitting accumulation! /******************************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <string> #define RESULT(x) x*x int main() {printf("RESULT is: %d\n",RESULT(2)); Printf (" the RESULT is: % d \ n ", the RESULT (2 + 1)); return 0; } /* RESULT: 4 RESULT: 5 */Copy the code
Very deceiving?? Why is the second calculation 5 instead of 9?
#define RESULT(x) x*x RESULT(2) = 2*2Copy the code
2. Define function trap a solution
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language define function // @time :2021/06/27 08:00 // @motto: no small steps no thousands of miles, no small streams no rivers and oceans, the wonderful life of the program needs unremitting accumulation! /******************************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <string> #define RESULT(x) (x)*(x) int main() {printf("RESULT is: %d\n",RESULT(2)); Printf (" the RESULT is: % d \ n ", the RESULT (2 + 1)); // equivalent (2+1 *(2+1) = 9 return 0; } /* RESULT is: 4 RESULT is: 9 */Copy the code
3. Define function trap 2
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language define function // @time :2021/06/27 08:00 // @motto: no small steps no thousands of miles, no small streams no rivers and oceans, the wonderful life of the program needs unremitting accumulation! /******************************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <string> #define RESULT(x) (x)*(x) int main() {printf("RESULT is: %d\n",RESULT(2)); Printf (" the RESULT is: % d \ n ", 9 / RESULT (2 + 1)); return 0; } /* RESULT is: 4 RESULT is: 9 */Copy the code
Very deceiving?? Why is the second calculation 9 instead of 1?
#define RESULT(x) x*x RESULT(2) = 2*2Copy the code
4. Define function trap 2 solution
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language define function // @time :2021/06/27 08:00 // @motto: no small steps no thousands of miles, no small streams no rivers and oceans, the wonderful life of the program needs unremitting accumulation! /******************************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <string> #define RESULT(x) ((x)*(x)) %d\n",RESULT(2)); Printf (" the RESULT is: % d \ n ", 9 / RESULT (2 + 1)); return 0; } /* RESULT: 4 RESULT: 1 */Copy the code
As the above discussion shows, it is safe to enclose parentheses not only around the parameters but also around the entire string for macro definitions.
Four. Guess you like it
- C array subscript out of bounds and memory overflow difference
- C language pointer declaration and definition
- P ++ / p –
- The C languagep++/§ + + / _ (p++) / _p + +
- C language uses Pointers to iterate over groups of numbers
- C language pointer and array difference
- C language pointer array and array pointer difference
- C NULL pointer
- C void Pointers
- C language field pointer
- C function value passing and address passing
- Default parameter of C language function
- C language function variable parameter
- C function pointer
- C language pointer function
- C language callback function callback
- C typedef
- C defines constants
- C define prevents repeated inclusion of header files
- C language to define Define a function
C language define a function
This article is published by the blog – Ape Say Programming Ape Say programming!