#defned typedef 以及#ifdef #define #ifndef #endif

1、#define 和typedef区别

 

#define一般用于定义常量和语句宏定义,在编译的时候充当宏替换。当然也可以完成typedef的一小部分功能,记住只是一小部分。

typedef 用来声明新类型名,目的是为了可读性以及可定义变量性。

 

例子1:

#define INT  int  //没有分号!!

typedef int  INT;//记住typedef前面没有 #,以及该语句需要 ; 分号结束。

 

这个例子中,INT num;这样语句都能实现,基本没有区别。

 

例子2:

#define pINT  int*

typedef int* pINT;

这个例子就有区别了,要定义int*a,*b;  用define实现的是 pINT a,b;替换之后 int* a,b;变成定义的是指针a,和变量b。而typedef 用法,pINT a,b;定义的是指针变量a和指针变量b。

 

以在声明新类型名的时候要用typedef,为了安全性。

 

2、#define 、#if defined()、#ifndef、#ifdef、#else   #elseif  #endif

a)#define 定义常量,如#define  DEBUG 说明DEBUG已有定义

b)#if defined(DEBUG),如果只有一个条件,等同于#ifdef  DEBUG

     所以#if defined()常用于多条件限制的环境,多条件一般中间可以用“||、&& 、!”来形成多条件判断,如#if defined(EBUG)&& defined(WIN32)

     意思是当DEBUG 和WIN32都有定义的时候,才进入#if条件下的语句。

c)#ifdef DEBUG 如果定义了DEBUG ,执行#ifdef 条件下的语句。其意图与#ifndef DEBUG

相反,中间的n代表没有的意思,即如果没有定义。。。。

d)#endif  配对#if 配对

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/*** *eh.h - User include file for exception handling. * * Copyright (c) 1993-1997, Microsoft Corporation. All rights reserved. * *Purpose: * User include file for exception handling. * * [Public] * ****/ #if _MSC_VER > 1000 #pragma once #endif #ifndef _INC_EH #define _INC_EH #if !defined(_WIN32) && !defined(_MAC) #error ERROR: Only Mac or Win32 targets supported! #endif #ifdef _MSC_VER // Currently, all MS C compilers for Win32 platforms default to 8 byte // alignment. #pragma pack(push,8) #endif // _MSC_VER #ifndef __cplusplus #error "eh.h is only for C++!" #endif /* Define _CRTIMP */ #ifndef _CRTIMP #ifdef _DLL #define _CRTIMP __declspec(dllimport) #else /* ndef _DLL */ #define _CRTIMP #endif /* _DLL */ #endif /* _CRTIMP */ /* Define _CRTAPI1 (for compatibility with the NT SDK) */ #ifndef _CRTAPI1 #if _MSC_VER >= 800 && _M_IX86 >= 300 #define _CRTAPI1 __cdecl #else #define _CRTAPI1 #endif #endif typedef void (__cdecl *terminate_function)(); typedef void (__cdecl *unexpected_function)(); typedef void (__cdecl *terminate_handler)(); typedef void (__cdecl *unexpected_handler)(); #ifndef _MAC struct _EXCEPTION_POINTERS; typedef void (__cdecl *_se_translator_function)(unsigned int, struct _EXCEPTION_POINTERS*); #endif #if _MSC_VER >= 1200 _CRTIMP __declspec(noreturn) void __cdecl terminate(void); _CRTIMP __declspec(noreturn) void __cdecl unexpected(void); #else _CRTIMP void __cdecl terminate(void); _CRTIMP void __cdecl unexpected(void); #endif _CRTIMP terminate_function __cdecl set_terminate(terminate_function); _CRTIMP unexpected_function __cdecl set_unexpected(unexpected_function); #ifndef _MAC _CRTIMP _se_translator_function __cdecl _set_se_translator(_se_translator_function); #endif #ifdef _MSC_VER #pragma pack(pop) #endif // _MSC_VER #endif // _INC_EH

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值