473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

C aptitude

Hi all,

I was going through the book "Test your skills in C" by Thamarai selvi
and R murugesan, publisheb by Tata Mcgraw-Hill,
ISBN 0-07-044759-4 , the following are from this book,
now my question is to what extent the following are true..???

1) a function with no action is allowed and is known as dummy function

2) (int(*(*)[3])(char*))ptr is the correct way to cast ptr to a
pointer to a 3 element array of function pointers with a return value
of int and a single parameter of a pointer to char

3)how could stderr be re directed from within a program to force all
error messages to be written to the end of the text file "error.log"
instead of location specified by the operating system

a) stderr = fopen("error.log","w");
b) freopen("error.log","a",stderr);
c) stderr = freopen(stderr,"a","error.log");
d) stderr = fopen("error.log","a");

the answer is given as d option

4) #include<stdlib.h>
#include <stdio.h>
char *format="%d";

void func();

int main(void)
{
int x;
func(scanf,&x);
printf("%d\n",x);

return EXIT_SUCCESS;
}

referring to the sample code given above, which of the following
would be correct implementation
for func ?

a) void func( int (*y)(const char*,...),int* x)
{ (*y)(format,x); }

b) void func( int (*y)(const char*,...),int* x)
{ (*y)(format,&x); }

answer is given as a option
Jun 27 '08 #1
5 2096
aa*****@gmail.com wrote:
Hi all,

I was going through the book "Test your skills in C" by Thamarai selvi
and R murugesan, publisheb by Tata Mcgraw-Hill,
ISBN 0-07-044759-4 , the following are from this book,
now my question is to what extent the following are true..???

1) a function with no action is allowed and is known as dummy function
True. Sometimes they are also called as stub functions.
2) (int(*(*)[3])(char*))ptr is the correct way to cast ptr to a
pointer to a 3 element array of function pointers with a return value
of int and a single parameter of a pointer to char
I think the following should do it:

(int(*)[3](char *))ptr
3)how could stderr be re directed from within a program to force all
error messages to be written to the end of the text file "error.log"
instead of location specified by the operating system

a) stderr = fopen("error.log","w");
b) freopen("error.log","a",stderr);
c) stderr = freopen(stderr,"a","error.log");
d) stderr = fopen("error.log","a");

the answer is given as d option
Wrong. The correct option is (b). The pre-defined streams need not be
lvalues.
4) #include<stdlib.h>
#include <stdio.h>
char *format="%d";

void func();

int main(void)
{
int x;
func(scanf,&x);
printf("%d\n",x);

return EXIT_SUCCESS;
}

referring to the sample code given above, which of the following
would be correct implementation
for func ?

a) void func( int (*y)(const char*,...),int* x)
{ (*y)(format,x); }

b) void func( int (*y)(const char*,...),int* x)
{ (*y)(format,&x); }

answer is given as a option
Yes. That's right. Note though that the call could as well have been:

y(format, x);

or

(****y)(format, x);

Jun 27 '08 #2
aa*****@gmail.com wrote, On 25/04/08 19:42:
Hi all,

I was going through the book "Test your skills in C" by Thamarai selvi
and R murugesan, publisheb by Tata Mcgraw-Hill,
ISBN 0-07-044759-4 , the following are from this book,
now my question is to what extent the following are true..???

1) a function with no action is allowed and is known as dummy function
It is true that it is allowed. It is also true that such functions are
often known (by me at least) as dummy functions. However, whether they
are called dummy functions is nothing to do with C.
2) (int(*(*)[3])(char*))ptr is the correct way to cast ptr to a
pointer to a 3 element array of function pointers with a return value
of int and a single parameter of a pointer to char
I can't think of a good reason for doing such a thing. If there ever was
a good reason I would not do it like that (and would reject anything
like that in a code review) since it can be made far clearer by using a
typedef for the function type (or function pointer type if you prefer).
3)how could stderr be re directed from within a program to force all
error messages to be written to the end of the text file "error.log"
instead of location specified by the operating system

a) stderr = fopen("error.log","w");
b) freopen("error.log","a",stderr);
c) stderr = freopen(stderr,"a","error.log");
d) stderr = fopen("error.log","a");

the answer is given as d option
stderr need not be a modifiable lvalue. I.e. you may not be able to do
the assignment in d. According to the standard the primary use of
freopen is to change the file associated with stdin/out/err for this
very reason! So that leave b as the only possibility, so if the book
really does say d it is wrong.
4) #include<stdlib.h>
#include <stdio.h>
char *format="%d";

void func();

int main(void)
{
int x;
func(scanf,&x);
printf("%d\n",x);

return EXIT_SUCCESS;
}

referring to the sample code given above, which of the following
would be correct implementation
for func ?

a) void func( int (*y)(const char*,...),int* x)
{ (*y)(format,x); }

b) void func( int (*y)(const char*,...),int* x)
{ (*y)(format,&x); }

answer is given as a option
Of the two, a is the only possible valid answer (the correct answer
depends on the version of the C standard in use). However, I don't like
the code anyway :-)
--
Flash Gordon
Jun 27 '08 #3
On Sat, 26 Apr 2008 02:53:29 +0530, santosh <sa*********@gmail.com>
wrote:
>aa*****@gmail.com wrote:
snip
>2) (int(*(*)[3])(char*))ptr is the correct way to cast ptr to a
pointer to a 3 element array of function pointers with a return value
of int and a single parameter of a pointer to char

I think the following should do it:

(int(*)[3](char *))ptr
Nope. The original is correct.

Remove del for email
Jun 27 '08 #4

"Flash Gordon" <sp**@flash-gordon.me.ukwrote in message
>
>1) a function with no action is allowed and is known as dummy function

It is true that it is allowed. It is also true that such functions are
often known (by me at least) as dummy functions. However, whether they are
called dummy functions is nothing to do with C.
You mean the standard doesn't define anything known as the "dummy" function.
However it is implicit in that

void dummy(void)
{
}

is allowed by C rules of grammar. It wasn't a necessary decision to allow
functions with no code in the body at all.

Dummy functions differ from stub functions. A dummy function does nothing, a
stub function returns valid but trival data. For instance if you wanted to
read a bar code, but had no barcode reader, you might write a stub function

int readbarcode(char *code)
{
strcpy(code, "123456789012345");
return 0;
}

as a placeholder until the real reader arrives.

Both are perfectly valid C programming concepts.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 27 '08 #5
Malcolm McLean wrote, On 26/04/08 11:13:
>
"Flash Gordon" <sp**@flash-gordon.me.ukwrote in message
>>
>>1) a function with no action is allowed and is known as dummy function

It is true that it is allowed. It is also true that such functions are
often known (by me at least) as dummy functions. However, whether they
are called dummy functions is nothing to do with C.
You mean the standard doesn't define anything known as the "dummy"
function. However it is implicit in that

void dummy(void)
{
}

is allowed by C rules of grammar. It wasn't a necessary decision to
allow functions with no code in the body at all.
Yes, and I stated that a function "with no action" is allowed. In fact,
nothing you say above contradicts anything I said and all it adds is an
example.
Dummy functions differ from stub functions. A dummy function does
nothing, a stub function returns valid but trival data. For instance if
<snip>

Not always. However, it has nothing to do with the question.
Both are perfectly valid C programming concepts.
I stated that the one that was asked about was valid. I said nothing
about the other for the same reason I said nothing about FFTs, the
reason being it was not what was being asked.
--
Flash Gordon
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: Matt | last post by:
Hello, I'm a hiring C++ developer employer looking for existing, online C++ aptitude tests. I have not yet extensively researched this yet, but as an example, I thought this test looked...
4
by: Savio | last post by:
Hi Freinds, I am an MCA final year student. I have heard that the MNC's always opt C as the subject for their Aptitude tests . Does anyone know which book is the best for refering C aptitude ? ...
14
by: vaishnavamruta | last post by:
hello, i want to give online c/c++ test, right now i am studying MCA IV semester and now our campusing will going to start. so, the different companies will come for interview, so i want to be...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.