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
  • 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 thetypedefDeclaration variables are not addedstructCan 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_TYPEVariable, 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.

usedefineThe 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 keywordtypedefIt’s valid at compile time, and since it’s compile time, sotypedefType 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

  • typedefAliases used to define types, define platform-independent data types, andstructAnd so on.
  • #defineNot only can you alias a type, but you can also define constants, variables, compiler switches, and so on.

3. Different scopes

#defineThere 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

typedefHas 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

  1. C array subscript out of bounds and memory overflow difference
  2. C language pointer declaration and definition
  3. P ++ / p –
  4. The C languagep++/§ + + / _ (p++) / _p + +
  5. C language uses Pointers to iterate over groups of numbers
  6. C language pointer and array difference
  7. C language pointer array and array pointer difference
  8. C NULL pointer
  9. C void Pointers
  10. C language field pointer
  11. C function value passing and address passing
  12. Default parameter of C language function
  13. C language function variable parameter
  14. C function pointer
  15. C language pointer function
  16. C language callback function callback
  17. C typedef
  18. C defines constants
  19. C define prevents repeated inclusion of header files
  20. C defines functions
  21. C language define function (multi-line writing)
  22. 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!