directory
- A typedef profile
- Typedefs simplify complex type declarations
- A. Define common variables
- B. Define function Pointers
- C. Define the structure
- 2. Define platform-independent types
- Typedefs simplify complex type declarations
- 2. Define profile
- Typedef is different from define
- 1. The execution time is different
- 2. Different functions
- 3. Different scopes
- Four. Guess you like it
C/C++ learning Directory >> C language basics
A typedef profile
A typedef is an alias for an existing type. It is more convenient to use. Note that it does not generate a new type.
Typedefs simplify complex type declarations
A. Define common variables
typedef int REAL_TYPE; REAL_TYPE a = 10; Int a = 10; printf("a=%d",a);Copy the code
B. Define function Pointers
// declare a typedef bool (*FuncPointer)(int, double) that returns a bool with two (int and double) parameters; FuncPointer FuncPointer pFunc; // Declare a FuncPointer object pFunc of type FuncPointerCopy the code
C. Define the structure
In C, if a structure is defined without a typedef, it must be declared as a struct; otherwise, an error will be reported. ** The following is an example:
// no typedef struct _Person {string name; int age; float height; }; struct _Person person; // use a typedef typedef struct _Person {string name; int age; float height; }Person; Person person; // Using typedef to define variables is much simplerCopy the code
inC++
A structure is defined in thetypedef
Declaration variables are not addedstruct
Can also be used normally;
2. Define platform-independent types
A typedef is used to define a platform-independent type. For example, the same code needs to be used on Linux/Mac/Windows, and each platform has its own differences.
#ifdef __APPLE__ //MAC platform: REAL_TYPE indicates the int type typedef int REAL_TYPE; # typepedef double REAL_TYPE; # typepedef double REAL_TYPE; #else //Linux platform: REAL_TYPE indicates bool type typedef bool REAL_TYPE; #endifCopy the code
Reloading the above code can be seen in different platform definitionsREAL_TYPE
Variable, the corresponding data types are not the same, heavy and can be customized according to the differences of the platform setting problem!
2. Define profile
In C, we can use #define to define an identifier to represent a constant/function. The general form of this identifier is:
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language typedef and define difference // @time :2021/06/27 08:00 // @motto: a small step without a thousand miles, a small flow without a river or sea, The wonderful program life needs to accumulate unremittingly! / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / define constants # define MAX_VALUE 100 // Define integer variable MAX_VALUE as 100 #define USER_NAME "huge" // define string variable USER_NAME as "huge" #define PI 3.1415926 #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
Anything that starts with # is a preprocessor, also known as precompile. Precompilation is not compilation, but processing before compilation. This is done automatically by the system before formal compilation.
use
define
The key to defining a complex multi-line function is to add one at each newline\
;
Typedef is different from define
1. The execution time is different
The keywordtypedef
It’s valid at compile time, and since it’s compile time, sotypedef
Type checking is available.
[# define] (https://www.codersrc.com/archives/8894.html) is a macro definition, occur in the pretreatment stage, namely before compilation, it only for simple and mechanical string substitution, without any checks. Anything that starts with # is a preprocessor, also known as precompile. Precompilation is not compilation, but processing before compilation. This is done automatically by the system before formal compilation.
2. Different functions
typedef
Aliases used to define types, define platform-independent data types, andstruct
And so on.#define
Not only can you alias a type, but you can also define constants, variables, compiler switches, and so on.
3. Different scopes
#define
There is no scope limit, as long as it is previously predefined macro, in the future program can be used;
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language typedef and define difference // @time :2021/06/27 08:00 // @motto: a small step without a thousand miles, a small flow without a river or sea, The wonderful program life needs to accumulate unremittingly! /******************************************************************************************/ void func1() { #define HW "HelloWorld"; } void func2() { string str = HW; cout << str << endl; }Copy the code
typedef
Has its own scope;
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language typedef and define difference // @time :2021/06/27 08:00 // @motto: a small step without a thousand miles, a small flow without a river or sea, The wonderful program life needs to accumulate unremittingly! /******************************************************************************************/ void func1() { typedef unsigned int UINT; } void func2() { UINT uValue = 5; //error C2065: 'UINT' : undeclared identifier }Copy the code
Four. Guess you like it
- C array subscript out of bounds and memory overflow difference
- C language pointer declaration and definition
- P ++ / p –
- The C languagep++/§ + + / _ (p++) / _p + +
- C language uses Pointers to iterate over groups of numbers
- C language pointer and array difference
- C language pointer array and array pointer difference
- C NULL pointer
- C void Pointers
- C language field pointer
- C function value passing and address passing
- Default parameter of C language function
- C language function variable parameter
- C function pointer
- C language pointer function
- C language callback function callback
- C typedef
- C defines constants
- C define prevents repeated inclusion of header files
- C defines functions
- C language define function (multi-line writing)
- C typedef is different from define
C language typedef and define are different
This article is published by the blog – Ape Say Programming Ape Say programming!