C99 parameter evaluation before C function is called - function

I know that this question is similar to this one, but I feel like I don't fully understand C99 standard. I want to ask about parameter evaluation itself, for example:
int index = 0;
sprintf(somebuf, "some-text-%d", index++);
So, it seems like index is not incremented before function call (I got some-text-0 as a result). Is it expected behavior?

By using the post-increment operator (++ follows index), the value is used first, then incremented. If you wanted to use the incremented value, you should have used the pre-increment operator (++index). FYI, this dates back to the earliest versions of C.

Related

Mysql - transforming non-null values part 2

In this question I asked if Mysql have a function which receives two arguments and returns null if the first is null or the second argument otherwise. Somebody said in the comment section that such function doesn't exist. How can I define this function in Mysql, considering that it may receive arguments of any type and the return value can be null or of the same type of the second parameter? Is it even possible?
This is not possible, given your or requirements.
The arguments and return values of stored functions (written in SQL) and user-defined functions (written in C) are all statically typed.
Granted, MySQL is pretty flexible with implicit casting, but the values are typed nonetheless. Even if you were okay with implicit casting, a scenario where that would be preferable to the seemingly-obvious solution is difficult to imagine.
IF(foo IS NULL,foo,bar) would suffice for the purpose and preserve the underlying types correctly, and IF(foo IS NULL,NULL,bar) would be almost the same thing, though the type of foo would be lost (e.g a "it's a NULL DATETIME").
You have previously rejected those, for reasons that are not intuitively obvious. When a purpose can be accomplished with built-ins, the motivation to reinvent the wheel is hard to understand.
try this:
Select IF(ISNULL(arg1), null, arg2)
Read more about Comparison Functions and Operators and Control Flow Functions.

Why is 'bicrement' not possible like this?

Why is this not possible?
int c = 0;
++c++;
Or in PHP:
$c = 0;
++$c++;
I would expect it to increment the variable c by 2, or perhaps do something weird, but instead it gives an error while compiling. I've tried to come up with a reason but got nothing really... My reasoning was this:
The compiler reads ++
It reads the variable
It does whatever it does to make the value of the variable increment and then get returned when executing the application
It encounters another ++
It uses the previous variable in order to return it before incrementing the value
This is where it gets confusing: does it use the variable c, or does it try to read the value that (++c) returned? Or, since you can only do varname++ (and not 2++), does it see (++c) as a pointer and then tries to read that memory location? Whatever it does, why would it give a compile error? Or is the error preventive, because the compiler's programmer knew it wouldn't do anything useful?
It's not really that I would want to use this, and certainly not for code that is not one-time use only, but I'm just curious.
For the same reason you can't do:
c++ = 5;
It returns a value, which cannot be modified nor assigned to. That's not a runtime error, either - that's a compilation error. (Like this one.)
Returning a reference wouldn't make sense either, because then:
$a = 1;
$b = $a++; // How can it be a reference if b should be 1 and a should be 2?
This isn't necessarily language-agnostic, although I'd be a little surprised to see a language in which it was different.
In C (and hence I assume in all non-annoying languages based on C), the operator precedence means your expression is equivalent to ++(c++). Based on your step-by-step, you were expecting it to be equivalent to (++c)++, but it isn't. Postfix ++ "binds more tightly" than prefix ++.
Like everyone says, the expression c++ results in a value, not a modifiable object (an expression in C that refers to an object is called an lvalue). This is necessary because the object c no longer holds the value that the expression evaluates to -- there is no object that c++ could refer to.
In C++ (not to be confused with c++!), ++c is an lvalue. It refers to the object c, which now has the new value.
So in C++ (++c)++ is syntactically correct. It happens to have undefined behavior if c is of type int (at least, it did in C++03: C++11 made some things defined that used to be undefined and I'm not up to date on those changes).
So, if you imagine (or go ahead and invent) a C++-like language in which the operator precedence is what you expected, then you could arrange for ++c++ to be valid. Presumably it would increment c twice, and evaluate to the value in between the old and new values. This imagined language would be "annoying" in the sense that it's only subtly different from C++, which would tend to be confusing.
++c++ is also valid in C++ if c is an instance of a user-defined type that overloads both increment operators. The reason is that an rvalue of user-defined type is an object (a "temporary object"), and can be modified. But as soon as the expression has been evaluated, the temporary is destroyed and the modification is lost.
Another way to Explain this is that the ++ operator only operates on an lvalue. But when you combine them, it's parsed as either ++(c++) or (++c)++ -- in either case, the parameter to the operator outside the parentheses is an rvalue (c's value before or after the increment), not an lvalue.

Functions in Lua

I am starting to learn Lua from Programming in Lua (2nd edition) I didn't understand the following in the book.
network = {
{name ="grauna", IP="210.26.30.34"},
{name ="araial", IP="210.26.30.23"},
}
If we want to sort the table by field name, the author mentions
table.sort(network, function (a,b) return (a.name > b.name) end }
Whats happening here? What does function (a,b) stand for? Is function a key word or something.
If was playing around with it and created a table order
order={x=1,x=22,x=10} // not sure this is legal
and then did
print (table.sort(order,function(a,b) return (a.x > b.x) end))
I did not get any output. Where am I going wrong?
Thanks
It's an anonymous function that takes two arguments and returns true if the first argument is less than the second argument. table.sort() runs this function for each of the elements that need sorting and compares each element with the previous element.
I think (but I am not sure) that order={x=1,x=22,x=10} has the same meaning in Lua as order={x=10}, a table with one key "x" associated with the value 10. Maybe you meant {{x=1},{x=22},{x=10}} to make an "array" of 3 components, each having the key "x".
To answer the second part of your question: Lua is very small, and doesn't provide a way to print a table directly. If you use a table as a list or array, you can do this:
print(unpack(some_table))
unpack({1, 2, 3}) returns 1, 2, 3. A very useful function.
function in lua is a keyword, similar to lambda in Scheme or Common Lisp (& also Python), or fun in Ocaml, to introduce anonymous functions with closed variables, i.e. closures

Functions are to Arguments as Statements are to ...?

Maybe this question makes little sense, however I feel I need clarification.
Functions and methods have parameters when defined and are called with arguments.
What are the values that are passed into a statement called? By statements I mean conditionals, loops, and the like.
For example:
print 'foo'
print('foo')
PHP treats these roughly the same, Python 3 is now using the function instead of the statement.
What is the relation of 'foo' to the print statement called?
Functions (and Expressions) return/state/deliver answers for questions. If you want to know "What is the sum of 5 and 3" you write an expression "5 + 3" or call a function "add( 5, 3 )". The arguments you pass to the function (or the operands you write in the expression) pose the question and thereby determine the answer.
But programmers have to change the world too - at least the content of the console window and at the end of a long day they want to shutdown the computer. So statements (and subroutines, i.e. named/callable pieces of code) are the means "to do something". A print statement/subroutine will just change the monitor's pixel, but answer no question; a print function will do that (side-effect) and answer "How many characters were written?". Whether an If will branch to here or there, or a While will stop or continue, is determined by the expressions/functions (= information) you put in the syntactically correct places. These arguments to statements determine what is to be done (not what is to be known).
Technically the difference between know and do is blurred. You can have assignment statements that return/deliver the assigned value (to make "a = b = c = 5;" or "while( line = getNextLine() ) {}" possible) or a ternary operator that let you use If in an expression.
But in all cases: The information (arguments/parameters/operands) you feed to the 'knowers' or 'doers' determine the results - so take care!
Statements are usually composed of operators and operands so if print is the operator then I suppose you could call 'foo' the operand.

What is the name of a function whose result depends only on its parameters?

I'm writing a toy compiler thingy which can optimise function calls if the result depends only on the values of the arguments. So functions like xor and concatenate depend only on their inputs, calling them with the same input always gives the same output. But functions like time and rand depend on "hidden" program state, and calling them with the same input may give different output. I'm just trying to figure out what the adjective is that distinguishes these two types of function, like "isomorphic" or "re-entrant" or something. Can someone tell me the word I'm looking for?
The term you are looking for is Pure
http://en.wikipedia.org/wiki/Pure_function
I think it's called Pure Function:
In computer programming, a function may be described as pure if both these statements about the function hold:
The function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change as program execution proceeds or between different executions of the program, nor can it depend on any external input from I/O devices.
Evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices.
The result value need not depend on all (or any) of the argument values. However, it must depend on nothing other than the argument values.
I guess you could say the adjective is "pure" if you go by "pure function".
I always learnt that a function whose output is always the same when the arguments are always the same is called "deterministic". Personally, I feel that that is a more descriptive term. I guess a "pure function" is by definition deterministic, and it seems a pure function is also required to not have any side-effects. I assume that that need not be the case for all deterministic functions (as long as the return value is always the same for the same arguments).
Wikipedia link: http://en.wikipedia.org/wiki/Deterministic_algorithm
Quote:
Given a particular input, it will always produce the same output, and the underlying machine will always pass through the same sequence of states.