一直吐口水是什么原因| 滑膜炎吃什么药| ar技术是什么意思| 羊胡子疮用什么药膏| 320是什么意思| 动卧是什么意思| 看睾丸去医院挂什么科| 高血压可以喝什么饮料| 左眼皮一直跳什么预兆| 淋巴结肿大是什么样子的| 小巧玲珑是什么意思| 什么的旋律| 阴性是什么意思| 磁共振是做什么的| 奔富红酒属于什么档次| 腰椎疼痛挂什么科| 吃什么补肝| 六月二十一是什么日子| 馋肉是身体里缺什么| 检查鼻炎要做什么检查| 梦见怀孕是什么意思| 餐后血糖高是什么原因| 什么叫代谢| 人流后吃什么水果好| 吃坏东西拉肚子吃什么药| 早上起来后背疼是什么原因| 栽赃是什么意思| 大学团委书记什么级别| 慢阻肺吃什么药最有效最好| 易烊千玺的真名叫什么| 南辕北辙是什么故事| 地铁和高铁有什么区别| 2月18日什么星座| 女命七杀代表什么| 胸片可以检查出什么| 做梦相亲是什么意思| 弥漫什么意思| 腺肌瘤是什么病| 查高血压挂什么科| 邓超什么星座的| 泌尿系感染吃什么药| 小孩尿酸高是什么原因| 星座上升是什么意思| 二氧化硅是什么晶体| 玄学是什么| 为什么会得霉菌感染| 燕窝是补什么的| 什么叫通分| 肺结节是什么意思| 肚子不舒服是什么原因| vodka是什么酒| 肾阴虚什么症状| cd是什么意思| 前列腺增生吃什么药| 洗面奶和洁面乳有什么区别| 梦见和死人一起吃饭是什么意思| bmi是什么意思| 军区司令是什么级别| 胳膊脱臼是什么症状| a21和以纯什么关系| 单位是什么| 视网膜脱落是什么原因引起的| 看脱发应该挂什么科| 书是什么排比句| 花苞裤不适合什么人穿| 什么狗不咬人| 狗吐了是什么原因| 庚申五行属什么| 鉴黄师是什么职业| 泌尿外科主要检查什么| 七星瓢虫吃什么食物| 尿血吃什么消炎药| 头孢全名叫什么| 神经性耳鸣吃什么药| 月抛什么意思| 孟力念什么| 孕期吃什么好| 过敏痒用什么药膏| 处女座男生喜欢什么样的女生| 凝血酸是什么| 百合是什么意思| 冰释前嫌的释是什么意思| 结扎后需要注意什么| 男人秒射是什么原因| 气血不足吃什么补最快| 军国主义是什么意思| 金牛和什么星座最配| 胆囊切除后可以吃什么水果| 彩虹旗是什么意思| 内裤上有黄色分泌物是什么原因| 世界上最多笔画的字是什么字| speedo是什么牌子| 教导是什么意思| 喉咙不舒服吃什么药| 劣质是什么意思| 探病是什么意思| hd是什么意思| 什么是直男| 梦见牛肉有什么征兆| 核桃和什么一起打豆浆| 小孩用脚尖走路是什么原因| 心脏痛什么原因| 胎盘埋在什么地方最好| 不耐受和过敏有什么区别| 洗纹身去医院挂什么科| 10月7日是什么星座| 心结是什么意思| 手指缝脱皮是什么原因| 蚕豆是什么豆| 肚子疼是什么原因| 梦见头发白了是什么意思| 舂米是什么意思| 唯我独尊是什么生肖| 先天性聋哑病属于什么遗传病| 氨纶是什么面料优缺点| 大拇指旁边是什么指| 淋巴结肿大是什么样子| 误机是什么意思| 梦见钱是什么预兆| 纸尿裤和拉拉裤有什么区别| g6pd筛查是检查什么| 难过美人关是什么生肖| 胎盘吃了对身体有什么好处| 梦见自己头发白了是什么意思| 聚酯纤维是什么料子| 一月14号是什么星座| 不一样的烟火什么意思| 磨砂膏是什么| oid是什么意思| 清晰是什么意思| 猪油不凝固是什么原因| 关节痛挂号挂什么科| 表里不一是什么意思| 强迫症吃什么药效果好| 少尉军衔是什么级别| 牛和什么生肖相冲| 肾结石有什么症状表现| 梦见相亲是什么意思| 蚊子怕什么味道| 肛门松弛吃什么药| 阿斯伯格综合征是什么| outlets是什么意思| 大梁是什么朝代| 海参几头是什么意思| 狗拉稀吃什么药| 鹅是什么动物| 幺是什么意思| 地球代表什么生肖| 闻思修是什么意思| 药店属于什么行业| 打嗝是什么原因引起的| 一般细菌培养及鉴定是检查什么| 两肺纹理增重什么意思| 梦见一群羊是什么意思| 儿童抗o高会引起什么病| 血清碱性磷酸酶高是什么意思| 数据是什么意思| 白带是什么意思| 苏联是什么国家| 什么可以补肾壮阳| 5月14日什么星座| anca是什么检查| 头发掉的多是什么原因| 静脉曲张吃什么食物| 转氨酶高是什么问题| 眼睛红血丝多是什么原因| 1979年出生属什么生肖| 内热是什么意思| 人中之龙是什么意思| 什么的秋天| 香菜吃多了有什么坏处| 松香有毒吗对人体有什么危害| 多吃黑芝麻有什么好处| 真菌镜检阴性是什么意思| 糖衣炮弹什么意思| 背疽是什么病| 焱字五行属什么| 小狗感冒症状是什么样的| 摩羯前面是什么星座| 谷氨酰转移酶高是什么病| 术后吃什么伤口愈合快| 池塘边的榕树上是什么歌| 什么病治不好| 玫瑰花可以和什么一起泡水喝| 完全性右束支传导阻滞是什么意思| 发烧呕吐是什么原因| 邮政ems是什么意思| 从容不迫什么意思| 白带是什么样子| 谝是什么意思| 室性期前收缩是什么病| 奢靡是什么意思| 四川有什么烟| 前列腺用什么药| 无机磷偏低有什么影响| 尿频是什么原因造成的| 35属什么| 什么样的星星| 淋巴结节吃什么药最好| 吃避孕药对身体有什么影响| adivon是什么牌子| 做梦梦到钓鱼是什么意思| 双什么意思| 男朋友昵称叫什么好听| 肝火旺吃什么中成药| 芹菜不能和什么食物一起吃| 特别容易饿是什么原因| 小儿湿疹是什么原因造成的| 老人反复发烧预示什么| 钻石王老五是什么意思| 一个虫一个圣读什么| 福荫是什么意思| 阴茎勃起不硬吃什么| 下午6点半是什么时辰| 口臭吃什么| 百合和拉拉有什么区别| 黔驴技穷的意思是什么| 微米是什么单位| 美国为什么叫鹰酱| 威士忌是什么酒| 桃符指的是什么| 尿频尿急尿痛吃什么药| 小孩腹泻吃什么药好得快| 梦见打雷是什么意思| 螃蟹为什么吐泡泡| 大专什么专业就业前景好| 531是什么意思| 等位基因是什么| 为什么会长闭口| 宫颈纳囊多发是什么意思| only是什么牌子| 蜂窝织炎是什么病| 刚出生的小猫吃什么| 8月24号是什么星座| 百香果的籽有什么功效| 乳腺炎吃什么药| 属鸡的幸运色是什么颜色| 经常手麻是什么原因| dr检查是什么| 玫瑰痤疮是什么原因| eb病毒是什么| 开塞露用多了有什么副作用| 三皇五帝是什么时期| 什么东西含铅| 胸闷气短什么原因| 男性尿路感染有什么症状| 虚火旺吃什么去火最快| 涉水是什么意思| tnt什么意思| 三拜九叩是什么意思| 打感情牌是什么意思| 孕妇贫血对胎儿有什么影响| 人体最大的消化腺是什么| 做面包用什么面粉| 喝黄瓜汁有什么好处| 尿频尿不尽吃什么药| 什么是纳米| 失眠挂什么科| 航母舰长是什么级别| 如果怀孕了会有什么预兆| 单亲家庭什么意思| 尿隐血弱阳性什么意思| 游泳对身体有什么好处| 食管裂孔疝是什么病| 百度Jump to content

4月17日新股发行一览:传艺科技(002866)发行

From Wikipedia, the free encyclopedia
百度 规模较大的“药局”,组织的频率不会很高,娱乐界存在不同的社交圈子,平均每月举办一两次。

The C preprocessor (CPP) is a text file processor that is used with C, C++ and other programming tools. The preprocessor provides for file inclusion (often header files), macro expansion, conditional compilation, and line control. Although named in association with C and used with C, the preprocessor capabilities are not inherently tied to the C language. It can and is used to process other kinds of files.[1]

C, C++, and Objective-C compilers provide a preprocessor capability, as it is required by the definition of each language. Some compilers provide extensions and deviations from the target language standard. Some provide options to control standards compliance. For instance, the GNU C preprocessor can be made more standards compliant by supplying certain command-line flags.[2]

The C# programming language also allows for directives, even though they cannot be used for creating macros, and is generally more intended for features such as conditional compilation.[3] C# seldom requires the use of the directives, for example code inclusion does not require a preprocessor at all (as C# relies on a package/namespace system like Java, no code needs to be "included").

The Haskell programming language also allows the usage of the C preprocessor, which is invoked by writing {-# LANGUAGE CPP #-} at the top of the file. The accepted preprocessor directives align with those in standard C/C++.

Features of the preprocessor are encoded in source code as directives that start with #.

Although C++ source files are often named with a .cpp extension, that is an abbreviation for "C plus plus"; not C preprocessor.

Preprocessor directives

[edit]

The following languages have the following accepted directives.

C/C++

[edit]

The following tokens are recognised by the preprocessor in the context of preprocessor directives.

  • #if
  • #elif
  • #else
  • #endif
  • #ifdef
  • #ifndef
  • #elifdef
  • #elifndef
  • #define
  • #undef
  • #include
  • #embed
  • #line
  • #error
  • #warning
  • #pragma
  • defined (follows a conditional directive; not actually a directive, but rather an operator)
  • __has_include (operator)
  • __has_cpp_attribute (operator, C++ only)
  • __has_c_attribute (operator, C only)
  • __has_embed (operator)

Until C++26, the C++ keywords import, export, and module were partially handled by the preprocessor as well.

Haskell also accepts C preprocessor directives.

C#

[edit]

Although C# does not have a separate preprocessor, these directives are processed as if there were one.

  • #nullable
  • #if
  • #elif
  • #else
  • #endif
  • #define
  • #undef
  • #region
  • #endregion
  • #error
  • #warning
  • #line
  • #pragma

C# does not use a preprocessor to handle these directives, and thus they are not handled or removed by a preprocessor, but rather directly read by the C# compiler as a feature of the language.

Objective-C

[edit]

The following tokens are recognised by the preprocessor in the context of preprocessor directives.

  • #if
  • #elif
  • #else
  • #endif
  • #ifdef
  • #ifndef
  • #define
  • #undef
  • #include
  • #import
  • #error
  • #pragma
  • defined

History

[edit]

The preprocessor was introduced to C around 1973 at the urging of Alan Snyder and also in recognition of the usefulness of the file inclusion mechanisms available in BCPL and PL/I. The first version offered file inclusion via #include and parameterless string replacement macros via #define. It was extended shortly after, firstly by Mike Lesk and then by John Reiser, to add arguments to macros and to support conditional compilation.[4]

The C preprocessor was part of a long macro-language tradition at Bell Labs, which was started by Douglas Eastwood and Douglas McIlroy in 1959.[5]

Phases

[edit]

Preprocessing is defined by the first four (of eight) phases of translation specified in the C Standard.

  1. Trigraph replacement: The preprocessor replaces trigraph sequences with the characters they represent. This phase was removed in C23 following the steps of C++17.
  2. Line splicing: Physical source lines that are continued with escaped newline sequences are spliced to form logical lines.
  3. Tokenization: The preprocessor breaks the result into preprocessing tokens and whitespace. It replaces comments with whitespace.
  4. Macro expansion and directive handling: Preprocessing directive lines, including file inclusion and conditional compilation, are executed. The preprocessor simultaneously expands macros and, since the 1999 version of the C standard, handles _Pragma operators.

Features

[edit]

File inclusion

[edit]

There are two directives in the C preprocessor for including contents of files:

  • #include, used for directly including the contents of a file in-place (typically containing code of some kind)
  • #embed, used for directly including or embedding the contents of a binary resource in-place

Code inclusion

[edit]

To include the content of one file into another, the preprocessor replaces a line that starts with #include with the content of the file specified after the directive. The inclusion may be logical in the sense that the resulting content may not be stored on disk and certainly is not overwritten to the source file. The file being included need not contain any sort of code, as this directive will copy the contents of whatever file is included in-place, but the most typical use of #include is to include a header file (or in some rarer cases, a source file).

In the following example code, the preprocessor replaces the line #include <stdio.h> with the content of the standard library header file named 'stdio.h' in which the function printf() and other symbols are declared.

#include <stdio.h>

int main(void) {
    printf("Hello, World!\n");
    return 0;
}

In this case, the file name is enclosed in angle brackets to denote that it is a system file. For a file in the codebase being built, double-quotes are used instead. The preprocessor may use a different search algorithm to find the file based on this distinction.

For C, a header file is usually named with a .h extension. In C++, the convention for file extension varies with common extensions .h and .hpp. But the preprocessor includes a file regardless of the extension. In fact, sometimes code includes .c or .cpp files.

To prevent including the same file multiple times which often leads to a compiler error, a header file typically contains an #include guard or if supported by the preprocessor #pragma once to prevent multiple inclusion.

Binary resource inclusion

[edit]

C23 and C++26 introduce the #embed directive for binary resource inclusion which allows including the content of a binary file into a source even though it's not valid C code.[6][7] This allows binary resources (like images) to be included into a program without requiring processing by external tools like xxd -i and without the use of string literals which have a length limit on MSVC. Similarly to xxd -i the directive is replaced by a comma separated list of integers corresponding to the data of the specified resource. More precisely, if an array of type unsigned char is initialized using an #embed directive, the result is the same as-if the resource was written to the array using fread (unless a parameter changes the embed element width to something other than CHAR_BIT). Apart from the convenience, #embed is also easier for compilers to handle, since they are allowed to skip expanding the directive to its full form due to the as-if rule.

The file to embed is specified the same as for #include – either with brackets or double quotes. The directive also allows certain parameters to be passed to it to customize its behavior. The C standard defines some parameters and implementations may define additional. The limit parameter is used to limit the width of the included data. It is mostly intended to be used with "infinite" files like urandom. The prefix and suffix parameters allow for specifying a prefix and suffix to the embedded data. Finally, the if_empty parameter replaces the entire directive if the resource is empty. All standard parameters can be surrounded by double underscores, just like standard attributes on C23, for example __prefix__ is interchangeable with prefix . Implementation-defined parameters use a form similar to attribute syntax (e.g., vendor::attr) but without the square brackets. While all standard parameters require an argument to be passed to them (e.g., limit requires a width), this is generally optional and even the set of parentheses can be omitted if an argument is not required, which might be the case for some implementation-defined parameters.

const unsigned char icon_display_data[] = {
    #embed "art.png"
};

/* specify any type which can be initialized form integer constant expressions will do */
const char reset_blob[] = {
    #embed "data.bin"
};

/* attributes work just as well */
const signed char aligned_data_str[] __attribute__ ((aligned (8))) = {
    #embed "attributes.xml"
};

int main() {
    return
#embed </dev/urandom> limit(1)
    ;
}

Conditional compilation

[edit]

Conditional compilation is supported via the if-else core directives #if, #else, #elif, and #endif and with contraction directives #ifdef and #ifndef which stand for #if defined(...) and #if !defined(...), respectively. In the following example code, the printf() call is only included for compilation if VERBOSE is defined.

#ifdef VERBOSE
  printf("trace message");
#endif

The following demonstrates more complex logic:

#if !(defined __LP64__ || defined __LLP64__) || defined _WIN32 && !defined _WIN64
	// code for a 32-bit system
#else
	// code for a 64-bit system
#endif

Macro string replacement

[edit]
Object-like

A macro specifies how to replace text in the source code with other text. An object-like macro defines a token that the preprocessor replaces with other text. It does not include parameter syntax and therefore cannot support parameterization. The following macro definition associates the text "1 / 12" with the token "VALUE":

#define VALUE 1 / 12
Function-like

A function-like macro supports parameters; although the parameter list can be empty. The following macro definition associates the expression "(A + B)" with the token "ADD" that has parameters "A" and "B".

#define ADD(A, B) (A + B)

A function-like macro declaration cannot have whitespace between the token and the first, opening parenthesis. If whitespace is present, the macro is interpreted as object-like with everything starting at the first parenthesis included in the replacement text.

Expansion

The preprocessor replaces each token of the code that matches a macro token with the associated replacement text in what is known as macro expansion. Note that text of string literals and comments is not parsed as tokens and is therefore ignored for macro expansion. For a function-like macro, the macro parameters are also replaced with the values specified in the macro reference. For example, ADD(VALUE, 2) expands to 1 / 12 + 2.

Variadic

A variadic macro (introduced with C99) accepts a varying number of arguments which is particularly useful when wrapping functions that accept a variable number of parameters, such as printf.

Order of expansion

Function-like macro expansion occurs in the following stages:

  1. Stringification operations are replaced with the textual representation of their argument's replacement list (without performing expansion).
  2. Parameters are replaced with their replacement list (without performing expansion).
  3. Concatenation operations are replaced with the concatenated result of the two operands (without expanding the resulting token).
  4. Tokens originating from parameters are expanded.
  5. The resulting tokens are expanded as normal.

This may produce surprising results:

#define HE HI
#define LLO _THERE
#define HELLO "HI THERE"
#define CAT(a,b) a##b
#define XCAT(a,b) CAT(a,b)
#define CALL(fn) fn(HE,LLO)
CAT(HE, LLO) // "HI THERE", because concatenation occurs before normal expansion
XCAT(HE, LLO) // HI_THERE, because the tokens originating from parameters ("HE" and "LLO") are expanded first
CALL(CAT) // "HI THERE", because this evaluates to CAT(a,b)

Undefine macro

[edit]

A macro definition can be removed from the preprocessor context via #undef such that subsequent reference to the macro token will not expand. For example:

#undef VALUE

Predefined macros

[edit]

The preprocessor provides some macro definitions automatically. The C standard specifies that __FILE__ expands to the name of the file being processed and __LINE__ expands to the number of the line that contains the directive. The following macro, DEBUGPRINT, formats and prints a message with the file name and line number.

#define DEBUGPRINT(_fmt, ...) printf("[%s:%d]: " _fmt, __FILE__, __LINE__, __VA_ARGS__)

For the example code below that is on line 30 of file util.c and for count 123, the output is: [util.c:30]: count=123.

DEBUGPRINT("count=%d\n", count);

The first C Standard specified that __STDC__ expand to "1" if the implementation conforms to the ISO standard and "0" otherwise and that __STDC_VERSION__ expand to a numeric literal specifying the version of the standard supported by the implementation. Standard C++ compilers support the __cplusplus macro. Compilers running in non-standard mode must not set these macros or must define others to signal the differences.

Other standard macros include __DATE__, the current date, and __TIME__, the current time.

The second edition of the C Standard, C99, added support for __func__, which contains the name of the function definition within which it is contained, but because the preprocessor is agnostic to the grammar of C, this must be done in the compiler itself using a variable local to the function.

One little-known usage pattern of the C preprocessor is known as X-Macros.[8][9][10] An X-Macro is a header file. Commonly, these use the extension .def instead of the traditional .h . This file contains a list of similar macro calls, which can be referred to as "component macros." The include file is then referenced repeatedly.

Many compilers define additional, non-standard macros. A common reference for these macros is the Pre-defined C/C++ Compiler Macros project, which lists "various pre-defined compiler macros that can be used to identify standards, compilers, operating systems, hardware architectures, and even basic run-time libraries at compile-time."

Most compilers targeting Microsoft Windows implicitly define _WIN32.[11] This allows code, including preprocessor commands, to compile only when targeting Windows systems. A few compilers define WIN32 instead. For such compilers that do not implicitly define the _WIN32 macro, it can be specified on the compiler's command line, using -D_WIN32.

#ifdef __unix__ /* __unix__ is usually defined by compilers targeting Unix systems */
# include <unistd.h>
#elif defined _WIN32 /* _WIN32 is usually defined by compilers targeting 32 or 64 bit Windows systems */
# include <windows.h>
#endif

The example code tests if a macro __unix__ is defined. If it is, the file <unistd.h> is then included. Otherwise, it tests if a macro _WIN32 is defined instead. If it is, the file <windows.h> is then included.

Line control

[edit]

The values of the predefined macros __FILE__ and __LINE__ can be set for a subsequent line via the #line directive. In the code below, __LINE__ expands to 314 and __FILE__ to "pi.c".

#line 314 "pi.c"
printf("line=%d file=%s\n", __LINE__, __FILE__);

Operators

[edit]

The preprocessor is capable of interpreting operators and evaluating very basic expressions, such as integer constants, arithmetic operators, comparison operators, logical operators, bitwise operations, the defined operator, and the # stringificafion operator. This allows the preprocessor to make evaluations such as:

#if X == 10 // if X equals 10, the preprocessor sees #if 10 == 10

Defined operator

[edit]

While the defined operator, denoted by defined is not a directive in its own right, if it is read within a directive, it is interpreted by the preprocessor and determines whether a macro has been defined.

The following are both accepted ways of invoking the defined operator.

#if defined(MY_MACRO)
#if defined MY_MACRO

Token stringification operator

[edit]

The stringification operator (a.k.a. stringizing operator), denoted by # converts a token into a string literal, escaping any quotes or backslashes as needed. For definition:

#define str(s) #s

str(\n) expands to "\n" and str(p = "foo\n";) expands to "p = \"foo\\n\";".

If stringification of the expansion of a macro argument is desired, two levels of macros must be used. For definition:

#define xstr(s) str(s)
#define str(s) #s
#define foo 4

str(foo) expands to "foo" and xstr(foo) expands to "4".

A macro argument cannot be combined with additional text and then stringified. However, a series of adjacent string literals and stringified arguments, also string literals, are concatenated by the C compiler.

Token concatenation

[edit]

The token pasting operator, denoted by ##, concatenates two tokens into one. For definition:

#define DECLARE_STRUCT_TYPE(name) typedef struct name##_s name##_t

DECLARE_STRUCT_TYPE(g_object) expands to typedef struct g_object_s g_object_t.

Abort

[edit]

Processing can be aborted via the #error directive. For example:

#if RUBY_VERSION == 190
#error Ruby version 1.9.0 is not supported
#endif

Warning

[edit]

As of C23[12] and C++23,[13] a warning directive, #warning, to print a message without aborting is provided. Some typical uses are to warn about the use of deprecated functionality. For example:

Prior to C23 and C++23, this directive existed in many compilers as a non-standard feature, such as the C compilers by GNU, Intel, Microsoft and IBM. Because it was non-standard, the warning macro had varying forms:

// GNU, Intel and IBM
#warning "Do not use ABC, which is deprecated. Use XYZ instead."

// Microsoft
#pragma message("Do not use ABC, which is deprecated. Use XYZ instead.")

Non-standard features

[edit]

Pragma

[edit]

The #pragma directive is defined by standard languages, but with little or no requirements for syntax after its name so that compilers are free to define subsequent syntax and associated behavior. For instance, a pragma is often used to allow suppression of error messages, manage heap and stack debugging and so on.

C99 introduced a few standard pragmas, taking the form #pragma STDC ..., which are used to control the floating-point implementation. The alternative, macro-like form _Pragma(...) was also added.

One of the most popular uses of the #pragma directive is #pragma once, which behaves the same way an #include guard would, condensed into a single directive placed at the top of the file. Despite being non-standard, it is supported by most compilers.

Trigraphs

[edit]

Many implementations do not support trigraphs or do not replace them by default.

Assertion

[edit]

Some Unix preprocessors provided an assertion feature – which has little similarity to standard library assertions.[14]

Include next

[edit]

GCC provides #include_next for chaining headers of the same name.[15]

Import

[edit]

Unlike C and C++, Objective-C includes an #import directive that is like #include but results in a file being included only once – eliminating the need for include guards and #pragma once.

#import <Foundation/Foundation.h>
#import "MyClass.h"

In Microsoft Visual C++ (MSVC), there also exists an #import preprocessor directive, used to import type libraries.[16] It is a nonstandard directive.

#import "C:\\Program Files\\Common Files\\System\\ado\\msado15.dll" no_namespace rename("EOF", "ADOEOF")

The Objective-C directive should not be confused with the C++ keyword import, which is used to import C++ modules (since C++20), and is not a preprocessor directive.

Nullable

[edit]

The #nullable directive in C# is used to enable and disable nullable reference types. To enable them, use #nullable enable, and #nullable disable to disable them.

#nullable enable

string? name = null; // OK
string fullName = null; // Warning: possible null assignment

#nullable disable

string test = null; // No warning

This directive does not exist in C/C++.

Region

[edit]

The #region and #endregion directives in C# are used to expand/collapse sections of code in IDEs, and has no effect on actual compilation of the program. It is primarily used for code organisation and readability.

#region Helper methods

void Log(string message)
{
    Console.WriteLine(message);
}

#endregion

While this directive does not exist in C/C++, MSVC and Visual Studio instead have #pragma region and #pragma endregion.[17] Thus the equivalent C++ code would be:

#pragma region Helper methods

void log(std::string_view message) {
    std::println(message);
}

#pragma endregion

Other uses

[edit]

Traditionally, the C preprocessor was a separate development tool from the compiler with which it is usually used. In that case, it can be used separately from the compiler. Notable examples include use with the (deprecated) imake system and for preprocessing Fortran. However, use as a general purpose preprocessor is limited since the source code language must be relatively C-like for the preprocessor to parse it.[2]

The GNU Fortran compiler runs "traditional mode" CPP before compiling Fortran code if certain file extensions are used.[18] Intel offers a Fortran preprocessor, fpp, for use with the ifort compiler, which has similar capabilities.[19]

CPP also works acceptably with most assembly languages and Algol-like languages. This requires that the language syntax not conflict with CPP syntax, which means no lines starting with # and that double quotes, which CPP interprets as string literals and thus ignores, don't have syntactical meaning other than that. The "traditional mode" (acting like a pre-ISO C preprocessor) is generally more permissive and better suited for such use.[20]

Some modern compilers such as the GNU C Compiler provide preprocessing as a feature of the compiler; not as a separate tool.

Limitations

[edit]

Text substitution limitations

[edit]

Text substitution has a relatively high risk of causing a software bug as compared to other programming constructs.[21][22]

Hidden multiple evaluation

Consider the common definition of a max macro:

#define max(a,b) (((a) > (b)) ? (a) : (b))

The expressions represented by a and b are both evaluated two times due to macro expansion, but this aspect is not obvious in the code where the macro is referenced. If the actual expressions have constant value, then multiple evaluation is not problematic from a logic standpoint even though it can affect runtime performance. But if an expression evaluates to a different value on subsequent evaluation, then the result may be unexpected. For example, given int i = 1; j = 2;, the result of max(i,j) is 2. If a and b were only evaluated once, the result of max(i++,j++) would be the same, but with double evaluation the result is 3.

Hidden order of operation

Failure to bracket arguments can lead to unexpected results. For example, a macro to double a value might be written as:

#define double(x) 2 * x

But double(1 + 2) expands to 2 * 1 + 2 which due to order of operations, evaluates to 4 when the expected is 6. To mitigate this problem, a macro should bracket all expressions and substitution variables:

#define double(x) (2 * (x))

Not general purpose

[edit]

The C preprocessor is not Turing-complete, but comes close. Recursive computations can be specified, but with a fixed upper bound on the amount of recursion performed.[23] However, the C preprocessor is not designed to be, nor does it perform well as, a general-purpose programming language. As the C preprocessor does not have features of some other preprocessors, such as recursive macros, selective expansion according to quoting, and string evaluation in conditionals, it is very limited in comparison to a more general macro processor such as m4.

Phase out

[edit]

Due to its limitations and lack of type safety (as the preprocessor is completely oblivious to C/C++ grammar, performing only text substitutions), C and C++ language features have been added over the years to minimize the value and need for the preprocessor.

Constant

For a long time, a preprocessor macro provided the preferred way to define a constant value. An alternative has always been to define a const variable, but that results in consuming runtime memory. A newer language construct (since C++11 and C23), constexpr allows for declaring a compile-time constant value that need not consume runtime memory.[24]

Inline function

For a long time, a function-like macro was the only way to define function-like behavior that did not incur runtime function call overhead. Via the inline keyword and optimizing compilers that inline automatically, some functions can be invoked without call overhead.

Import

The include directive limits code structure since it only allows including the content of one file into another. More modern languages support a module concept that has public symbols that other modules import – instead of including file content. Many contend that resulting code has reduced boilerplate and is easier to maintain since there is only one file for a module; not both a header and a body. C++20 adds modules, and an import statement that is not handled via preprocessing.[25][26] Modules in C++ compile faster and link faster than traditional headers,[27] and eliminate the necessity of #include guards or #pragma once. Until C++26, import, export, and module keywords were partially handled by the preprocessor.

For code bases that cannot migrate to modules immediately, C++ also offers "header units" as a feature, which allows header files to be imported in the same way a module would. Unlike modules, header units may emit macros, offering minimal breakage between migration. Header units are designed to be a transitional solution before totally migrating to modules.[28] For instance, one may write import <string>; instead of #include <string>, or import "MyHeader.hpp"; instead of #include "MyHeader.hpp". Paradoxically, most build systems, such as CMake, do not currently support this feature.

See also

[edit]

References

[edit]
  1. ^ General-purpose text preprocessing with the C preprocessor. Featuring JavaScript
  2. ^ a b "The C Preprocessor: Overview". Retrieved 17 July 2016.
  3. ^ "C# preprocessor directives". Microsoft. 14 January 2022.
  4. ^ Ritchie (1993)
  5. ^ "Bell SAP – SAP with conditional and recursive macros". HOPL: Online Historical Encyclopaedia of Programming Languages. Archived from the original on 14 October 2023. Retrieved 4 October 2020.
  6. ^ "WG14-N3017 : #embed – a scannable, tooling-friendly binary resource inclusion mechanism". open-std.org. 27 June 2022. Archived from the original on 24 December 2022.
  7. ^ "#embed - a scannable, tooling-friendly binary resource inclusion mechanism". open-std.org.
  8. ^ Wirzenius, Lars. C "Preprocessor Trick For Implementing Similar Data Types". Retrieved January 9, 2011
  9. ^ Meyers, Randy (May 2001). "The New C: X Macros". Dr. Dobb's Journal. Retrieved 1 May 2008.
  10. ^ Beal, Stephan (August 2004). "Supermacros". Retrieved 27 October 2008. {{cite journal}}: Cite journal requires |journal= (help)
  11. ^ List of predefined ANSI C and Microsoft C++ implementation macros.
  12. ^ "WG14-N3096 : Draft for ISO/IEC 9899:2023" (PDF). open-std.org. 1 April 2023. Archived (PDF) from the original on 2 April 2023.
  13. ^ "Working Draft, Standard for Programming Language C++" (PDF). 22 March 2023.
  14. ^ GCC Obsolete features
  15. ^ "Wrapper Headers (The C Preprocessor)".
  16. ^ "#import directive (C++)".
  17. ^ "region and endregion pragma".
  18. ^ "1.3 Preprocessing and conditional compilation". GNU Project.
  19. ^ "Using the fpp Preprocessor". Intel. Retrieved 14 October 2015.
  20. ^ "Overview (The C Preprocessor)". gcc.gnu.org. Having said that, you can often get away with using cpp on things which are not C. Other Algol-ish programming languages are often safe (Ada, etc.) So is assembly, with caution. -traditional-cpp mode preserves more white space, and is otherwise more permissive. Many of the problems can be avoided by writing C or C++ style comments instead of native language comments, and keeping macros simple.
  21. ^ Gerard J. Holzmann. "The power of ten - Rules for developing safety critical code" (PDF). safety of macros. p. 4.
  22. ^ Michael D. Ernst; Greg J. Badros; David Notkin (December 2002). "An empirical analysis of c preprocessor use". IEEE Transactions on Software Engineering. 28 (12): 1146–1170. doi:10.1109/TSE.2002.1158288.
  23. ^ "Is the C99 preprocessor Turing complete?". Archived from the original on 24 April 2016.
  24. ^ Gabriel Dos Reis; Bjarne Stroustrup (22 March 2010). "General Constant Expressions for System Programming Languages, Proceedings SAC '10" (PDF). Archived (PDF) from the original on 13 June 2018. Retrieved 8 July 2024.
  25. ^ "N4720: Working Draft, Extensions to C++ for Modules" (PDF). Archived (PDF) from the original on 30 April 2019.
  26. ^ "P1857R1 – Modules Dependency Discovery".
  27. ^ "Overview of modules in C++". Microsoft. 12 February 2022.
  28. ^ "Walkthrough: Build and import header units in Microsoft Visual C++". Microsoft. 12 April 2022.

Sources

[edit]
[edit]
右束支传导阻滞是什么意思 一贫如什么 3月是什么季节 尿道炎吃什么药 1月7号什么星座
多动症是什么原因造成的 解痉是什么意思 高岗为什么自杀 血小板计数高是什么原因 7月初是什么星座
紫藤什么时候开花 蛇什么时候蜕皮 室性早搏是什么意思 梦见蝙蝠是什么意思 东营有什么大学
胆囊壁毛糙吃什么药 喝酒容易醉是什么原因 改善什么 张学友属什么生肖 不可磨灭是什么意思
86年属虎是什么命huizhijixie.com 猎头是干什么的hcv9jop8ns2r.cn 流年不利什么意思zhongyiyatai.com 南京有什么特产可以带回家hcv9jop0ns6r.cn 指甲脱层是什么原因creativexi.com
恢复伤口的鱼叫什么鱼hcv7jop6ns7r.cn 脚为什么会肿hcv8jop4ns4r.cn 甲状腺挂什么科hcv8jop6ns4r.cn 今天突然拉稀拉出血什么原因hcv8jop2ns3r.cn 同房干涩什么原因导致的hcv8jop3ns2r.cn
bu是什么颜色hcv9jop1ns0r.cn 什么食物含维生素b12最多hcv8jop9ns2r.cn 4.19是什么星座xscnpatent.com 空调出现pl是什么意思hcv8jop6ns6r.cn 复出是什么意思hcv9jop7ns2r.cn
美纹纸是干什么用的fenrenren.com 头皮起疙瘩是什么原因hcv8jop6ns7r.cn 妈妈是什么beikeqingting.com 热闹非凡是什么意思hcv9jop4ns5r.cn 艾滋病有什么危害hcv7jop6ns4r.cn
百度