C language typedef and #define

Sometimes in order to code portability and cross-platform development, or to shorten the writing characters, use typedef for type renaming.

typedef

effect

With a custom name for an existing data type name.

defined as:

typedef  datatype  name;

Instructions

To facilitate memory, can be summarized as the steps of:

  1. First with the existing definition of a variable type

  2. Before type definition statement added typedef

  3. The variable name into a custom name

  4. Finally, there must be a semicolon;

See the following procedures:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef int TYPE32;
typedef int ARRAY[10];
typedef char *POINTER;
typedef char *POINTERARRAY[3];

int main()
{
    TYPE32 a = 10;
    ARRAY arr = {1,2,3,4,5,6,7,8,9,10};
    POINTER ps= "123";
    POINTERARRAY pt= {"123","456","789"};

    printf("sizeof(TYPE32) = %d\n",sizeof(TYPE32));
    printf("sizeof(ARRAY) = %d\n",sizeof(ARRAY));
    printf("sizeof(POINTER) = %d\n",sizeof(POINTER));
    printf("sizeof(POINTERARRAY) = %d\n",sizeof(POINTERARRAY));
    putchar(10);

    printf("a = %d\n",a);
    putchar(10);
    for (int i = 0;i<sizeof(arr)/sizeof(arr[0]);i++)
        printf("arr[%d] = %d\n",i,arr[i]);
    putchar(10);
    printf("a = %s\n",ps);
    putchar(10);
    for (int i = 0;i<sizeof(pt)/sizeof(pt[0]);i++)
        printf("arr[%d] = %s\n",i,pt[i]);

    return 0;
}

The results are:

sizeof(TYPE32) = 4
sizeof(ARRAY) = 40
sizeof(POINTER) = 4
sizeof(POINTERARRAY) = 12

a = 10

arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
arr[5] = 6
arr[6] = 7
arr[7] = 8
arr[8] = 9
arr[9] = 10

a = 123

arr[0] = 123
arr[1] = 456
arr[2] = 789

Mainly on the typedef array. Do not be confused.

#define

effect

#define replace text only in the pretreatment stage, using the format:

Source text replacement text #define

Sometimes the role of typedef and #define will be the same, but sometimes there is a huge difference. See the following procedures:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef char *POINTER;
#define POINTERD char *

int main()
{
    POINTER pa,pb;
    printf("sizeof(POINTER) = %d\n",sizeof(POINTER));
    printf("sizeof(pa) = %d sizeof(pb) = %d\n",
           sizeof(pa),sizeof(pb));

    POINTERD pc,pd;
    printf("sizeof(POINTERD) = %d\n",sizeof(POINTERD));
    printf("sizeof(pc) = %d sizeof(pd) = %d\n",
           sizeof(pc),sizeof(pd));

    return 0;
}

The results are:

sizeof(POINTER) = 4
sizeof(pa) = 4 sizeof(pb) = 4
sizeof(POINTERD) = 4
sizeof(pc) = 4 sizeof(pd) = 1

The results can be seen from the upper side:

  • typedef is a statement, sentence semicolon
  • #define is a macro, not a complete statement
  • typedef can only give existing types of aliases, not nothing new types
  • Define macro is completed at the time of the pretreatment, typedef is done at compile time, as compared with the better typedef
  • In general, the type of diet rename uppercase to distinguish shown
  • Sometimes we will carry out some simple structure function macros, code nesting, thereby reducing code redundancy, reduce call overhead, but can also cause compiled file is too large
Published 77 original articles · won praise 5 · Views 4863

Guess you like

Origin blog.csdn.net/SAKURASANN/article/details/104649497