Can you give me examples of expression where expression results into value?

Printable View

- 04-13-2007forumuserDoes every expression results into constant value?
Can you give me examples of expression where expression results into value?

- 04-13-2007MacGyver
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. - 04-13-2007forumuser
- 04-13-2007MacGyver
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. - 04-13-2007quzah
The result of an operation on any non-constant variable. For example:

Code:`int a, b,c;`

...

c = a + b;

*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. - 04-16-2007forumuser
The output of the expression is always numeric or decimal value? Is there any case where value is different?

- 04-16-2007KONI
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 = "*"

| "/"

| "%"

| "&&"

Factor = ident

| number

| string

| "true"

| "false"

| "this"

| "readInt"

| "readChar"

| "(" Expression ")"

| "{" Statements "return" Expression "}"

| "new" ident Arguments

| Factor "." ident

| Factor "." ident Arguments

Arguments = "(" Expressions ")"

Code:`char myFunc();`

int main ( void )

{

if (myFunc());

}

Do you see now that your question doesn't make any sense ? - 04-16-2007forumuser
fun() If this function returns void, can it be the example of the expression which does not return any value?

- 04-16-2007quzah
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. - 04-17-2007MacGyver
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.

- 04-17-2007forumuser
fun() is a postfix expression as per my understanding

- 04-18-2007MacGyver
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. - 04-18-2007wiiire