"C" Programming Language - Miscellaneous

Started by sivaji, Jan 10, 2008, 04:38 PM

Previous topic - Next topic

sivaji

Programming Stuffs in "C" - Technical Skills for INTERVIEW

Miscellaneous

1: What can I safely assume about the initial values of variables which are not explicitly initialized? If global variables start out as ``zero,'' is that good enough for null pointers and floating-point zeroes?

They're always zero.

2: How can I insert or delete a line (or record) in the middle of a file?

Using fcntl(), lock the line or record in the file exclusively. Now, using another thread, read the file, at each byte, trying to write that byte back. Whenever you succeed, write that byte into another file. Then copy the new file over the old file, releasing the lock first.

3: How can I return several values from a function?

Code like this ought to work.
long int foo() {
   return 2L +3; /* returns both values */
}

4: If I have a char * variable pointing to the name of a function as a string, how can I call that function?

Try the following:
eval(s);

Now all you need to do is write eval().

5: I seem to be missing the system header file <math.h>. Can someone send me a copy?

A lot of people claim that it is useless to send people headers from other machines. Not so! It can be informative, and can show you a lot about how blatantly stupid your request was, although it can't show you anything you wouldn't have known in an instant had you thought before posting.

Of course, we'd be happy to send you the header files...

----cut here----
/* math.h rev 7.0b (3/7/95) */
/* RCS log: #log% - can anyone tell me why this doesn't work?
* - joe, 2/12/93
*/

/*
* Copyright 1995 Berserkley Software Systems && Analytic Overdrive
*/

/* Parts of this header, including in particular the second and
* third clauses of the first sentance of the fourth comment, were
* based on copyright agreements from other sources, including
* Xerox corporation.
*/

/*
* math.h - math related macros and headers
*/

#ifndef _MATH_H
#define _MATH_H

/*
* global data and definitions
*/

#ifdef __LITERAL_BIBLICAL_FUNDEMENTALISM
#define PI 3.0            /* 1 Kings 7:23 */
#endif

/*
* common (portable) structures and functions
*/

/*
* machine specific data
*/
#include <machine/math.h>

#endif /* _MATH_H // prevent multiple inclusion by using C++ comments*/


6: How can I call FORTRAN (C++, BASIC, Pascal, Ada, LISP, perl) functions from C? (And vice versa?)

You can do things like this:
DO CALL FORTRAN;      fortran();
__LINE__ BASIC;         basic();
sub pascal;         pascal();
(((((lisp)))))         lithp();

  • &perl_c;         perl():

    (You can't call Ada from C; it's unsafe.)

    [* ] C is pass by value, of course.

    7: Is C++ a superset of C? Can I use a C++ compiler to compile C code?

    C++ is a superset of something, we're not sure what. You can use a C++ compiler to compile C code, but the results may surprise you.

    8: How can I get the ASCII value corresponding to a character, or vice versa?

    chr$(foo); You would have known this if you had an integer basic in ROM.

    9: How can I implement sets and/or arrays of bits?

    With linked lists of bitfields. You may also wish to simply use a large set of constants and some clever use of the switch statement, i.e.:
    enum { zero, one, two, three };
    int bitwise_or(int n, int m) {
       switch (n) {
       case three:
          return three;
          break;
       case two:
          switch (m) {
          case one: case three: return three; break;
          default: return two; break;
          }
          break;
       case one:
          switch (m) {
          case two: case three: return three; break;
          default: return one; break;
          }
          break;
       default: case zero:
          switch (m) {
          case one: return one; break;
          case two: return two; break;
          case three: return three; break;
          case zero: default: return zero; break;
          }
          break;
       }
    }

    Obviously, you'll need to increase this slightly to deal with more than two bits. This is much more readable than the alleged ``C'' solution:
    int bitwise_or(int n,int m){return n|m;}

    Note how the lack of whitespace around operators obscures the functionality of the code. A clear argument for explicit statement of program logic over arcane operators, if I ever saw one.

    The enum at the top isn't declared ``const int'', because the resulting ``const poisoning'' would require casts during all of the switch statements.

    10: What is the most efficient way to count the number of bits which are set in a value?

    Start a counter at zero and add one to it for each bit set. Some operating systems may provide a call to do this. For values over INT_MAX/2, start the counter at CHAR_BIT * sizeof(int) and subtract one for each bit not set.

    11: Are pointers really faster than arrays? How much do function calls slow things down? Is ++i faster than i = i + 1?

    Yes. About 10 ms per call. Only on machines which feature preincrement addressing.

    12: This program crashes before it even runs! (When single-stepping with a debugger, it dies before the first statement in main.)

    You probably declared main as ``void main(void)''. It's also possible that the first statement in main is abort(); - by the as if rule, the compiler can abort at any time before then, too. Some compilers have bugs, and will produce buggy code for any module which includes the letters ``a'', ``b'', ``o'', ``r'', and ``t'' in that order before the first function declaration.

    13: What do ``Segmentation violation'' and ``Bus error'' mean?

    C programs are very territorial, and divide their code into segments. Violating these segments can trigger riots; similarly, pointers and integral constants are at the front of the bus, wheras arrays, strings, and other second-class data types are required to be at the rear of the bus. When they start forgetting their places, you can get a bus error. This is what the whole ``integral'' type thing is about - integrated bussing.
Am now @ Chennai