009 预处理 #pragma once; typedef; #ifdef

 

 

/*
目录:
   一 #pragma once    
   二 typedef
   三 #ifdef
*/

 

一 #pragma once

为了避免同一个文件被include多次,C/C++中有两种方式,一种是#ifndef方式,一种是#pragma once方式

 


二 typedef

// 给类型/结构取别名

 

#include "stdafx.h"

typedef char * PCHAR;


int main(int argc, char *argv[])
{
    char szStr[20] = "test";
    PCHAR pStr = szStr;

    printf("%c", *pStr);

    return 0;
}

 

 

// 系统编写 - minwindef.h
typedef unsigned long       DWORD;
typedef int                 BOOL;
typedef unsigned char       BYTE;
typedef unsigned short      WORD;
typedef float               FLOAT;
typedef FLOAT               *PFLOAT;
typedef BOOL near           *PBOOL;
typedef BOOL far            *LPBOOL;
typedef BYTE near           *PBYTE;
typedef BYTE far            *LPBYTE;
typedef int near            *PINT;
typedef int far             *LPINT;
typedef WORD near           *PWORD;
typedef WORD far            *LPWORD;
typedef long far            *LPLONG;
typedef DWORD near          *PDWORD;
typedef DWORD far           *LPDWORD;
typedef void far            *LPVOID;
typedef CONST void far      *LPCVOID;

typedef int                 INT;
typedef unsigned int        UINT;
typedef unsigned int        *PUINT;

 

 

// 组合应用 :  typedef struct

 

#include "stdafx.h"

#define PI 11            // 宏替换

typedef int DATA;        // 重命名: 类型 - int 
typedef struct SNode    // 重命名: 类型 - struct和struct指针 
{
    DATA data;
    SNode *pNext;
}SNode, *pSNode;        

typedef SNode* PNODE;    // 重命名: 类型 - struct指针


int main(int argc, char *argv[])
{
    SNode node = { PI, nullptr };
    pSNode pNode = &node;
    printf("%d\n", pNode->data);

    PNODE pNode2 = &node;
    printf("%d\n", pNode2->data);

    return 0;
}
/*
// result
11
11
*/

 

 

typedef bool(*pCompare)(StuNode *pMin, StuNode *pIndex);

 


三 #ifdef

 

#ifdef _XXXX
  ...程序段1...    // 如果定义XXXX,执行程序段1
#else
  ...程序段2...    // 如果没定义XXXX,执行程序段2
#endif

 

// 用于调试
#define  DEBUG

int main()
{
#ifdef DEBUG
    printf("%d", _xxx);
#endif
    return 0;
}

 

 

// 系统应用

 

// corecrt.h
#ifndef _CRT_NO_TIME_T
    #ifdef _USE_32BIT_TIME_T
        typedef __time32_t time_t;
    #else
        typedef __time64_t time_t;
    #endif
#endif

// corecrt.h
typedef long                          __time32_t;
typedef __int64                       __time64_t;


// test.cpp
#include "stdafx.h"
#include 
int main(int argc, char *argv[])
{


    struct tm *ptr;
    time_t it;

    it = time(NULL);
    ptr = gmtime(&it);
    printf(ctime(&it));

    printf("__time32_t size = %d\n", sizeof(__time32_t));
    printf("__time64_t size = %d\n", sizeof(__time64_t));    
    return 0;
}
/*
Sat Sep 14 11:31:03 2019
__time32_t size = 4
__time64_t size = 8

*/

 

你可能感兴趣的:(009 预处理 #pragma once; typedef; #ifdef)