C questions

S

sophia.agnes

Dear all,

I was going through a C book written by an indian author
the following are the questions given in that book

1) consider the following statement:
s1 : an operator may require an lvalue operand, yet yield an r
value
s2: an operator may accept an rvalue operand , yet gives an lvalue

which of the following is true about s1 and s2

(a) both s1 and s2 are true
(b) only s1 correct
(c) only s2 is correct
(d) both are false

answer is given as (a)

& is one such operator
int n;
*n is lvalue
&n is rvalue

operator operand result
& n is l value &n rvalue

* (p + 1) is r value *(p + 1) lvalue


2) which of the following is false

a) a string has type array of characters
b) a string has storage class static
c) adjacent string literals are concatenated into a single string
d) Concatenation of ordinary and wide string literal is a single
string

answer is given as d option

3)consider the following

process1(int counter)
{

if(counter == 1)
{
char buf[80];
--------------
}
}

process2(int counter)
{
char buff[80];
if(counter == 1)
{
--------
}

}

which one of the following is true

(a) both process require same stack space in all cases
(b) process 1 require more stack space
(c) process 2 require more stack space
(d) stack space for process is not allocated if counter == 1

answer is given as a option

4) consider the following statements

s1: evaluating the address of an object value after indirection is
simply object
s2:evaluating an object value from indirecting after taking its
address is not the object

which of the following is true
(a) only s1
(b) both are correct
(c) only s2 is correct
(d) both are wrong

answer is given as b option

5)which operation require more memory access
(a) branch
(b) conditional code test
(c) shift register right /* what is this ?*/
(d) all are same

answer is given as d option

well what do you folks think of all this stuff ?
 
H

Harald van Dijk

2) which of the following is false

a) a string has type array of characters
b) a string has storage class static
c) adjacent string literals are concatenated into a single string
d) Concatenation of ordinary and wide string literal is a single string

answer is given as d option

It's possible that it used to be right in the previous version of C -- I
am not sure about that -- but now, this is wrong. Concatenation of a
narrow and a wide string literal is allowed, and produces one single wide
string.
3)consider the following

process1(int counter)
{

if(counter == 1)
{
char buf[80];
--------------
}
}

process2(int counter)
{
char buff[80];
if(counter == 1)
{
--------
}

}

which one of the following is true

(a) both process require same stack space in all cases
(b) process 1 require more stack space
(c) process 2 require more stack space
(d) stack space for process is not allocated if counter == 1

This is implementation-specific; some implementations may always reserve
memory for objects with block scope, but there is no requirement for them
to do so.
4) consider the following statements

s1: evaluating the address of an object value after indirection is
simply object
s2:evaluating an object value from indirecting after taking its address
is not the object

I don't understand these statements. When x is an object, *&x is
completely equivalent to x, and when x is a pointer, &*x is equivalent to
x, except that it is not an lvalue.
5)which operation require more memory access
(a) branch
(b) conditional code test
(c) shift register right /* what is this ?*/
(d) all are same

This is, again, specific to the implementation.
 
R

Richard Heathfield

(e-mail address removed) said:
Dear all,

I was going through a C book written by an indian author
the following are the questions given in that book

1) consider the following statement:
s1 : an operator may require an lvalue operand, yet yield an r
value

Without actually doing an exhaustive study, I'm pretty sure this is false.
s2: an operator may accept an rvalue operand , yet gives an lvalue

This can indeed be true, although it need not be. (Operators are not
required to yield lvalues, but there are circumstances in which they do.)
which of the following is true about s1 and s2

(a) both s1 and s2 are true
(b) only s1 correct
(c) only s2 is correct
(d) both are false

answer is given as (a)

& is one such operator
int n;
*n is lvalue

Close. n is an object. *n is illegal.
&n is rvalue

& can take the name of a function, which is far from being an lvalue! I
can't think of any operator that /requires/ an lvalue operand.


operator operand result
& n is l value &n rvalue

* (p + 1) is r value *(p + 1) lvalue


2) which of the following is false

a) a string has type array of characters

False. A string is a data format. It doesn't have a type. It *can* be
stored in an array of char, but equally it can be stored in an area of
memory allocated by malloc.
b) a string has storage class static

Objects have storage classes. A string is not an object per se, but a
collection of contiguous objects.

c) adjacent string literals are concatenated into a single string

This, at least, is true.
d) Concatenation of ordinary and wide string literal is a single
string

False - the behaviour is undefined.
answer is given as d option

See above.
3)consider the following

process1(int counter)
{

if(counter == 1)
{
char buf[80];
--------------
}
}

process2(int counter)
{
char buff[80];
if(counter == 1)
{
--------
}

}

which one of the following is true

(a) both process require same stack space in all cases
(b) process 1 require more stack space
(c) process 2 require more stack space
(d) stack space for process is not allocated if counter == 1

answer is given as a option

All the answers incorrectly assume the presence of a stack.
4) consider the following statements

s1: evaluating the address of an object value after indirection is
simply object
s2:evaluating an object value from indirecting after taking its
address is not the object

which of the following is true
(a) only s1
(b) both are correct
(c) only s2 is correct
(d) both are wrong

answer is given as b option

I can't parse the s1 and s2 statements. I would say that both need
rewriting for comprehensibility.
5)which operation require more memory access
(a) branch
(b) conditional code test
(c) shift register right /* what is this ?*/
(d) all are same

answer is given as d option

The Standard doesn't say.
well what do you folks think of all this stuff ?

I think it's a large heap of oh my goodness, is that the time?
 
J

jacob navia

Richard said:
Richard Tobin said:


Well, there ya go - it shows that I didn't think very hard...

Of course, it won't take just any old lvalue. It requires a modifiable
lvalue that is not a structure or union.

unless it is overloaded...

:)
 
R

Richard Heathfield

Richard Tobin said:

Well, there ya go - it shows that I didn't think very hard...

Of course, it won't take just any old lvalue. It requires a modifiable
lvalue that is not a structure or union.
 
E

Eric Sosman

Dear all,

I was going through a C book written by an indian author
the following are the questions given in that book

1) consider the following statement:
s1 : an operator may require an lvalue operand, yet yield an r
value
s2: an operator may accept an rvalue operand , yet gives an lvalue

which of the following is true about s1 and s2

(a) both s1 and s2 are true
(b) only s1 correct
(c) only s2 is correct
(d) both are false

answer is given as (a)

& is one such operator
int n;
*n is lvalue
&n is rvalue

operator operand result
& n is l value &n rvalue

* (p + 1) is r value *(p + 1) lvalue

The answer is correct, but the explanation seems at best
"confused." In particular, `*n' is not an lvalue, but an
error requiring a diagnostic. Also `(p + 1)' is not always
an rvalue (consider `void *p'), and `*(p + 1)' is not always
an lvalue (consider `int *p = &n').
2) which of the following is false

a) a string has type array of characters
b) a string has storage class static
c) adjacent string literals are concatenated into a single string
d) Concatenation of ordinary and wide string literal is a single
string

answer is given as d option

Certainly (b) is also false.
3)consider the following

process1(int counter)
{

if(counter == 1)
{
char buf[80];
--------------
}
}

process2(int counter)
{
char buff[80];
if(counter == 1)
{
--------
}

}

which one of the following is true

(a) both process require same stack space in all cases
(b) process 1 require more stack space
(c) process 2 require more stack space
(d) stack space for process is not allocated if counter == 1

answer is given as a option

All four answers are false (or perhaps "undecidable") from
the point of view of the C language, although one or more may
be true of particular implementations of C.
4) consider the following statements

s1: evaluating the address of an object value after indirection is
simply object
s2:evaluating an object value from indirecting after taking its
address is not the object

which of the following is true
(a) only s1
(b) both are correct
(c) only s2 is correct
(d) both are wrong

answer is given as b option

I don't know, because I cannot make sense of either s1 or s2.
5)which operation require more memory access
(a) branch
(b) conditional code test
(c) shift register right /* what is this ?*/
(d) all are same

answer is given as d option

As in #3, the question is meaningless as far as the C
language goes, but may have meaning for some implementations.
well what do you folks think of all this stuff ?

Not much. I've seen worse, but I've seen much better.
 
J

jameskuyper

Richard said:
(e-mail address removed) said:


Without actually doing an exhaustive study, I'm pretty sure this is false.
a=3

....
& can take the name of a function, which is far from being an lvalue! I
can't think of any operator that /requires/ an lvalue operand.

The increment, decrement, and assignment operators all require an
lvalue operands.

....
False - the behaviour is undefined.

C99 Section 6.4.5p4:
"In translation phase 6, the multibyte character sequences specified
by any sequence of adjacent character and wide string literal tokens
are concatenated into a single multibyte character sequence. If any of
the tokens are wide string literal tokens, the resulting multibyte
character sequence is treated as a wide string literal; otherwise, it
is treated as a character string literal."
 
S

sophia.agnes

I don't understand these statements. When x is an object, *&x is
completely equivalent to x, and when x is a pointer, &*x is equivalent to
x, except that it is not an lvalue.
answer is given as follows:-

consider the following program

main()
{
char x[] = "abcde";
printf("%d %d", x+3,&*x+3);
printf("%d %d", x+3,*&(x + 3));

}

x = (*(x + i));
&x = &(*(x+i));

C precedence rule evaluate & and * from right to left, so we can
remove the outer parenthesis i.e
&x = &*(x + i) reduced to &x = (x+i)
 
C

Chris Torek

... I can't think of any operator that /requires/ an lvalue operand.

All of the assignment operators (e.g., +=), and the pre- and
post-fix ++ and -- operators, require at least one lvalue operand.

(As you note, unary & requires an lvalue only when it is applied
to objects, since function designators are not lvalues.)
 
B

Ben Bacarisse

Richard Heathfield said:
(e-mail address removed) said:


Without actually doing an exhaustive study, I'm pretty sure this is false.


This can indeed be true, although it need not be. (Operators are not
required to yield lvalues, but there are circumstances in which they do.)


Close. n is an object. *n is illegal.


& can take the name of a function, which is far from being an lvalue! I
can't think of any operator that /requires/ an lvalue operand.

The operators ++, -- and the assignment operators all require an
lvalue operand (albeit a modifiable one).
False. A string is a data format. It doesn't have a type. It *can* be
stored in an array of char, but equally it can be stored in an area of
memory allocated by malloc.


Objects have storage classes. A string is not an object per se, but a
collection of contiguous objects.

That is true, but there is a grain of truth in there. The standard
says that the array initialised from the characters in the string
literal has "static storage duration".
This, at least, is true.


False - the behaviour is undefined.

It seems to be defined in C99. The result is a treated as single wide
string literal.
 
R

Richard Tobin

& is one such operator
int n;
*n is lvalue

Close. n is an object. *n is illegal.
&n is rvalue

& can take the name of a function, which is far from being an lvalue![/QUOTE]

Though you are of course right about that, the answer does come close
to the *idea* of lvalues. The idea of an lvalue is that it's
something on the left-hand side of an assignment - something that can
be assigned to - a place rather than a value. The ability to take the
address of something clearly implies that it is a place.

The fact that you can't say

int foo(void) {...}
int bar(void) {...}
....
foo = bar;

is not really any different from the situation with

int foo[10];
int bar[10];
....
foo = bar;

so there's no real reason why function names should not be considered
lvalues just as array names are.

-- Richard
 
R

Richard Heathfield

(e-mail address removed) said:
C99 Section 6.4.5p4:
"In translation phase 6, the multibyte character sequences specified
by any sequence of adjacent character and wide string literal tokens
are concatenated into a single multibyte character sequence. If any of
the tokens are wide string literal tokens, the resulting multibyte
character sequence is treated as a wide string literal; otherwise, it
is treated as a character string literal."

Thanks for the correction. I did actually check this while composing my
reply, but only in C89 (which is more conveniently searchable for me). C99
has changed the rules. For those few diehards (such as the Microsoft guys,
for instance) who *still* don't have a C99-conforming implementation and
have to make do with C89/90/95, the behaviour remains undefined.
 
R

Randy Howard

C99
has changed the rules. For those few diehards (such as the Microsoft guys,
for instance) who *still* don't have a C99-conforming implementation and
have to make do with C89/90/95, the behaviour remains undefined.

Microsoft's is by no means the only compiler where "diehards" are stuck
with C89 and friends. gcc may /pretend/ to be a c99 compiler, but most
are not fooled.
 
B

Ben Bacarisse

Randy Howard said:
Microsoft's is by no means the only compiler where "diehards" are stuck
with C89 and friends.

The needle on my irony detector was bent by Richard Heathfield's
message. Did yours not flicker?
 
C

Chris Hills

Randy said:
Microsoft's is by no means the only compiler where "diehards" are stuck
with C89 and friends. gcc may /pretend/ to be a c99 compiler, but most
are not fooled.

Actually most compilers are not full C99 implementations. It depends
weather they are C95+ or C99- as to how they react.
 
R

Randy Howard

The needle on my irony detector was bent by Richard Heathfield's
message. Did yours not flicker?

Yeah, but the non-regular, non-indoctrinated may find it confusing,
just as they did when he questioned dmr on topicality.
 
J

jameskuyper

Randy said:
Microsoft's is by no means the only compiler where "diehards" are stuck
with C89 and friends. gcc may /pretend/ to be a c99 compiler, but most
are not fooled.

For the purposes of this question, gcc's other flaws are irrelevant;
the thing that matters is whether it handles concatenation of narrow
and wide character literals correctly, and according to my tests it
does.
 
T

Thad Smith

Ben said:
That is true, but there is a grain of truth in there. The standard
says that the array initialised from the characters in the string
literal has "static storage duration".

Yes, but the statement was about a string, not a string literal.
 
R

Ravishankar S

Dear all,

I was going through a C book written by an indian author
the following are the questions given in that book

Would be interesting for me to know which book this is ?
1) consider the following statement:
s1 : an operator may require an lvalue operand, yet yield an r
value

To be specific , the question applies to unary operators only ? Then the &
operator is one such:
&n is a an rvalue. For a binary operators I can think of a == b
2) which of the following is false

a) a string has type array of characters /* it is meant string literals ?*/
b) a string has storage class static /* it is meant string
literals have internal linkage */
c) adjacent string literals are concatenated into a single string
d) Concatenation of ordinary and wide string literal is a single
string

answer is given as d option
3)consider the following

process1(int counter)
{

if(counter == 1)
{
char buf[80];
--------------
}
}

process2(int counter)
{
char buff[80];
if(counter == 1)
{
--------
}

}

which one of the following is true

(a) both process require same stack space in all cases
(b) process 1 require more stack space
(c) process 2 require more stack space
(d) stack space for process is not allocated if counter == 1

answer is given as a option
most architectures use something like a stack for automatic storage.
but i am not sure if this answer is correct..

4) consider the following statements

s1: evaluating the address of an object value after indirection is
simply object

s2:evaluating an object value from indirecting after taking its
address is not the object

*&(x) == x and &*(x) != x
Is this what is meant by the complicated language ??

which of the following is true
(a) only s1
(b) both are correct
(c) only s2 is correct
(d) both are wrong

answer is given as b option

5)which operation require more memory access
(a) branch
(b) conditional code test
(c) shift register right /* what is this ?*/
(d) all are same

answer is given as d option
Not a C question. But since none of the instructions are load-store, they
dont do memory access,
the answer is (d).
well what do you folks think of all this stuff ?
Good for embedded programming job interview in india :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top