C Inetrview Question and Answer Set-4

| Thursday 9 June 2011

Null Statements

4.1: What is this infamous null statement, anyway?

A null statement is an expression statement consisting solely of the terminating semicolon. The optional expression is dropped. It can be distinguished from any other statement by byte count or study of side-effects.

4.2: How do I ``get'' a null statement in my programs?

In ANSI C, there are six types of statements; labeled statements, compound statements, expression-statements, selection statements, iteration statements, and jump statements. All of them, except the jump and expression statments, are defined in terms of optional preceeding text, and other statements. The jump statements are never null statements. An expression statement is considered to be ``a null statement'' if the optional expression part of it has been left out. A null statement can appear on its own, or (most frequently) as the statement body of an iteration statement. These two null statements are equivalent, though neither of them is equivalent to any non-null statement. [*]
You may accidentally get a null statement by deleting the body of a non-null statement.
[*] Actually, they are functionally equivalent to a large set of non-null statements, namely, those with no side-effects. However, the FDA has yet to approve any such, as their lack of side effects is conjectured, and not clinically proven. This applies only to the ANSI standard, and not the ISO standard, as the FDA has no jurisdiction outside the U.S.

4.3: Is there more than one null statement?

Sort of. You can use ``;'', ``0;'', or ``1;'' - they will all act like a null statement. Only the first is a ``true'' null statement (all bits zero). They are basically equivalent. Note that (void *) 0; is a null statement of type pointer to void, for instance.

4.4: But I thought { } was a null statement!

No. { statement-list[opt] } is a compound statement. An empty block is not the same as a null statement, however, although it can be used in many of the same places. It's really a null block. (You can convert it with a cast, but it's not directly compatible. For instance, you can't use a null block as one of the controlling statements of a for loop.)

4.5: I use the statement #define NULLSTMT(F) (F) ; to allow me to cast a null statement to an appropriate type.

This trick, though popular in some circles, does not buy much. The resulting code is invalid, and will not compile. This (in the author's opinion) outweighs any arguable type consistency. It may be more common in industrial code. If it becomes common practice, C++ will probably legalize it.

4.6: I use the statement #define NULLSTMT(F) (F) 0; to allow me to cast a null statement to an appropriate type.

This trick will likely work, but think: what does it really buy you? Mostly, it will indicate to even the most casual observer that you are shakey on the concept of null statements, making it harder for them to check your code.

4.7: But wouldn't it be better to use ; (rather than 0;) in case the value of 0 changes, perhaps on a machine with nonzero no-op instructions?

No. The '0' of '0;' is not evaluated as an instruction, rather, it is just ignored. The only reason to use '0;' instead of ';' is to help keep 1-heavy code properly balanced (in C, which uses binary representations for numbers, it is possible for code to become unbalanced; an unbalanced binary tree is a common source of poor performance.

4.8: Is a null statement a null pointer?

No. A null pointer is a pointer where all of the address bits are zero (no matter what the segment bits are), and can be obtained by typing '(char *) (int) 0'. A null statement is not a pointer to anything. They are not interchangeable, although you can combine them to get an effectively-null statement, such as NULL;. This does not buy you anything.

4.9: I'm still confused. I just can't understand all this null statement stuff.

Follow these two simple rules:
1.      When you don't want to do anything in source code, don't write it.
2.      If you need a null statement to round out an expression, use an unadorned ; to provide it.
3.      Send large donations, checks, and money orders to the author of the FAQ, or the moderator of the group, whichever you prefer. Then, cross the top question off the FAQ, answer the question at the bottom, and mail it to three people. Within two weeks, you will receive 729 answers to various questions! Do not break the chain; Emily Postnews broke the chain, and now no one listens to her. 

0 comments:

Post a Comment

Popular Posts

Company Placement Papers

 

Copyright © 2010 All Question Papers Blogger Template by Dzignine