Velocity Reviews > some tricky questions

# some tricky questions

birensubudhi@gmail.com
Guest
Posts: n/a

 06-02-2007
1) void foo(char *s,char *t)
{
while(*s++=*t++);

}

which C function is equivalent to foo ?

2) #define ROUND(x,n)

((x+n-1)&(~(n-1)))
what is hte value of ROUND(223,64) ?
DESCRIBE IT HOW IT IS SO?

3) void foo(int x)
{
int i=0;
while(x)
{
x=x&(x-1);
i++;
}
printf("%d",i);
}
what is the o/p of this function & why it is so ?
what function does this '&' does ?
4) union{
int i;
char c[sizeof(int)];
}x;
x.i=1;
if(x.c[0]==1)
printf("the m/c is _______________ endian");
else
printf("the m/c is _______________ endian");

fill in the blanks with tthe correct option:
a) little,big b)big,big
c) big,little d)little,little

5) int fun2(char *a,char *b)
{
for(; *a==*b;a++,b++)
if(*a=='\0')
return 0;

return *a-*b;
}
char a[10]="date",b[10]="data";

what is the value of fun2(a,b)?

Guest
Posts: n/a

 06-02-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 1) void foo(char *s,char *t)
> {
> while(*s++=*t++);
>
>
> }
>
> which C function is equivalent to foo ?

FYI, we don't do homework.

--
Tor <torust [at] online [dot] no>

Army1987
Guest
Posts: n/a

 06-02-2007

<(E-Mail Removed)> ha scritto nel messaggio
news:(E-Mail Removed) oups.com...
> 1) void foo(char *s,char *t)
> {
> while(*s++=*t++);
>
>
> }
>
> which C function is equivalent to foo ?

None. strcpy returns a char* equal to its first argument. Also, the
second argument of strcpy is a const char*.
> 2) #define ROUND(x,n)
>
> ((x+n-1)&(~(n-1)))
> what is hte value of ROUND(223,64) ?
> DESCRIBE IT HOW IT IS SO?

It is the value of ((223+64-1)&(~(64-1))), of course.

> 3) void foo(int x)
> {
> int i=0;
> while(x)
> {
> x=x&(x-1);
> i++;
> }
> printf("%d",i);
> }
> what is the o/p of this function & why it is so ?
> what function does this '&' does ?

If x is INT_MIN, it is allowed to make demons fly out of your nose.
BTW, what is an o/p? And the '&' does no "function", at least in the C
sense.

> 4) union{
> int i;
> char c[sizeof(int)];
> }x;
> x.i=1;
> if(x.c[0]==1)
> printf("the m/c is _______________ endian");
> else
> printf("the m/c is _______________ endian");
>
> fill in the blanks with tthe correct option:
> a) little,big b)big,big
> c) big,little d)little,little

Nope. It could be middle endian. And if I'm not wrong, the standard
specifies nothing about the bit order. On a conforming implementation, it
could be sizeof (int) == 2 && CHAR_BIT ==8, and
the first byte of i could contain the first, third, fifth, seventh,
ninth, eleventh, thirteenth, and fifteenth bit in that order, and
the second byte could contain the sixteenth, fourteenth, ..., second bit in
that order.

> 5) int fun2(char *a,char *b)
> {
> for(; *a==*b;a++,b++)
> if(*a=='\0')
> return 0;
>
> return *a-*b;
> }
> char a[10]="date",b[10]="data";
>
> what is the value of fun2(a,b)?

It is 'e' - 'a', which nothing requires to be four. It could be
anything from CHAR_MIN - CHAR_MAX to -1 included, and it could be
anything from 1 to CHAR_MAX - CHAR_MIN included. Not all the world
uses ASCII.

per9000
Guest
Posts: n/a

 06-02-2007
On Jun 2, 12:28 pm, "Army1987" <(E-Mail Removed)> wrote:
> <(E-Mail Removed)> ha scritto nel messaggionews:1180772304.807456.242510@n15g2000prd .googlegroups.com...
>
> [...]
>
> > 2) #define ROUND(x,n)

>
> > ((x+n-1)&(~(n-1)))
> > what is hte value of ROUND(223,64) ?
> > DESCRIBE IT HOW IT IS SO?

>
> It is the value of ((223+64-1)&(~(64-1))), of course.
> [...]

The reason: "by definition".
[]-|--<

/Per

--

Per Erik Strandberg
home: www.pererikstrandberg.se
work: www.incf.org
also: www.spongswedencare.se

Kenny McCormack
Guest
Posts: n/a

 06-02-2007
In article <(E-Mail Removed) .com>,
<(E-Mail Removed)> wrote:
>1) void foo(char *s,char *t)
> {
> while(*s++=*t++);
>
>
> }
>
>which C function is equivalent to foo ?

What is homework, Alex?

Keith Thompson
Guest
Posts: n/a

 06-02-2007
(E-Mail Removed) writes:
> 1) void foo(char *s,char *t)
> {
> while(*s++=*t++);
>
>
> }
>
> which C function is equivalent to foo ?

foo.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Robert Gamble
Guest
Posts: n/a

 06-02-2007
On Jun 2, 6:28 am, "Army1987" <(E-Mail Removed)> wrote:
> <(E-Mail Removed)> ha scritto nel messaggionews:1180772304.807456.242510@n15g2000prd .googlegroups.com...
>
> > 1) void foo(char *s,char *t)
> > {
> > while(*s++=*t++);

>
> > }

>
> > which C function is equivalent to foo ?

>
> None. strcpy returns a char* equal to its first argument. Also, the
> second argument of strcpy is a const char*.
>
> > 2) #define ROUND(x,n)

>
> > ((x+n-1)&(~(n-1)))
> > what is hte value of ROUND(223,64) ?
> > DESCRIBE IT HOW IT IS SO?

>
> It is the value of ((223+64-1)&(~(64-1))), of course.
>
> > 3) void foo(int x)
> > {
> > int i=0;
> > while(x)
> > {
> > x=x&(x-1);
> > i++;
> > }
> > printf("%d",i);
> > }
> > what is the o/p of this function & why it is so ?
> > what function does this '&' does ?

>
> If x is INT_MIN, it is allowed to make demons fly out of your nose.
> BTW, what is an o/p? And the '&' does no "function", at least in the C
> sense.
>
> > 4) union{
> > int i;
> > char c[sizeof(int)];
> > }x;
> > x.i=1;
> > if(x.c[0]==1)
> > printf("the m/c is _______________ endian");
> > else
> > printf("the m/c is _______________ endian");

>
> > fill in the blanks with tthe correct option:
> > a) little,big b)big,big
> > c) big,little d)little,little

>
> Nope. It could be middle endian.

Right.

> And if I'm not wrong, the standard
> specifies nothing about the bit order. On a conforming implementation, it
> could be sizeof (int) == 2 && CHAR_BIT ==8, and
> the first byte of i could contain the first, third, fifth, seventh,
> ninth, eleventh, thirteenth, and fifteenth bit in that order, and
> the second byte could contain the sixteenth, fourteenth, ..., second bit in
> that order.

Robert Gamble

Richard
Guest
Posts: n/a

 06-02-2007

> (E-Mail Removed) wrote:
>> 1) void foo(char *s,char *t)
>> {
>> while(*s++=*t++);
>>
>>
>> }
>>
>> which C function is equivalent to foo ?

>
> FYI, we don't do homework.

Who is "we"? Because someone else did.

Mark McIntyre
Guest
Posts: n/a

 06-02-2007
On Sun, 03 Jun 2007 00:12:06 +0200, in comp.lang.c , Richard
<(E-Mail Removed)> wrote:

>
>> (E-Mail Removed) wrote:
>>> 1) void foo(char *s,char *t)
>>> {
>>> while(*s++=*t++);
>>>
>>>
>>> }
>>>
>>> which C function is equivalent to foo ?

>>
>> FYI, we don't do homework.

>
>Who is "we"? Because someone else did.

test...

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

Guest
Posts: n/a

 06-03-2007
Richard wrote:
> Tor Rustad <(E-Mail Removed)> writes:

[...]

>> FYI, we don't do homework.

>
> Who is "we"? Because someone else did.

I exclude spammers, and that someone else was spamming in my view.

--
Tor <torust [at] online [dot] no>