Program Environment and Preprocessing

1. Linux environment practice, familiar with using (network search)
ls//list to list all files in the current folder
cd//change directory Change (switch) directory
mkdir//make directory Create directory (folder)
rm//remove delete file
mv//move move file
touch//create file
pwd//print work directory print current working directory


3. Familiarize yourself with preprocessing identifiers:
__LINE__ : the current line number of the file
__FILE__ : the source file
that was edited __DATE__ : the date the
file was edited __TIME__ : the time the file was edited


4. The difference between macros and functions:
   The differences are mainly reflected in: code length, execution speed, operator priority, parameter evaluation, and parameter types .

 Code length:

    #define Macro: Macro code is inserted into the program each time it is used. Except for very small macros, the length of the program will grow substantially.

    Function: The function code appears in only one place; every time the function is used, the same code in that place is called.

 Execution speed:

    #define macro: faster.

    Functions: There is additional overhead of function calls/returns.

 Operator precedence:

    #define macros: Macro arguments are evaluated in the context of all surrounding expressions, unless they are parenthesized, the precedence of adjacent operators may produce unpredictable results.

    Function: The function parameter is evaluated only once when the function is called, and its result value is passed to the function. Expression evaluation results are more predictable.

 Parameter evaluation:

    #define macros: Parameters are re-evaluated each time they are used in a macro definition. Parameters with side effects may have unpredictable results due to multiple evaluations.

    Functions: Parameters are evaluated once when the function is called, and multiple use of parameters in a function does not result in multiple evaluations. Side effects of parameters do not cause any special problems.

 Parameter Type:

    #define macros: Macros are type independent.

    Function: The parameters of the function are related to the type. If the parameters are of different types, you need to use different functions, even if they perform the same task.

 

5. Write a macro to swap the odd and even digits of a number.
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#define SWAP(x)\
 (((x&(0x55555555)) << 1) | ((x&(0xAAAAAAAA))>>1))//hex
/ / Extract the odd bits of x and shift left by one, even bits and shift right by one

int main()
{
 int x = 5;
 printf("%d\n", SWAP(x));
 system("pause");
 return 0;
}


6. Use macros to find the larger of two numbers.
#include <stdio.h>
#define MAX(a,b)\
 ((a) > (b) ? (a) : (b))
int main()
{
 int a = 5;
 int b = 10;
 printf( "%d\n", MAX(a, b));
 system("pause");
 return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324786864&siteId=291194637