Home » , » C-Interview Questions for freshers

C-Interview Questions for freshers

9. What is an lvalue?

An lvalue is an expression to which a value can be assigned. The lvalue expression is located on the left side of an assignment statement, whereas an rvalue is located on the right side of an assignment statement. Each assignment statement must have an lvalue and an rvalue. The lvalue expression must reference a storable variable in memory. It cannot be a constant. For instance, the following lines show a few examples of lvalues:

int x;
int* p_int;
x = 1;
*p_int = 5;
The variable x is an integer, which is a storable location in memory. Therefore, the statement x = 1 qualifies x to be an lvalue. Notice the second assignment statement, *p_int = 5. By using the * modifier to reference the area of memory that p_int points to, *p_int is qualified as an lvalue. In contrast, here are a few examples of what would not be considered lvalues:

#define CONST_VAL 10
int x;
/* example 1 */
1 = x;
/* example 2 */
CONST_VAL = 5;
In both statements, the left side of the statement evaluates to a constant value that cannot be changed because constants do not represent storable locations in memory. Therefore, these two assignment statements do not contain lvalues and will be flagged by your compiler as errors.

10. Can an array be an lvalue?

Is an array an expression to which we can assign a value? The answer to this question is no, because an array is composed of several separate array elements that cannot be treated as a whole for assignment purposes. The following statement is therefore illegal:

int x[5], y[5];

x = y;

You could, however, use a for loop to iterate through each element of the array and assign values individually, such as in this example:

int i;
int x[5];
int y[5];
...
for (i=0; i<5; i++)
     x[i] = y[i]
...
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;
...
In the preceding example, the entire contents of the my_name structure were copied into the your_name structure. This is essentially the same as the following line:

memcpy(your_name, my_name, sizeof(your_name));

11. What is an rvalue?

rvalue can be defined as an expression that can be assigned to an lvalue. The rvalue appears on the right side of an assignment statement.

Unlike an lvalue, an rvalue can be a constant or an expression, as shown here:

int x, y;
x = 1;               /* 1 is an rvalue; x is an lvalue */
y = (x + 1);         /* (x + 1) is an rvalue; y is an lvalue */
An assignment statement must have both an lvalue and an rvalue. Therefore, the following statement would not compile because it is missing an rvalue:

int x; x = void_function_call() /* the function void_function_call() returns nothing */
If the function had returned an integer, it would be considered an rvalue because it evaluates into something that the lvalue, x, can store.



cyber security jobs,post jobs for free,jobs with a psychology degree,medical coding jobs, business administration jobs,hr interview questions and answers for freshers,hospitality management jobs, information technology jobs,data analyst job description,web design jobs,computer science jobs,cyber security jobs,post jobs for free,jobs with a psychology degree,medical coding jobs,
Share this article :

0 comments:

Post a Comment

 
Copyright © 2013. Freshers Job List - All Rights Reserved