directory

  • A. Define profile
  • Define multiline functions
    • 1. Easy to use
    • 2. Classic cases
  • 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)? #define MACRO(arg1, arg2) do {\ \ stmt1; \ stmt2; \ \} While (0) The key is to add a "\ "to each newline.Copy 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.

usedefineDefine a complex function with multiple linesThe key is to add one at every newline\;

Define multiline functions

1. Easy to use

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language define function (multi-line writing) // @time :2021/06/27 08:00 // @motto: a small step without a thousand miles, a small stream without a river or sea, The wonderful program life needs to accumulate unremittingly! /******************************************************************************************/ #include "stdafx.h" #include <stdio.h> //#define swap(x,y) do{x=x+y; y=x-y; x=x-y; } #define swap1(x,y) \ do{\ x=x+y; \ y=x-y; \ x=x-y; \ }while(0) int main() { int a = 2, b = 3; Printf (" a=%d\t,b=%d\n", a, b); swap1(a, b); Printf (" a=%d\t,b=%d\n\n", a, b); return(0); } /* A =2,b=3, a=3,b=2 */Copy the code

2. Classic cases

/* Requirements: 1. in debug mode: a. If macro DEBUG_TO_FILE is defined, output logs to file B. If the macro DEBUG_TO_FILE is defined, Then print the log to console 2. Release mode does not print any log */ / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language define function (multi-line writing) // @time :2021/06/27 08:00 // @motto: a small step without a thousand miles, a small stream without a river or sea, The wonderful program life needs to accumulate unremittingly! /******************************************************************************************/ #include <stdio.h> #include <string.h> // Enable the following macro to indicate that the program is running in the debug version, otherwise it is the release version. Here we assume that only the debug version output debugging information #define _DEBUG #ifdef _DEBUG Comments are output to terminal #define DEBUG_TO_FILE #ifdef DEBUG_TO_FILE // Debug information is output to the following file #define DEBUG_FILE "/ TMP/debugmSG "/ / Buffer length of debug information #define #define printDebugMsg(moduleName, format,...) {\ char buffer[DEBUG_BUFFER_MAX+1]={0}; \ snprintf( buffer, DEBUG_BUFFER_MAX \ , "[%s] "format" File:%s, Line:%d\n", moduleName, ##__VA_ARGS__, __FILE__, __LINE__ ); \ FILE* fd = fopen(DEBUG_FILE, "a"); \ if ( fd ! = NULL ) {\ fwrite( buffer, strlen(buffer), 1, fd ); \ fflush( fd ); \ fclose( fd ); #define printDebugMsg(moduleName, format,...) \ printf( "[%s] "format" File:%s, Line:%d\n", moduleName, ##__VA_ARGS__, __FILE__, __LINE__ ); #endif //end for #ifdef DEBUG_TO_FILE #else #define printDebugMsg(moduleName, format,... #endif //end for #ifdef _DEBUG int main(int argc, char** argv) { printDebugMsg( "www.codersrc.com", "name = %s", "C language tutorial "); return 0; }Copy the code

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 defines functions
  21. C language define function (multi-line writing)

C language define a function (multi-line writing)

This article is published by the blog – Ape Say Programming Ape Say programming!