Does every expression results into constant value?

This is a discussion on Does every expression results into constant value? within the C Programming forums, part of the General Programming Boards category; Can you give me examples of expression where expression results into value?...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    61

    Does every expression results into constant value?

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

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    61
    Quote Originally Posted by MacGyver View Post
    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. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    61
    The output of the expression is always numeric or decimal value? Is there any case where value is different?

  7. #7
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    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"
                   | "readInt"
                   | "readChar"
                   | "(" 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. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    61
    fun() If this function returns void, can it be the example of the expression which does not return any value?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    61
    fun() is a postfix expression as per my understanding

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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. #13
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    Quote Originally Posted by forumuser View Post
    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)
    we are one

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21