Adsnese

Ad

Monday, December 24, 2007

C Interview Questions And Answers- 19

[Q19.1 How can I read a single character from the keyboard...]

Note that the answers are often not unique even across different
variants of a system; bear in mind when answering system-
---
Note that the answers may differ even across variants of
otherwise similar systems (e.g. across different variants of
Unix); bear in mind when answering system-specific questions
that the answer that applies to your system may not apply to
everyone else's.

==========

[Q19.3 How can I display a percentage-done indication...]

current line. The character '\b' is a backspace, and will
usually move the cursor one position to the left.
---
usually move the cursor one position to the left. (But remember
to call fflush(), too.)

==========

[Q19.8 How can I direct output to the printer?]

Under some circumstances, another (and perhaps the only)
possibility is to use a window manager's screen-capture
function, and print the resulting bitmap.

==========

[Q19.10 How can I do graphics?]

A modern, platform-independent graphics library (which also
supports 3D graphics and animation) is OpenGL. Other graphics
standards which may be of interest are GKS and PHIGS.

==========

[Q19.12 How can I find out the size of a file, prior to reading it in?]

You can fseek() to the end and then use
ftell(), or maybe try fstat(), but these tend to have the same
sorts of problems: fstat() is not portable, and generally tells
you the same thing stat() tells you; ftell() is not guaranteed
to return a byte count except for binary files. Some systems
---
to return a byte count except for binary files (but, strictly
speaking, binary files don't necessarily support fseek to
SEEK_END at all). Some systems provide functions called
filesize() or filelength(), but these are obviously not
portable, either.

==========

[Q19.20 How can I read a directory in a C program?]

(MS-DOS also has FINDFIRST and FINDNEXT routines which
do essentially the same thing.)
---
do essentially the same thing, and MS Windows has FindFirstFile
and FindNextFile.)

==========

[Q19.23 How can I allocate arrays or structures bigger than 64K?]

to allocate huge amounts of it contiguously. (The C Standard
does not guarantee that single objects can be 32K or larger,
or 64K for C9X.) Often it's a good idea to use data
---
or 64K for C99.) Often it's a good idea to use data

==========

[Q19.25 How can I access memory (a memory-mapped device...]

Then, *magicloc refers to the location you want.
---
Then, *magicloc refers to the location you want. If the
location is a memory-mapped I/O register, you will probably also
want to use the volatile qualifier.

==========

[Q19.27 How can I invoke another program...]

Depending on your operating system, you may also be able to use
system calls such as exec or spawn (or execl, execv, spawnl,
spawnv, etc.).

==========

[Q19.37 How can I implement a delay... with sub-second resolution?]

A: Unfortunately, there is no portable way. V7 Unix, and derived
systems, provided a fairly useful ftime() function with
resolution up to a millisecond, but it has disappeared from
System V and POSIX. Other routines you might look for on your
---
A: Unfortunately, there is no portable way. Routines you might
look for on your system include clock(), delay(), ftime(),
gettimeofday(), msleep(), nap(), napms(), nanosleep(),
setitimer(), sleep(), Sleep(), times(), and usleep().

==========

[Q19.40 How do I... Use sockets? Do networking?]

and W. R. Stevens's _UNIX Network Programming_. There is also
plenty of information out on the net itself, including the
"Unix Socket FAQ" at http://kipper.york.ac.uk/~vic/sock-faq/
---
"Unix Socket FAQ" at http://www.developerweb.net/sock-faq/

and "Beej's Guide to Network Programming" at
http://www.ecst.csuchico.edu/~beej/guide/net/.

(One tip: depending on your OS, you may need to explicitly
request the -lsocket and -lnsl libraries; see question 13.25.)

==========

[Q20.14 Are pointers really faster than arrays?]

It is "usually" faster to march through large arrays with
---
For conventional machines, it is usually faster to march through
large arrays with pointers rather than array subscripts, but for
some processors the reverse is true.

==========

[Q20.20 Why don't C comments nest? ...]

Note also that // comments, as in C++, are not yet legal in C,
so it's not a good idea to use them in C programs (even if your
compiler supports them as an extension).
---
Note also that // comments have only become legal in C as of
C99.

==========

20.20b: Is C a great language, or what? Where else could you write
---
20.21b: Is C a great language, or what? Where else could you write
something like a+++++b ?

==========

[Q20.27 ... Can I use a C++ compiler to compile C code?]

compilation modes. See also questions 8.9 and 20.20.
---
compilation modes. (But it's usually a bad idea to compile
straight C code as if it were C++; the languages are different
enough that you'll generally get poor results.) See also
questions 8.9 and 20.20.

==========

20.32: Will 2000 be a leap year? Is (year % 4 == 0) an accurate test
for leap years?
---
20.32: Is (year % 4 == 0) an accurate test for leap years? (Was 2000 a
leap year?)

A: Yes and no, respectively. The full expression for the present
Gregorian calendar is
---
A: No, it's not accurate (and yes, 2000 was a leap year).
The full expression for the present Gregorian calendar is

==========

[Q20.34 ...how do you write a program which produces its own source code...?]

(This program, like many of the genre, neglects to #include
stdio.h, and assumes that the double-quote character " has the
---
(This program has a few deficiencies, among other things
neglecting to #include stdio.h, and assuming that the double-
quote character " has the value 34, as it does in ASCII.)

Here is an improved version, posted by James Hu:

#define q(k)main(){return!puts(#k"\nq("#k")");}
q(#define q(k)main(){return!puts(#k"\nq("#k")");})

==========

[Q20.35 What is "Duff's Device"?]

A: It's a devastatingly deviously unrolled byte-copying loop,
devised by Tom Duff while he was at Lucasfilm. In its "classic"
form, it looks like:
---
A: It's a devastatingly devious way of unrolling a loop, devised by
Tom Duff while he was at Lucasfilm. In its "classic" form, it
was used to copy bytes, and looked like this:

==========

[Q20.38 Where does the name "C" come from, anyway?]

A: C was derived from Ken Thompson's experimental language B, which
was inspired by Martin Richards's BCPL (Basic Combined
Programming Language), which was a simplification of CPL
(Cambridge Programming Language).
---
(Combined Programming Language, or perhaps Cambridge Programming
Language).

==========

[Q20.40 Where can I get extra copies of this list?]

What about back issues?

This list is an evolving document containing questions which
have been Frequent since before the Great Renaming; it is not
just a collection of this month's interesting questions. Older
copies are obsolete and don't contain much, except the
occasional typo, that the current list doesn't.


No comments:

My Ad

.