Preprocessing and function types

1, Macro definition

1.1 macro definition without parameters

#define PI 3.14159

It is generally expressed in uppercase letters to distinguish it from variable names


#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, it will not be replaced
#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 continue the line with '\' at the end of the line.
  • #The define command appears outside the function in the program. The valid range of the macro name defines the command, and then ends at this 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:

  • Parameters should be enclosed in parentheses when defining macros.
  • Macro extensions use parentheses to protect the low priority operation of the expression, so as to achieve the desired effect when called.

2, #include instruction

#Include can include all the contents of another source file,

#include "studio.h"
#include <stdio.h>
  • Angle brackets: the system looks for the file to be included in the directory where the C library function header file is stored, that is, the standard method. For example, it refers to the header file contained in the calling library function
  • Double quotation marks: the system first looks for the file to be included in the user's current directory. If it cannot be found, the system then looks for the file to be included in the directory where the C library function header file is stored For example: documents written by users themselves

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

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


file f2.c
#include <stdio.h>
#include "f1.h"

void main()
{
    int a;
    P("Please enter the first integer value:\n");
    S(D, &a);
}

Generally, the following contents are placed in h medium

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

Note:

  • A #include command can specify only one included file
  • File inclusion can be nested, that is, one included file can also contain another included file
  • When in filel C contains the file File2 H, it will become one file instead of two files after precompiling. At this time, if File2 If there is a global static variable in H, the global variable is in filel It is also valid in the C file. At this time, there is no need to declare it with extern.

3, Conditional compilation

3.1 #if command, #else command and elif command

① , #if command

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

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 for #if 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 substitution name has been defined, compile the "statement segment"; If no macro substitution name is defined #ifdef later.

  • #Ifdef and #else: if the macro substitution name has been defined, compile "statement segment 1"; If you do not define #ifdef the following macro replacement name, compile "statement segment 2".

#ifndef: if there is no definition

  • #Ifndef: if the macro substitution name is not defined, compile the "statement segment"; If you define #ifndef the following macro replacement name.

  • #Ifndef and #else: if the macro substitution name has not been defined, compile "statement segment 1"; If you define #ifndef the following macro substitution name, "statement 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 pre-defined macro definitions.

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

3.4 \line command

#Line is used to change__ LINE__ And__ FILE__ The content of__ LINE__ Store the line number of the current compilation line__ FILE__ Store the file name of the current compilation

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.Current line number:%d\n", __LINE__);
  	printf("2.Current line number:%d\n", __LINE__);
	printf("\n");
 }
 

3.5 #pragma command

#pragma: sets the state of the compiler or instructs the compiler to complete some specific actions.

  • message parameter: this parameter can output the corresponding information in the compilation information output window.
  • code_seg parameter: set the code segment stored in the function code in the program
  • 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__: Creation time of current source program
  • __ TIME__: Creation time of current source program
  • __ STDC__: It is used to judge whether the current compiler is standard C, and its value is 1, which means it conforms to standard C, otherwise it is not standard C

4, Internal and external functions

4.1 internal functions

Internal function (static function): you want this function to be used only 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

Defining external functions can be decorated 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");
 }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Tags: C

Posted by aouriques on Thu, 14 Apr 2022 23:19:09 +0300