You are on page 1of 2

Is ++i really faster than i = i + 1?

Anyone asking this question does not really know what he is talking about.
Any good compiler will and should generate identical code for ++i, i += 1, and i
= i + 1. Compilers are meant to optimize code. The programmer should not be bot
her about such things. Also, it depends on the processor and compiler you are us
ing. One needs to check the compiler's assembly language output, to see which on
e of the different approcahes are better, if at all.
Note that speed comes Good, well written algorithms and not from such silly tric
ks.
What do lvalue and rvalue mean?
An lvalue is an expression that could appear on the left-hand sign of an assignm
ent (An object that has a location). An rvalue is any expression that has a valu
e (and that can appear on the right-hand sign of an assignment).
The lvalue refers to the left-hand side of an assignment expression. It must alw
ays evaluate to a memory location. The rvalue represents the right-hand side of
an assignment expression; it may have any meaningful combination of variables an
d constants.
Is an array an expression to which we can assign a value?
An lvalue was defined as an expression to which a value can be assigned. The ans
wer to this question is no, because an array is composed of several separate arr
ay elements that cannot be treated as a whole for assignment purposes.
The following statement is therefore illegal:
int x[5], y[5];
x = y;
Additionally, you might want to copy the whole array all at once. You can do so
using a library function such as the memcpy() function, which is shown here:
memcpy(x, y, sizeof(y));
It should be noted here that unlike arrays, structures can be treated as lvalues
. Thus, you can assign one
structure variable to another structure variable of the same type, such as this:
typedef struct t_name
{
char last_name[25];
char first_name[15];
char middle_init[2];
} NAME;
...
NAME my_name, your_name;
...
your_name = my_name;
...
What does the term cast refer to? Why is it used?

Casting is a mechanism built into C that allows the programmer to force the conv
ersion of data types. This may be needed because most C functions are very parti
cular about the data types they process. A programmer may wish to override the d
efault way the C compiler promotes data types.

You might also like