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

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 :)

The book name is Advanced test in C
and
Embedded System Programming !!!!!!!!!!!

by Ashok .K pathak

some more questions from the book

state whether true or false

1) system can't determine size of the stack before program runs
answer is given as true

2) location of constant string in memory(text area or data area)
may be compiler dependent

answer is given as true
string may be placed in data area of string area depending on
implementation

3)in all cases printf(ptr); and printf("%s",ptr); will print the same
string,where ptr points to the address of the string

answer is given as false
printf(ptr); fails if ptr contains %
ex:- printf("a%cb"),printf("%s","a%cb"); will print different
strings

4) which of the following is false

(a) a reentrant function may not use variable in a non atomic way
unless they are stored on the stack of the task that called the
function or are other wise the private variable of that task

(b) a reentrant function may not call any other function that are
not reentrant
(c) a reentrant function may not use the hardware in a non atomic
way
(d) a reentrant function may call any other function that are not
reentrant

answer is given as d option

a single copy of code executed by mulltiple tasks and will always
works correctly is called a reentrant code. for example , many task
may call sprintf(),but there is only a single copy of the subroutine
in the system

5)is the following code reentrant?

int j;

void f(void)
{
if(j > 10)
{
j = j+1;
printf("%d",j);
}
else
{
j = j-1;
printf("%d",j);
}
}

answer is given as no

violation of rule 1: global variable j is in a fixed loacation in
memory and therefore shared by any tasks that call f.The use of j is
not atomic, because the RTOS might switch tasks between the time it is
tested and the time that it is updated. may violate rule 2 for this
function to reentrant,printf must also be reentrant


6) In a program written in C find the major and minor cycle(rem) ?

main()
{
while(1)
{
proc_w();
proc_x();
proc_w();
proc_y();
proc_w();
proc_x();
proc_w();
proc_z();

}
}


answer is given as

major cycle wxwywxwz
minor cycle wxwz wxwy wx wy wz

if scheduling of a task is shown on time line and if the assignment
uses rate-monotonic discipline, then scheduling sequence repeats
itself after a period of time. this sequence is called a major cycle,
smaller sequence also repeat they are called minor cycles
 
R

Ravishankar S

The book name is Advanced test in C
and
Embedded System Programming !!!!!!!!!!!

by Ashok .K pathak

some more questions from the book

state whether true or false

1) system can't determine size of the stack before program runs
answer is given as true

2) location of constant string in memory(text area or data area)
may be compiler dependent

answer is given as true
string may be placed in data area of string area depending on
implementation

3)in all cases printf(ptr); and printf("%s",ptr); will print the same
string,where ptr points to the address of the string

answer is given as false
printf(ptr); fails if ptr contains %
ex:- printf("a%cb"),printf("%s","a%cb"); will print different
strings

4) which of the following is false

(a) a reentrant function may not use variable in a non atomic way
unless they are stored on the stack of the task that called the
function or are other wise the private variable of that task

(b) a reentrant function may not call any other function that are
not reentrant
(c) a reentrant function may not use the hardware in a non atomic
way
(d) a reentrant function may call any other function that are not
reentrant

answer is given as d option

a single copy of code executed by mulltiple tasks and will always
works correctly is called a reentrant code. for example , many task
may call sprintf(),but there is only a single copy of the subroutine
in the system

5)is the following code reentrant?

int j;

void f(void)
{
if(j > 10)
{
j = j+1;
printf("%d",j);
}
else
{
j = j-1;
printf("%d",j);
}
}

answer is given as no

violation of rule 1: global variable j is in a fixed loacation in
memory and therefore shared by any tasks that call f.The use of j is
not atomic, because the RTOS might switch tasks between the time it is
tested and the time that it is updated. may violate rule 2 for this
function to reentrant,printf must also be reentrant


6) In a program written in C find the major and minor cycle(rem) ?

main()
{
while(1)
{
proc_w();
proc_x();
proc_w();
proc_y();
proc_w();
proc_x();
proc_w();
proc_z();

}
}


answer is given as

major cycle wxwywxwz
minor cycle wxwz wxwy wx wy wz

if scheduling of a task is shown on time line and if the assignment
uses rate-monotonic discipline, then scheduling sequence repeats
itself after a period of time. this sequence is called a major cycle,
smaller sequence also repeat they are called minor cycles

The answers seem OK AFAIK. Better is to post at comp.embedded for comments
 
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.

This is what you meant is n't it ?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(void)
{

int *x,n=10;
x = &n;

if( &*x == x)
puts("EQUAL");
else
puts("NOT EQUAL");

printf("\n *x = %p",*x);
printf("\n &n = %p",&n);

return(EXIT_SUCCESS);
}

I am getting output as
EQUAL

*x = 0xa
&n = 0xbf84e8a0

now can you make it clear that why &*x isn't a l value ?
is it because we can't assign to it or any other reason ?
 
H

Harald van Dijk

now can you make it clear that why &*x isn't a l value ? is it because
we can't assign to it or any other reason ?

It's the other way around: we can't assign to it because it isn't an
lvalue. It's an expression that is not directly formed by reading an
object. It's the same way that x+0 isn't an lvalue, even though for most
arithmetic types, it's otherwise completely equivalent.

What would you expect & &*x to mean? Would you want it to give you a
pointer to x? If you would, then where do you stop? Consider x+0 again,
would you consider that an lvalue? And if so, how about when x is of type
short (so x+0 is of type int)? I'm not saying it couldn't be done, but
getting all the details right would be far more trouble than it's worth.
 
K

Keith Thompson

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

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.

There is a real reason: an lvalue designates an object, and a function
is not an object. Whether that's a real *good* reason is another
question.
 
J

James Kuyper

Ravishankar said:
Would be interesting for me to know which book this is ?


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

That doesn't qualify, because "==" does not require an lvalue operand.

On the other hand, "=" does require an lvalue operand, and I've already
given a=3 as ane example.
*&(x) == x and &*(x) != x
Is this what is meant by the complicated language ??

No, equality applies to values. The clearest way to show that an
expression refers to an object is to use the expression to assign a
value to the object it refers to, though this only applies to modifiable
lvalues. As far as I can tell what he's saying is that:

int i, *p;

// s1: Take address after performing indirection.
// Since &(*p) supposedly refers to an object, this
// should not be a constraint violation:
&(*p) = &i;

// s2: Perform indirection after taking address
// Since *(&i) supposedly does not refer to an object,
// this should be a constraint violation:
*(&i) = 3;

If I'm correct about what the author meant, he's got it exactly backwards.
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top