1. prevent repeated definitions in a header file
The format is as follows:
# Ifndef comdef_h
# Define comdef_h
// Header file content
# Endif
This can be seen at the beginning of many header files, that is, it cannot be understood. What is repeated definition ??? Try a program
#include <stdio.h>
int main(int argc,char *argv[])
{
printf("lsdkfla/n");
}
#include <stdio.h>
#include <stdio.h>
int main(int argc,char *argv[])
{
printf("lsdkfla/n");
}
Editing Using Dynamic files
#gcc test1.c -Wall
The compilation structure is correct ....
#gcc test2.c -Wall
Oh, no error either ??? What's the purpose of this !!! I don't understand. Is it possible that there will be two stdio. h library connections in the. Out structure compiled by test2.c? I will compile the static library. Compile to a static file:
#gcc -Wall -static test1.c -o test1
Compilation result, no error!
#gcc -Wall -static test2.c -o test2
The compilation structure is correct... Use LS-L to check the file size. Oh, it's as big as it is... What is the function? I thought for a long time... I suddenly thought of writing a header file.
- Example 3 test. h main. c
/*test.h*/
#include <stdio.h>
int test(int a)
{
printf("test .../n");
}
/*main.c*/
#include "test.h"
int main(int argc,char *argv[])
{
test(4);
}
Compilation Method:
#gcc -Wall main.c
Compilation result, no error .... Continue the test .....
- Example 4 test. h main. c
/* Test. H */
#include <stdio.h>
int test(int a)
{
printf("test .../n");
}
/*main.c*/
#include "test.h"
#include "test.h"
int main(int argc,char *argv[])
{
test(4);
}
Compilation result: An error occurred... Haha...
Test. h: 3: Error: 'test' redefinition
Test. h: 3: Error: 'test' is defined here
I finally figured it out ..... So let me add a macro definition .... Example 5 test. h main. c/* test. H */
# Ifndef _ test_h
# DEFINE _ test_h
# Include <stdio. h>
Int test (int)
{
Printf ("test.../N ");
}
# Endif/* end _ test_h */
/*main.c*/
#include "test.h"
#include "test.h"
int main(int argc,char *argv[])
{
test(4);
}
Compilation Method:
#gcc -Wall main.c
Compilation result: the program is compiled normally without any error message ....
[Edit]
2 prevent the number of bytes of the type produced by different platforms and compilers
Typedef unsigned char Boolean;/* Boolean value type .*/
Typedef unsigned long int uint32;/* unsigned 32 bit value */
Typedef unsigned short uint16;/* unsigned 16 bit value */
Typedef unsigned char uint8;/* unsigned 8 bit value */
Typedef signed long int int32;/* signed 32 bit value */
Typedef signed short int16;/* signed 16 bit value */
Typedef signed Char int8;/* signed 8 bit value */
// The following is not recommended
Typedef unsigned char byte;/* unsigned 8 bit value type .*/
Typedef unsigned short word;/* unsinged 16 bit value type .*/
Typedef unsigned long DWORD;/* unsigned 32 bit value type .*/
[Edit]
3. Get a byte or word on the specified address.
Obtains a word of the specified address.
#define MEM_B(x) (*((byte *)(x)))
Obtains a word of the specified address.
#define MEM_W(x) (*((word *)(x)))
Example 1 test. c
#include <stdio.h>
#define MEM_B(x) (*((byte *)(x)))
int main(int argc,char *argv[])
{
char a='c';
printf("&a==%c /n",MEM_B(&a));
}
Compilation Method:
#gcc -Wall test.c
Execution result:
&a==c
[Edit]
4. Calculate the maximum and minimum values.
Definition method:
#define MAX( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define MIN( x, y ) ( ((x) < (y)) ? (x) : (y) )
Example: test. c
#include <stdio.h>
#define MAX(x,y) (((x)>(y))?(x):(y))
int main(int argc,char *argv[])
{
printf("max==%d /n",MAX(4,5));
}
Compilation Method:
#gcc -Wall test.c
Execution result:
#max==5
[Edit]
5. Get the offset of a field in the struct.
Definition method:
#define FPOS( type, field ) ( (dword) &((( type *) )0)-> field )
This is the most depressing one, and finally it's done .... Example of depressing typedef and calculation rules: Test. c
#include <stdio.h>
typedef unsigned long dword;
#define FPOS( type,field) (dword)&(((type *)0)->field)
typedef struct test
{
int a;
int b;
char c ;
}d;
int main(int argc,char *argv[])
{
printf("offset==%d /n",FPOS(d,b));
}
Compilation Method:
#gcc -Wall test.c
Execution result
#offset==0x4
The "&" operation level is smaller than the "->" operation level.
C language operator table is recommended. </Tr> </table>
[Edit]
6. Obtain the number of bytes occupied by the field in the struct.
Definition method:
#define FSIZ(type,field) sizeof(((type *) 0)->field )
Example: test. c
#include <stdio.h>
#define FSIZ(type,field) sizeof(((type *) 0)->field)
struct test
{
int a;
int b;
char c;
};
int main(int argc,char *argv[])
{
printf("sizeof==%d /n",FSIZ(struct test,b);
}
Compilation Method:
#gcc -Wall test.c
Execution result:
#sizeof=4
[Edit]
7. convert two bytes into a word in LSB format
Tip: definitions of LSB and MSB
- The LSB (least significant bit) is the "least significant bit ".
- MSB (most significant bit) is "the most significant bit"
Definition Format
#define FLIPW(ray) ((((word)(ray)[0])* 256)+(ray)[1])
Example: test. c
#include <stdio.h>
typedef unsigned short word;
#define FLIPW(ray) ((((word)(ray)[0])* 256)+(ray)[1])
int main(int argc,char *argv[])
{
char test[2]={0x06,0x07};
printf("LSB==%d /n",FLIPW(test));
}
Compilation Method:
#gcc -Wall test.c
Execution Structure
#1541
??? Which number is ??? 6*256 + 5 = 1541 suddenly came up with an idea. It cannot be changed to a byte order. Let's take a look... Example: test. c
- Include <stdio. h>
typedef unsigned short word;
#define FLIPW(ray) ((((word)(ray)[1])* 256)+(ray)[2])
int main(int argc,char *argv[])
{
char test[2]={0x06,0x07};
printf("LSB==%d /n",FLIPW(test));
}
Compilation Method:
#gcc -Wall test.c
Execution Structure
#1286
?? Which number is ??? 5*256 + 6 = 1286
[Edit]
8. convert a word into two bytes in LSB format.
Definition method:
#define FLOPW( ray, val ) (ray)[0] = ((val) / 256); (ray)[1] = ((val) & 0xFF)
[Edit]
9 references
Document 1: macro definition skills in C Language
Http://www.ednchina.com/blog/levension/21415/message.aspx
Document 2:
Http://gawaine.itpub.net/
Document 3:
http://www.yesky.com/309/1853309.shtml
| Operator |
Explanation |
Combination Method |
| () []->. |
Parentheses (functions, etc.), arrays, two types of structure member access |
From left to right |
| ! ~ ++ -- +- * & (Type) sizeof </TD> |
Negative, bitwise negative, incremental, decrement, positive and negative, <p> indirect, address fetch, type conversion, and Size Estimation |
From right to left |
| */% |
Multiplication, division, modulo |
From left to right |
| +- |
Add, subtract |
From left to right |
| <> |
Left shift, right shift |
From left to right |
| <<=>=> |
Less than, less than or equal to, greater than or equal to, greater |
From left to right |
| =! = |
Equal to, not equal |
From left to right |
| & |
Bitwise AND |
From left to right |
| ^ |
Bitwise OR |
From left to right |
| | |
By bit or |
From left to right |
| && |
Logic and |
From left to right |
| | |
Logic or |
From left to right |
| ? : |
Condition |
From right to left |
| = + =-= * =/= <P> & = ^ = |=< <=> = |
Various assignments |
From right to left |
| , |
Comma (Order) |
From left to right |