Preprocessing and function types

table of Contents

 

1. Macro definition

1.1 Macro definition without parameters

1.2 Macro definition with parameters

Two, #include directive

Three, conditional compilation

3.1 #if command, #else command and elif command

①, #if command

②, #else command

③, #elif command

3.2 #ifdef command and #ifndef command

3.3 #undef command

3.4 #line command

3.5 #pragma command

Four, internal function and external function

4.1 Internal functions

4.2 External functions


1. Macro definition

1.1 Macro definition without parameters

#define PI 3.14159

一般用大写字母表示,以便与变量名相区分


#define SIDE 5
#define PERIMETER 4 * SIDE
#define  AREA SIDE * SIDE

#define  STAND "you are girl"
printf(STAND);

Note :

  • If there is a macro name in the string, no replacement is performed

#define TEST "this is an example" 

main()
{
	char exp[30] = "The TEST is not that TEST";
   	printf(" %s\n", exp);
   	printf("\n");
}

  • If the string is longer than one line, you can use '\' to continue the line at the end of the line.
  • The #define command appears outside the function in the program, and the effective range of the macro name defines the command to the end of the source file.

 

  • Use the #define command to terminate the scope of the macro definition
#define TEST "this is an example" 

main()
{
   	printf(TEST);
   	printf("\n");
   	#undef TEST
}	

1.2 Macro definition with parameters

#define MIX(a,b) ((a) * (b) + (b))

main()
{
	int x = 5, y = 9;
	
   	printf("%d, %d\n", x, y);
   	printf("the min number %d\n", MIX(x, y));
   	printf("\n");
   	
}	

Note :

  • Parentheses should be added to the parameters when defining the macro.
  • Macro expansion uses parentheses to protect the low-priority operations of the expression, so that the desired effect can be achieved when called.

Two, #include directive

#include can include the entire content of another source file,

#include "studio.h"
#include <stdio.h>
  • Angle brackets: The system searches for the files to be included in the directory where the C library function header files are stored, that is, the standard way. Such as: the header file included for calling library functions
  • Double quotation marks: The system first searches for the files to be included in the user's current directory, if not found, then searches for the files to be included in the directory where the C library function header file is stored. For example: files written by the user .

There are embedded files embedded in the #include command, nested files.

文件f1.h
#define P printf
#define S scanf
#define D "%d"
#define C "%c"


文件f2.c
#include <stdio.h>
#include "f1.h"

void main()
{
    int a;
    P("请输入第一个整数值:\n");
    S(D, &a);
}

In general, put the following content in .h

  • Macro definition
  • Structure, union, and enumeration declarations
  • typedef declaration
  • External function declaration
  • Global variable declaration

Note :

  • A #include command can only specify one included file
  • File includes can be nested, that is, one included file can also contain another included file
  • When the file file2.h is included in filel.c, it becomes one file instead of two files after precompilation. At this time, if there is a global static variable in file2.h, the global variable is in the filel.c file It is also valid, and there is no need to use the extern statement at this time.

Three, conditional compilation

3.1 #if command, #else command and elif command

①, #if command

#if command: If the parameter expression after the #if command is true, compile the program segment between #if to #endif, otherwise skip this program. #endif is used to indicate the end of the #if section.

form:

#define NUM 50

main()
{
	int i = 0;
	
	#if NUM > 50
	 	i++;
	#endif
	
	#if NUM == 50
	 	i = i + 50;
	#endif
	
	#if NUM < 50
	 	i--;
	#endif
	
   	printf(" %d\n", i);
   	printf("\n");
}	

②, #else command

#else: Provide another option when #if is false,

#define NUM 50

main()
{
	int i = 0;
	
	#if NUM > 50
	 	i++;
	#else
	#if NUM < 50
	 	i--;
	#else
	 	i = i + 50;
	#endif
	#endif
	
   	printf(" %d\n", i);
   	printf("\n");
}	

③, #elif command

#elif command: used to create "if... or if..."

 

#define NUM 50

main()
{
	int i = 0;
	
	#if NUM > 50
	 	i++;
	#elif NUM < 50
	 	i--;
	#else
	 	i = i + 50;
	#endif
	
   	printf(" %d\n", i);
   	printf("\n");   	
}	

3.2 #ifdef command and #ifndef command

 #ifdef: if defined

  • #ifdef: If the macro replacement name has been defined, the "sentence segment" is compiled; if the macro replacement name after #ifdef is not defined.

  • #ifdef is used in conjunction with #else: if the macro replacement name has been defined, "sentence segment 1" is compiled; if the macro replacement name after #ifdef is not defined, "sentence segment 2" is compiled.

 #ifndef: If there is no definition

  • #ifndef: If the macro replacement name is not defined, the "sentence segment" is compiled; if the macro replacement name after #ifndef is defined.

  • #ifndef is used in conjunction with #else: If the macro substitution name has not been defined, the "sentence segment 1" is compiled; if the macro substitution name after #ifndef is defined, the "sentence segment 2" is compiled.

#define STR "dillgence is the parent of success\n"

main()
{
	#ifdef STR
		printf(STR);
	#else
		printf("dillgence is the root of all evil\n");  
	#endif
	
	printf("\n");
	
	#ifndef ABC
		printf("dillgence is the root of all evil\n");
	#else
		printf(STR);  
	#endif
	
   	printf("\n");
   	
}	

3.3 #undef command

#undef can delete the pre-defined macro definition.

#define MAX_SIZE 100
char array[MAX_SIZE];
#undef  MAX_SIZE

3.4 #line command

#line is used to change the contents of __LINE__ and __FILE__, __LINE__ stores the line number of the current compilation line, and __FILE__ stores the current compilation file name

The line number is the current line number in the source program, and the file name is the name of the source file. The command #line is mainly used for debugging and other special applications.

#line 100 "15.7.C" 
 
  main()
{
  	printf("1.当前行号:%d\n", __LINE__);
  	printf("2.当前行号:%d\n", __LINE__);
	printf("\n");
 }
 

3.5 #pragma command

#pragma: Set the status of the compiler, or instruct the compiler to complete some specific actions.

  • message parameter: This parameter can output the corresponding message in the compiling message output window.
  • code_seg parameter: set the code segment where the function code in the program is stored
  • code parameter: Ensure that the header file is compiled once.

Predefined macro name

  • __LINE__: The line number of the currently compiled code.
  • __FILE__: The file name of the current source program.
  • __DATE__: the creation time of the current source program
  • __TIME__: The creation time of the current source program
  • __STDC__: used to judge whether the current compiler is C standard, its value is 1, which means it conforms to standard C, otherwise it is not standard C

Four, internal function and external function

4.1 Internal functions

Internal function (static function): I hope this function is only used by the source file where it is located,

static int ADD(int iNum1, int iNum2)
{
}

static char* GetString(char* pString)
{
	return pString;
}

static void ShowString(char* pString)
{
	printf("%s\n", pString );
}
 
  main()
{
  	char* pMyString;
  	pMyString = GetString("MingRi");
  	ShowString(pMyString);
  	
	printf("\n");
 }

4.2 External functions

The definition of external functions can be modified with extern. When using an external function, you need to use extern to declare that the function used is an external function

extern int Add(int iNum1, int iNum2);
extern char* GetString(char* pString);
extern void ShowString(char* pString);

 char* GetString(char* pString)
{
	return pString;
}

 void ShowString(char* pString)
{
	printf("%s\n", pString );
}
 
  main()
{
  	char* pMyString;
  	pMyString = GetString("MingRi");
  	ShowString(pMyString);
	printf("\n");
 }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_41070511/article/details/114134988