(ubuntu)在andorid andk工程中使用ccache加速编译速度 (Repost)
Designated Initializer & C++

Cpp Memo part 5 -- Conditionals

tubo posted @ 2014年10月10日 23:05 in 未分类 , 1306 阅读
Cpp Memo part 5 – Conditionals

1 Prewords

A "conditional" is a directive that instructs the preprocessor to select whether or not to include a chunk of code in the final token stream passed to the compiler. Preprocessor conditionals can:

  • Test arithmetic expressions, or
  • Test whether a name is defined as a macro, or
  • both simultaneously using the special `defined' operator.

A conditional in the C preprocessor resembles in some ways an `if' statement in C, but it is important to understand the difference between them.

  • If statement:

    The condition in an `if' statement is tested during the execution of your program. Its purpose is to allow your program to behave differently from run to run, depending on the data it is operating on.

  • If directive

    The condition in a preprocessing conditional directive is tested when your program is compiled. Its purpose is to allow different code to be included in the program depending on the situation at the time of compilation.

2 Conditional Uses

There are three general reasons to use a conditional.

  • A program may need to use different code depending on the machine or operating system it is to run on.

    In some cases the code for one operating system may be erroneous on another operating system; for example, it might refer to data types or constants that do not exist on the other system. When this happens, it is not enough to avoid executing the invalid code. Its mere presence will cause the compiler to reject the program. With a preprocessing conditional, the offending code can be effectively excised from the program when it is not valid.

  • You may want to be able to compile the same source file into two different programs.

    One version might make frequent time-consuming consistency checks on its intermediate data, or print the values of those data for debugging, and the other not.

  • A conditional whose condition is always false is one way to exclude code from the program but keep it as a sort of comment for future reference.

Simple programs that do not need system-specific logic or complex debugging hooks generally will not need to use preprocessing conditionals.

3 Conditional Syntax

A conditional in the C preprocessor begins with a "conditional directive": `#if', `#ifdef' or `#ifndef'.

3.1 Ifdef

The simplest sort of conditional is

#ifdef MACRO


#endif /* MACRO */

This block is called a "conditional group". CONTROLLED TEXT will be included in the output of the preprocessor if and only if MACRO is defined. We say that the conditional "succeeds" if MACRO is defined, "fails" if it is not.

Sometimes you wish to use some code if a macro is not defined. You can do this by writing `#ifndef' instead of `#ifdef'. One common use of `#ifndef' is to include code only the first time a header file is included.

#ifndef _TEST_H_
#define _TEST_H_

// #ifdef __cplusplus
// extern "C" {
// #endif

// #ifdef __cplusplus
// }
// #endif

#endif /* _TEST_H_ */

3.2 If

The `#if' directive allows you to test the value of an arithmetic expression, rather than the mere existence of one macro. Its syntax is



#endif /* EXPRESSION */

EXPRESSION is a C expression of integer type, subject to stringent restrictions. It may contain

  • Integer constants.
  • Character constants, which are interpreted as they would be in normal code.
  • Arithmetic operators for addition, subtraction, multiplication, division, bitwise operations, shifts, comparisons, and logical operations (`&&' and `||'). The latter two obey the usual short-circuiting rules of standard C.
  • Macros. All macros in the expression are expanded before actual computation of the expression's value begins.
  • Uses of the `defined' operator, which lets you check whether macros are defined in the middle of an `#if'.
  • Identifiers that are not macros, which are all considered to be the number zero. This allows you to write `#if MACRO' instead of `#ifdef MACRO', if you know that MACRO, when defined, will always have a nonzero value. Function-like macros used without their function call parentheses are also treated as zero.

    In some contexts this shortcut is undesirable. The `-Wundef' option causes GCC to warn whenever it encounters an identifier which is not a macro in an `#if'.

The preprocessor does not know anything about types in the language. Therefore, `sizeof' operators are not recognized in `#if', and neither are `enum' constants. They will be taken as identifiers which are not macros, and replaced by zero. In the case of `sizeof', this is likely to cause the expression to be invalid.

3.3 Defined

The special operator `defined' is used in `#if' and `#elif' expressions to test whether a certain name is defined as a macro. `defined NAME' and `defined (NAME)' are both expressions whose value is 1 if NAME is defined as a macro at the current point in the program, and 0 otherwise. Thus, `#if defined MACRO' is precisely equivalent to `#ifdef MACRO'.

`defined' is useful when you wish to test more than one macro for existence at once. For example,

#if defined (__vax__) || defined (__ns16000__)

would succeed if either of the names `__vax__' or `__ns16000__' is defined as a macro.

Conditionals written like this:

#if defined BUFSIZE && BUFSIZE >= 1024

can generally be SIMPLIFIED to just `#if BUFSIZE >= 1024', since if `BUFSIZE' is not defined, it will be interpreted as having the value zero .

3.4 Else

The `#else' directive can be added to a conditional to provide alternative text to be used if the condition fails. This is what it looks like:

#else /* Not EXPRESSION */
#endif /* Not EXPRESSION */

If EXPRESSION is nonzero, the TEXT-IF-TRUE is included and the TEXT-IF-FALSE is skipped. If EXPRESSION is zero, the opposite happens.

You can use `#else' with `#ifdef' and `#ifndef', too.

3.5 Elseif

One common case of nested conditionals is used to check for more than two possible alternatives. For example, you might have

#if X == 1
#else /* X != 1 */
#if X == 2
#else /* X != 2 */
#endif /* X != 2 */
#endif /* X != 1 */

Another conditional directive, `#elif', allows this to be abbreviated as follows:

#if X == 1
#elif X == 2
#else /* X != 2 and X != 1*/
#endif /* X != 2 and X != 1*/

`#elif' stands for "else if". Like `#else', it goes in the middle of a conditional group and subdivides it; it does not require a matching `#endif' of its own. Like `#if', the `#elif' directive includes an expression to be tested. The text following the `#elif' is processed only if the original `#if'-condition failed and the `#elif' condition succeeds.

More than one `#elif' can go in the same conditional group. Then the text after each `#elif' is processed only if the `#elif' condition succeeds after the original `#if' and all previous `#elif' directives within it have failed.

`#else' is allowed after any number of `#elif' directives, but `#elif' may not follow `#else'.

4 Deleted Code

If you replace or delete a part of the program but want to keep the old code around for future reference, you often cannot simply comment it out. Block comments do not nest, so the first comment inside the old code will end the commenting-out. The probable result is a flood of syntax errors.

One way to avoid this problem is to use an always-false conditional instead. For instance, put `#if 0' before the deleted code and `#endif' after it. This works even if the code being turned off contains conditionals, but they must be entire conditionals (balanced `#if' and `#endif').

celeb networth 说:
2021年9月21日 01:28

I learned a lot from the insight you shared here. It's good to learn more about this topic, and if you have some free time or you're curious about some celebrity basic information, you can visit celebrity networth and search for it.

TBSE HS Question Pap 说:
2022年8月17日 02:47

The Previous Question Paper for the HS exam for the year 2023 has been released by the Board Of Higher Secondary Education TBSE Higher, Government of Tripura. The investigation The department provides classes in the fields of humanities, arts, science, and business. The Tripura Board will make available the Tripura 12 Question Paper 2023, the TBSE 12th Question Paper 2023, the TBSE HS Question Paper 2023, and the Tripura TBSE HS 12th Exam Guess Paper 2023. TBSE HS Question Paper 2023 All students who have registered for the examinations in 2023 may get the Tripura TBSE HS 12th Exam New Model Paper 2023 by going to the official website or by visiting our website.

milan 说:
2023年1月11日 08:00

In programming, a conditional is a statement that controls the flow of peter veres cancer help execution based on a certain condition. This post explains the details about Cpp Memo part 5 Conditionals. The table of contents and its details are explained here in detail. Thank you so much for sharing the details here and keep sharing more updates on that.

meidir 说:
2023年6月20日 02:50

You made some respectable points there. I seemed on the web for the problem and found most people will go along with with your website. 花藝師

meidir 说:
2023年6月28日 06:43

Aw, it was a very good post. In concept I have to put in place writing similar to this additionally – taking time and actual effort to create a good article… but so what can I say… I procrastinate alot by no indicates often get something completed. cosmetics






We stumbled over here by a different web page and thought I should check things out. I like what I see so now i’m following you. Look forward to looking over your web page yet again. makeup






Thanks for taking the time to discuss this, I feel strongly about it and really like learning more on this matter. If possible, as you gain expertise, would you mind updating your blog page with more data? It’s extremely helpful for me. beauty






Heya i would really love to subscribe and read your blog posts .! skincare






After study a handful of the websites with your website now, and I truly as if your strategy for blogging. I bookmarked it to my bookmark website list and will be checking back soon. Pls consider my web site likewise and make me aware if you agree. lotions






This is really fascinating, You are an overly skilled blogger. Ive joined your rss feed and stay up for looking for more of your great post. Also, Ive shared your website in my social networks! beauty






I am sorry, that has interfered… I understand this question. It is possible to discuss. style






Heya i am for the first time here. I found this board and I find It really useful & it helped me out much. I hope to give something back and aid others like you helped me. cosmetics






Getting a issue with the Feed, could you help me? beauty






Many thanks for placing up this post, I feel every person will thanks for that. cologne

登录 *

loading captcha image...
or Ctrl+Enter