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

  1. C array subscript out of bounds and memory overflow difference
  2. C language pointer declaration and definition
  3. P ++ / p –
  4. The C languagep++/§ + + / _ (p++) / _p + +
  5. C language uses Pointers to iterate over groups of numbers
  6. C language pointer and array difference
  7. C language pointer array and array pointer difference
  8. C NULL pointer
  9. C void Pointers
  10. C language field pointer
  11. C function value passing and address passing
  12. Default parameter of C language function
  13. C language function variable parameter
  14. C function pointer
  15. C language pointer function
  16. C language callback function callback
  17. C typedef
  18. C defines constants
  19. C define prevents repeated inclusion of header files
  20. 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!