Home Interview Questions and AnswersTechnical Interview Questions and AnswersC Programming C Language Basics Interview Questions and Answers For Freshers Part-3

c language8. What is the difference between goto and long jmp( ) and setjmp()?

A goto statement implements a local jump of program execution, and the longjmp() and setjmp() functions implement a nonlocal, or far, jump of program execution. Generally, a jump in execution of any kind should be avoided because it is not considered good programming practice to use such statements as goto and longjmpin your program.

A goto statement simply bypasses code in your program and jumps to a predefined position. To use the gotostatement, you give it a labeled position to jump to. This predefined position must be within the same function. You cannot implement gotos between functions. Here is an example of a goto statement:

void
bad_programmers_function(
void
)

{

int
x;

printf(
“Excuse me while I count to 5000…\n”
);

x =
1
;

while
(
1
)

{

printf(
“%d\n”
, x);

if
(x ==
5000
)

goto
all_done;

else

x = x +
1
;

}

all_done:

printf(
“Whew! That wasn’t so bad, was it?\n”
);

}

This example could have been written much better, avoiding the use of a goto statement. Here is an example of an improved implementation:

void
better_function(
void
)

{

int
x;

printf(
“Excuse me while I count to 5000…\n”
);

for
(x=
1
; x<=
5000
; x++)

printf(
“%d\n”
, x);

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 xto 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]

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.

You may also like

Leave a Comment