简体   繁体   English

为什么也将C和C ++关键字“ #defined”或“ typedefed”作为其大写版本?

[英]Why are C and C++ keywords “#defined” or “typedefed” as their capital versions too?

Sorry for the confusing title but I couldn't think of a title that would describe my question clearly. 抱歉,标题令人困惑,但我想不出一个标题可以清楚地描述我的问题。 When writing a C or C++ program, you can usually refer to keywords such as char with CHAR, or int with INT, or void with VOID, and on. 在编写C或C ++程序时,通常可以使用CHAR来引用诸如char的关键字,或者使用INT来引用int,或者使用VOID来引用void等等。

In winnt.h, they are defined as follows: 在winnt.h中,它们的定义如下:

#ifndef VOID
#define VOID void
typedef char CHAR;
typedef short SHORT;
typedef long LONG;
#if !defined(MIDL_PASS)
typedef int INT;
#endif
#endif

What is the use of doing this? 这样做有什么用? Is it to satisfy convention or ensure compatibility? 是满足约定还是确保兼容性?

I've always thought of these trivial typedefs (eg int -> INT) to be more about consistency of than anything. 我一直认为这些琐碎的typedef(例如int-> INT)比一致性更重要。

What I mean by this, is that there are many many typedefs in Win32 in caps - DWORD, WORD, LPARAM, WPARAM, HANDLE, HWND, some of these are simply integers and their name provides context as to their use, and some of them (eg RECT and WNDCLASS) represent structures. 我的意思是,大写的Win32中有许多typedef-DWORD,WORD,LPARAM,WPARAM,HANDLE,HWND,其中一些仅仅是整数,它们的名称提供了有关其用法的上下文,其中一些(例如RECT和WNDCLASS)表示结构。

Consider the case of function void f(int, BOOL, char, PAINTSTRUCT); 考虑函数void f(int,BOOL,char,PAINTSTRUCT); to me it feels more consitent to allow all capitals: VOID f(INT, BOOL, CHAR, PAINTSTRUCT). 对我来说,允许使用所有大写字母更合适:VOID f(INT,BOOL,CHAR,PAINTSTRUCT)。 But that could just be me. 但这可能只是我。

Mostly convention if I'm not mistaken. 如果我没记错的话,通常都是约定俗成的。 I think windows put some of them in their to ensure compatibility for some data types (integers for example, maintaining compatibility of 8 bit, 16 bit, 32 bit, etc.). 我认为Windows将其中的一些放入其中以确保对某些数据类型的兼容性(例如,整数,保持8位,16位,32位等的兼容性)。

The capitalized (#defined) versions are often used to allow the code to be made compatible with various compilers. 大写(#defined)版本通常用于使代码与各种编译器兼容。 And typedefs are typically used to ensure the newly named types meet stricter rules than the originals defined by the compiler. typedef通常用于确保新命名的类型比编译器定义的原始类型满足更严格的规则。

Typically, typedef int INT , or the like can be seen inside blocks that are detecting the compiler-environment, so that a name like "INT" can be used to provide more restrictive semantics that "int". 通常,可以在检测编译器环境的块内看到typedef int INT等,因此可以使用诸如“ INT”之类的名称来提供“ int”的更多限制性语义。

In "C", all primitive types, like int and char , are "at least" a certain size (I don't know that size right now). 在“ C”中,所有基本类型(如intchar )都“至少”具有一定大小(我现在不知道该大小)。 Which means the compiler is allowed to make them larger. 这意味着允许编译器将它们放大。 I know of a system that used a 32-bit char type. 我知道一个使用32位char类型的系统。 This leads to easily writing non-portable code when assumptions are made, such as assuming "int" is always 32 bits. 进行假设(例如,假设“ int”始终为32位)时,这很容易编写不可移植的代码。

An experiment in your environment might help. 在您的环境中进行实验可能会有所帮助。 Try a small program like the following and then compile and run it with 32-bit settings and then 64-bit settings: 请尝试以下类似的小程序,然后使用32位设置和64位设置进行编译和运行:

int main (int argc, char **argv)
{
    printf("sizeof INT=%u\n", sizeof (INT));
    printf("sizeof int=%u\n", sizeof (int));
}

As far as VOID, defining it to "void" is a little odd. 就VOID而言,将其定义为“ void”有点奇怪。 However, it looks like - in the snippet given - it's being used to determine if the others have already been defined by the same header file - which is a very important step in "C" since duplicate typedefs lead to errors. 但是,在给定的代码段中,它看起来像是用来确定其他文件是否已经由同一头文件定义了-这是“ C”中非常重要的一步,因为重复的typedef会导致错误。 IIRC, duplicate defines can lead to errors or warnings, denpending on the compiler and settings. IIRC,重复定义会导致错误或警告,严重影响编译器和设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM