# Thread: Does every expression results into constant value?

1. ## Does every expression results into constant value?

Can you give me examples of expression where expression results into value? 2. Not sure I understand your question at all. An expression in C can be one of many things. There is no requirement that the result is constant.

Even boolean expressions result in numerical results. Something like (0 != 0) will actually result in 0. 3. Originally Posted by MacGyver Not sure I understand your question at all. An expression in C can be one of many things. There is no requirement that the result is constant.

Even boolean expressions result in numerical results. Something like (0 != 0) will actually result in 0.

0!=0 results into constant numerical value Hence the result of expression is constant value. Can you tell me some examples where the result of expression evaluation is not a constant value? 4. I believe what you mean is that the output of the expression is always consistent. Constant means a value cannot be changed. Either way, it's all semantics, and I think I might understand the question now.

For the built-in boolean operators, I believe the output is always guarenteed to be 0 or 1. Nevertheless, such as assumption that you only have to worry about 1 and 0 could bite you later on.

There are functions defined in C that return various non-zero values depending upon other factors. For instance, strcmp() is guarenteed to return 0 if the strings are equal and a non-zero value if the strings in question are not equal. In particular, I believe it is defined to return a negative number if the first string is less than the second, and a positive non-zero number if the reverse condition is true.

If this doesn't answer your question, you need to be more specific. 5. The result of an operation on any non-constant variable. For example:
Code:
```int a, b,c;
...
c = a + b;```
The result is a non-constant value. Try initializing const arrays with the results of function calls, or addresses of pointers. Your compiler should tell you it doesn't result in a constant.

Or again, explain further what it is you're talking about.

Quzah. 6. The output of the expression is always numeric or decimal value? Is there any case where value is different? 7. Ok, I'll take the risk you might not understand one word of what I'm saying, but I'll post it anyway. An "expression" in programming is related to the Formal Grammar of a programming language, or, more specifically the production:

expression -> ...

where "..." depends from one language to another. I'll post an easy example of a formal grammar from a language called "Drei", used at my university in the compilation course:

Code:
```Expression  = [ SumExpression CompOp ] SumExpression

CompOp         = "=="
| "!="
| "<"
| ">"
| "<="
| ">="

SumExpression  = Term
| SumExpression SumOp Term

SumOp          = "+"
| "-"
| "||"

Term           = [ Term ProdOp ] [ NegateOp ] Factor

NegateOp       = "-"
| "!"

ProdOp         = "*"
| "/"
| "&#37;"
| "&&"

Factor         = ident
| number
| string
| "true"
| "false"
| "this"
| "(" Expression ")"
| "{" Statements "return" Expression "}"
| "new" ident Arguments
| Factor "." ident
| Factor "." ident Arguments

Arguments      = "(" Expressions ")"```
As you can see, if you evaluate the productions from expression that you will arrive at the production "factor". Actually, an expression can have ALL the values defined in factor (in this language). It can even take the result from a function, so you could write:

Code:
```char myFunc();

int main ( void )
{
if (myFunc());
}```
Here, we use a char as the expression in the if clause. Now, a char has an integere representation so you could argue that it's a numeric nevertheless.

Do you see now that your question doesn't make any sense ? 8. fun() If this function returns void, can it be the example of the expression which does not return any value? 9. No, because that's not an expression, it's a statement. Just like ; by itself. It's not an expression, it's just a statement.

Quzah. 10. Somewhat offtopic, I wrote a certain post where I discussed the return value of functions on an x86 system. Declaring main() as void did not stop the calling environment from reading off a return value. 11. fun() is a postfix expression as per my understanding 12. The argument would be that functions that return void are not true functions in the mathematical sense because they do not return a value. This is why some languages, particularly like BASIC, that distinguish between a subroutine, which does not return a value, and a function, which does return a value.

All you are doing when you call a function that returns void is change execution of the program to a different location, which eventually returns control back to where you were prior to the jump. Hence, I'm not sure if you could argue that void functions really form a mathematical expression. 13. Originally Posted by forumuser Can you give me examples of expression where expression results into value?
I would say C expressions are state-less.

In an imperative language, you could argue that an expression results in a value.

Although the value of such expressions is relatively questionable (hahahahahahaha) Popular pages Recent additions 