C

What is an lvalue and rvalue in C

Lvalue or location value is an address that is stored into while rvalue is the "read" value, ie., value of a constant or the value stored at a location. In the assignment

a = 31;

we have the lvalue of a used on the left hand side and the integer contant value 31 assigned to this location.

In the assignment

How is a signed negative number stored in memory

Q:How is signed negative numbers stored in memory?
Q: How is -1 represented in memory?

A:
In most intel architectures, signed negative numbers are stored in memory as  two's Complement.
eg.
-1    will be stored as  11111111,
-2    as  11111110,
-3    as 11111101

Embedded C interview Questions for Embedded Systems Engineers

Q: Which is the best way to write Loops?
Q: Is Count Down_to_Zero Loop better than Count_Up_Loops?
A: Always, count Down to Zero loops are better.
This is because,at loop termination, comparison to Zero can be optimized by complier. (Eg using SUBS)
Else, At the end of the loop there is ADD and CMP.

Q: What is loop unrolling?
A: Small loops can be unrolled for higher performance, with the disadvantage of increased
codesize. When a loop is unrolled, a loop counter needs to be updated less often and
fewer branches are executed. If the loop iterates only a few times, it can be fully unrolled,
so that the loop overhead completely disappears.
eg:
int countbit1(uint n)
{ int bits = 0;
while (n != 0)
{
if (n & 1) bits++;
n >>= 1;
}
return bits;
}

int countbit2(uint n)
{ int bits = 0;
while (n != 0)
{
if (n & 1) bits++;
if (n & 2) bits++;
if (n & 4) bits++;
if (n & 8) bits++;
n >>= 4;
}
return bits;
}

Q: How does, taking the address of local variable result in unoptimized code?
A: The most powerful optimization for compliler is register allocation. That is it operates the variable from register, than memory. Generally local variables are allocated in registers. However if we take the address of a local variable, compiler will not allocate the variable to register.

Q: How does global variable result in unoptimized code?
A: For the same reason as above, compiler will never put the global variable into register. So its bad.