Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a "true;" or "10;" statement mean in C++ and how can it be used?

In C++ one can write any of the following statements:

10;
true;
someConstant; //if this is really an integer constant

or something like

int result = obtainResult();
result; // looks totally useless

The latter can be used to suppress a compiler warning "A variable is initialized but not referenced" (C4189 in VC++) if a macro that is expanded into an empty string in some configuration is later used with the result variable. Like this:

int result = obtainResult();
result;
assert( result > 0 ); // assert is often expanded into an empty string in Release versions of code

What's the meaning of such statements? How can they be used except for compiler warning suppression?

like image 979
sharptooth Avatar asked Jul 09 '09 08:07

sharptooth


People also ask

What does true mean in C?

For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true. To make life easier, C Programmers typically define the terms "true" and "false" to have values 1 and 0 respectively.

When if statement is true in C?

C – If statement Syntax of if statement: The statements inside the body of “if” only execute if the given condition returns true. If the condition returns false then the statements inside “if” are skipped.

What is the use of statement in C?

A statement is a command given to the computer that instructs the computer to take a specific action, such as display to the screen, or collect input. A computer program is made up of a series of statements.

What is the use of conditional statement in C?

C has the following conditional statements: Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.


2 Answers

This kind of statements is a logical expansion of how other pieces of the language works. Consider having a function that returns a value, for example int foo(), that also has some side effects. Sometimes you only want those side effects to happen, so you write foo(); as a statement.

Now, while this does not look exactly like 10;, the function call will evaluate to an int sooner or later, and nothing happens to that int, just like with 10;.

Another example of the same issue is that since you can do a = b = 10;, that means b = 10 has to evaluate to 10, hence you can not do assignment without generating a value that has to be suppressed.

Being able to write such values as statements is just a logical way of building the language, but for the cases you present it might even be a good idea to give a compiler warning for it.

Unless you use it to suppress compiler warnings ;)

like image 102
Magnus Hoff Avatar answered Sep 27 '22 21:09

Magnus Hoff


These statements (called expression-statements in the C++ grammar) are valid because they are expressions.

Expressions are all constructs that calculate some kind of value, such as

  • 3 + 5
  • someVariable
  • someFunctionCall( 2 )
  • someVar += 62
  • val > 53

I think, to keep the grammar simple, they decided to not differentiate between those expressions that actually have a side effect (such as the function call or the assignment) and those that don't.

like image 32
Timbo Avatar answered Sep 27 '22 22:09

Timbo