Miscellaneous interview question and answer for C programming

1. Where in memory are my variables stored?
Variables can be stored in several places in memory, depending on their lifetime. Variables that are defined outside any function (whether of global or file static scope), and variables that are defined inside a function as static variables, exist for the lifetime of the program's execution. These variables are stored in the "data segment." The data segment is a fixed-size area in memory set aside for these variables. The data segment is subdivided into two parts, one for initialized variables and another for uninitialized variables.

Variables that are defined inside a function as auto variables (that are not defined with the keyword static) come into existence when the program begins executing the block of code (delimited by curly braces {}) containing them, and they cease to exist when the program leaves that block of code.

Variables that are the arguments to functions exist only during the call to that function. These variables are stored on the "stack". The stack is an area of memory that starts out small and grows automatically up to some predefined limit. In DOS and other systems without virtual memory, the limit is set either when the program is compiled or when it begins executing. In UNIX and other systems with virtual memory, the limit is set by the system, and it is usually so large that it can be ignored by the programmer.

The third and final area doesn't actually store variables but can be used to store data pointed to by variables. Pointer variables that are assigned to the result of a call to the malloc() function contain the address of a dynamically allocated area of memory. This memory is in an area called the "heap." The heap is another area that starts out small and grows, but it grows only when the programmer explicitly calls malloc() or other memory allocation functions, such as calloc(). The heap can share a memory segment with either the data segment or the stack, or it can have its own segment. It all depends on the compiler options and operating system. The heap, like the stack, has a limit on how much it can grow, and the same rules apply as to how that limit is determined.

2. Do variables need to be initialized?
No. All variables should be given a value before they are used, and a good compiler will help you find variables that are used before they are set to a value. Variables need not be initialized, however. Variables defined outside a function or defined inside a function with the static keyword are already initialized to 0 for you if you do not explicitly initialize them.

Automatic variables are variables defined inside a function or block of code without the static keyword. These variables have undefined values if you don't explicitly initialize them. If you don't initialize an automatic variable, you must make sure you assign to it before using the value.

Space on the heap allocated by calling malloc() contains undefined data as well and must be set to a known value before being used. Space allocated by calling calloc() is set to 0 for you when it is allocated.

3. What is page thrashing?
Some operating systems (such as UNIX or Windows in enhanced mode) use virtual memory. Virtual memory is a technique for making a machine behave as if it had more memory than it really has, by using disk space to simulate RAM (random-access memory). In the 80386 and higher Intel CPU chips, and in most other modern microprocessors (such as the Motorola 68030, Sparc, and Power PC), exists a piece of hardware called the Memory Management Unit, or MMU.

The MMU treats memory as if it were composed of a series of "pages." A page of memory is a block of contiguous bytes of a certain size, usually 4096 or 8192 bytes. The operating system sets up and maintains a table for each running program called the Process Memory Map, or PMM. This is a table of all the pages of memory that program can access and where each is really located.

Every time your program accesses any portion of memory, the address (called a "virtual address") is processed by the MMU. The MMU looks in the PMM to find out where the memory is really located (called the "physical address"). The physical address can be any location in memory or on disk that the operating system has assigned for it. If the location the program wants to access is on disk, the page containing it must be read from disk into memory, and the PMM must be updated to reflect this action (this is called a "page fault"). Hope you're still with me, because here's the tricky part. Because accessing the disk is so much slower than accessing RAM, the operating system tries to keep as much of the virtual memory as possible in RAM. If you're running a large enough program (or several small programs at once), there might not be enough RAM to hold all the memory used by the programs, so some of it must be moved out of RAM and onto disk (this action is called "paging out").

The operating system tries to guess which areas of memory aren't likely to be used for a while (usually based on how the memory has been used in the past). If it guesses wrong, or if your programs are accessing lots of memory in lots of places, many page faults will occur in order to read in the pages that were paged out. Because all of RAM is being used, for each page read in to be accessed, another page must be paged out. This can lead to more page faults, because now a different page of memory has been moved to disk. The problem of many page faults occurring in a short time, called "page thrashing," can drastically cut the performance of a system.

Programs that frequently access many widely separated locations in memory are more likely to cause page thrashing on a system. So is running many small programs that all continue to run even when you are not actively using them. To reduce page thrashing, you can run fewer programs simultaneously. Or you can try changing the way a large program works to maximize the capability of the operating system to guess which pages won't be needed. You can achieve this effect by caching values or changing lookup algorithms in large data structures, or sometimes by changing to a memory allocation library which provides an implementation of malloc() that allocates memory more efficiently. Finally, you might consider adding more RAM to the system to reduce the need to page out.

4. What is a const pointer?
The access modifier keyword const is a promise the programmer makes to the compiler that the value of a variable will not be changed after it is initialized. The compiler will enforce that promise as best it can by not enabling the programmer to write code which modifies a variable that has been declared const.

A "const pointer," or more correctly, a "pointer to const," is a pointer which points to data that is const (constant, or unchanging). A pointer to const is declared by putting the word const at the beginning of the pointer declaration. This declares a pointer which points to data that can't be modified. The pointer itself can be modified. The following example illustrates some legal and illegal uses of a const pointer:

const char  *str = "hello";
char  c = *str    /* legal */
str++;            /* legal */
*str = 'a';       /* illegal */
str[1] = 'b';     /* illegal */

The first two statements here are legal because they do not modify the data that str points to. The next two statements are illegal because they modify the data pointed to by str.

Pointers to const are most often used in declaring function parameters. For instance, a function that counted the number of characters in a string would not need to change the contents of the string, and it might be written this way:

my_strlen(const char *str)
{
        int count = 0;
        while (*str++)
        {
            count++;
        }
        return count;
}

Note that non-const pointers are implicitly converted to const pointers when needed, but const pointers are not converted to non-const pointers. This means that my_strlen() could be called with either a const or a non-const character pointer.

5. When should the register modifier be used? Does it really help?
The register modifier hints to the compiler that the variable will be heavily used and should be kept in the CPU's registers, if possible, so that it can be accessed faster. There are several restrictions on the use of the register modifier.

First, the variable must be of a type that can be held in the CPU's register. This usually means a single value of a size less than or equal to the size of an integer. Some machines have registers that can hold floating-point numbers as well.

Second, because the variable might not be stored in memory, its address cannot be taken with the unary and operator. An attempt to do so is flagged as an error by the compiler. Some additional rules affect how useful the register modifier is. Because the number of registers is limited, and because some registers can hold only certain types of data (such as pointers or floating-point numbers), the number and types of register modifiers that will actually have any effect are dependent on what machine the program will run on. Any additional register modifiers are silently ignored by the compiler.

Also, in some cases, it might actually be slower to keep a variable in a register because that register then becomes unavailable for other purposes or because the variable isn't used enough to justify the overhead of loading and storing it.

So when should the register modifier be used? The answer is never, with most modern compilers. Early C compilers did not keep any variables in registers unless directed to do so, and the register modifier was a valuable addition to the language. C compiler design has advanced to the point, however, where the compiler will usually make better decisions than the programmer about which variables should be stored in registers. In fact, many compilers actually ignore the register modifier, which is perfectly legal, because it is only a hint and not a directive.

In the rare event that a program is too slow, and you know that the problem is due to a variable being stored in memory, you might try adding the register modifier as a last resort, but don't be surprised if this action doesn't change the speed of the program.

6. When should the volatile modifier be used?
The volatile modifier is a directive to the compiler's optimizer that operations involving this variable should not be optimized in certain ways. There are two special cases in which use of the volatile modifier is desirable. The first case involves memory-mapped hardware (a device such as a graphics adaptor that appears to the computer's hardware as if it were part of the computer's memory), and the second involves shared memory (memory used by two or more programs running simultaneously).

Most computers have a set of registers that can be accessed faster than the computer's main memory. A good compiler will perform a kind of optimization called "redundant load and store removal." The compiler looks for places in the code where it can either remove an instruction to load data from memory because the value is already in a register, or remove an instruction to store data to memory because the value can stay in a register until it is changed again anyway.

If a variable is a pointer to something other than normal memory, such as memory-mapped ports on a peripheral, redundant load and store optimizations might be detrimental. For instance, here's a piece of code that might be used to time some operation:

time_t time_addition(volatile const struct timer *t, int a)
{
        int     n;
        int     x;
        time_t  then;
        x = 0;
        then = t->value;
        for (n = 0; n < 1000; n++)
        {
                x = x + a;
        }
       return t->value - then;
}

In this code, the variable t->value is actually a hardware counter that is being incremented as time passes. The function adds the value of a to x 1000 times, and it returns the amount the timer was incremented by while the 1000 additions were being performed.

Without the volatile modifier, a clever optimizer might assume that the value of t does not change during the execution of the function, because there is no statement that explicitly changes it. In that case, there's no need to read it from memory a second time and subtract it, because the answer will always be 0. The compiler might therefore "optimize" the function by making it always return 0.

If a variable points to data in shared memory, you also don't want the compiler to perform redundant load and store optimizations. Shared memory is normally used to enable two programs to communicate with each other by having one program store data in the shared portion of memory and the other program read the same portion of memory. If the compiler optimizes away a load or store of shared memory, communication between the two programs will be affected.

7. Can a variable be both const and volatile?
Yes. The const modifier means that this code cannot change the value of the variable, but that does not mean that the value cannot be changed by means outside this code. For instance, the timer structure was accessed through a volatile const pointer. The function itself did not change the value of the timer, so it was declared const. However, the value was changed by hardware on the computer, so it was declared volatile. If a variable is both const and volatile, the two modifiers can appear in either order.

8. When should the const modifier be used?
There are several reasons to use const pointers. First, it allows the compiler to catch errors in which code accidentally changes the value of a variable, as in

while (*str = 0) /* programmer meant to write *str != 0 */
{
    /* some code here */
    str++;
}

in which the = sign is a typographical error. Without the const in the declaration of str, the program would compile but not run properly.

Another reason is efficiency. The compiler might be able to make certain optimizations to the code generated if it knows that a variable will not be changed.

Any function parameter which points to data that is not modified by the function or by any function it calls should declare the pointer a pointer to const. Function parameters that are passed by value (rather than through a pointer) can be declared const if neither the function nor any function it calls modifies the data.

In practice, however, such parameters are usually declared const only if it might be more efficient for the compiler to access the data through a pointer than by copying it.

9. How reliable are floating-point comparisons?
Floating-point numbers are the "black art" of computer programming. One reason why this is so is that there is no optimal way to represent an arbitrary number. The Institute of Electrical and Electronic Engineers (IEEE) has developed a standard for the representation of floating-point numbers, but you cannot guarantee that every machine you use will conform to the standard.

Even if your machine does conform to the standard, there are deeper issues. It can be shown mathematically that there are an infinite number of "real" numbers between any two numbers. For the computer to distinguish between two numbers, the bits that represent them must differ. To represent an infinite number of different bit patterns would take an infinite number of bits. Because the computer must represent a large range of numbers in a small number of bits (usually 32 to 64 bits), it has to make approximate representations of most numbers.

Because floating-point numbers are so tricky to deal with, it's generally bad practice to compare a floating-point number for equality with anything. Inequalities are much safer. If, for instance, you want to step through a range of numbers in small increments, you might write this:

#include 
const float first = 0.0;
const float last = 70.0;
const float small = 0.007;
main()
{
        float   f;
        for (f = first; f != last && f < last + 1.0; f += small)
                ;
        printf("f is now %gn", f);
}

However, rounding errors and small differences in the representation of the variable small might cause f to never be equal to last (it might go from being just under it to being just over it). Thus, the loop would go past the value last. The inequality f < last + 1.0 has been added to prevent the program from running on for a very long time if this happens. If you run this program and the value printed for f is 71 or more, this is what has happened.

A safer way to write this loop is to use the inequality f < last to test for the loop ending, as in this example:

float   f;
for (f = first; f < last; f += small)
        ;

You could even precompute the number of times the loop should be executed and use an integer to count iterations of the loop, as in this example:

float   f;
int     count = (last - first) / small;
for (f = first; count-- > 0; f += small)

10. How can you determine the maximum value that a numeric variable can hold?
The easiest way to find out how large or small a number that a particular type can hold is to use the values defined in the ANSI standard header file limits.h. This file contains many useful constants defining the values that can be held by various types, including these:

Value Description
CHAR_BIT-Number of bits in a char
CHAR_MAX-Maximum decimal integer value of a char
CHAR_MIN-Minimum decimal integer value of a char
MB_LEN_MAX-Maximum number of bytes in a multibyte character
INT_MAX-Maximum decimal value of an int
INT_MIN-Minimum decimal value of an int
LONG_MAX-Maximum decimal value of a long
LONG_MIN-Minimum decimal value of a long
SCHAR_MAX-Maximum decimal integer value of a signed char
SCHAR_MIN-Minimum decimal integer value of a signed char
SHRT_MAX-Maximum decimal value of a short
SHRT_MIN-Minimum decimal value of a short
UCHAR_MAX-Maximum decimal integer value of unsigned char
UINT_MAX-Maximum decimal value of an unsigned integer
ULONG_MAX-Maximum decimal value of an unsigned long int
USHRT_MAX-Maximum decimal value of an unsigned short int

For integral types, on a machine that uses two's complement arithmetic (which is just about any machine you're likely to use), a signed type can hold numbers from -2(number of bits - 1) to +2(number of bits - 1) - 1.

An unsigned type can hold values from 0 to +2(number of bits)- 1. For instance, a 16-bit signed integer can hold numbers from -215(-32768) to +215 - 1 (32767).

11. Are there any problems with performing mathematical operations on different variable types?
C has three categories of built-in data types: pointer types, integral types, and floating-point types. Pointer types are the most restrictive in terms of the operations that can be performed on them. They are limited to

- subtraction of two pointers, valid only when both pointers point to elements in the same array. The result is the same as subtracting the integer subscripts corresponding to the two pointers.

+ addition of a pointer and an integral type. The result is a pointer that points to the element which would be selected by that integer.

Floating-point types consist of the built-in types float, double, and long double. Integral types consist of char, unsigned char, short, unsigned short, int, unsigned int, long, and unsigned long. All of these types can have the following arithmetic operations performed on them:

+ Addition

- Subtraction

* Multiplication

/ Division

Integral types also can have those four operations performed on them, as well as the following operations: % Modulo or remainder of division

<< Shift left

>> Shift right

& Bitwise AND operation

| Bitwise OR operation

^ Bitwise exclusive OR operation

! Logical negative operation

~ Bitwise "one's complement" operation

Although C permits "mixed mode" expressions (an arithmetic expression involving different types), it actually converts the types to be the same type before performing the operations (except for the case of pointer arithmetic described previously). The process of automatic type conversion is called "operator promotion."

12. What is operator promotion?
If an operation is specified with operands of two different types, they are converted to the smallest type that can hold both values. The result has the same type as the two operands wind up having. To interpret the rules, read the following table from the top down, and stop at the first rule that applies.

If Either Operand Is And the Other Is Change Them To
long double-any other type-long double
double-any smaller type-double
float-any smaller type-float
unsigned long-any integral type-unsigned long
long-unsigned > LONG_MAX-long
long-any smaller type-long
unsigned-any signed type-unsigned

The following example code illustrates some cases of operator promotion. The variable f1 is set to 3/4. Because both 3 and 4 are integers, integer division is performed, and the result is the integer 0. The variable f2 is set to 3/4.0. Because 4.0 is a float, the number 3 is converted to a float as well, and the result is the float 0.75.

#include 
main()
{
    float f1 = 3 / 4;
    float f2 = 3 / 4.0;
    printf("3 / 4 == %g or %g depending on the type used.n", f1, f2);
}

13. When should a type cast be used?
There are two situations in which to use a type cast. The first use is to change the type of an operand to an arithmetic operation so that the operation will be performed properly. The variable f1 is set to the result of dividing the integer i by the integer j. The result is 0, because integer division is used. The variable f2 is set to the result of dividing i by j as well. However, the (float) type cast causes i to be converted to a float. That in turn causes floating-point division to be used and gives the result 0.75.

#include 
main()
{
    int i = 3;
    int j = 4;
    float f1 = i / j;
    float f2 = (float) i / j;
    printf("3 / 4 == %g or %g depending on the type used.n", f1, f2);
}

The second case is to cast pointer types to and from void * in order to interface with functions that expect or return void pointers. For example, the following line type casts the return value of the call to malloc() to be a pointer to a foo structure.

struct foo *p = (struct foo *) malloc(sizeof(struct foo));

14. When should a type cast not be used?
A type cast should not be used to override a const or volatile declaration. Overriding these type modifiers can cause the program to fail to run correctly.

A type cast should not be used to turn a pointer to one type of structure or data type into another. In the rare events in which this action is beneficial, using a union to hold the values makes the programmer's intentions clearer.

15. Is it acceptable to declare/define a variable in a C header?
A global variable that must be accessed from more than one file can and should be declared in a header file. In addition, such a variable must be defined in one source file. Variables should not be defined in header files, because the header file can be included in multiple source files, which would cause multiple definitions of the variable.

The ANSI C standard will allow multiple external defnitions, provided that there is only one initialization. But because there's really no advantage to using this feature, it's probably best to avoid it and maintain a higher level of portability.

"Global" variables that do not have to be accessed from more than one file should be declared static and should not appear in a header file.

16. What is the difference between declaring a variable and defining a variable?
Declaring a variable means describing its type to the compiler but not allocating any space for it. Defining a variable means declaring it and also allocating space to hold the variable. You can also initialize a variable at the time it is defined. Here is a declaration of a variable and a structure, and two variable definitions, one with initialization:

extern int decl1;  /* this is a declaration */
struct decl2 
{
    int member;
}; /* this just declares the type--no variable mentioned */
int     def1 = 8;      /* this is a definition */
int     def2;          /* this is a definition */

To put it another way, a declaration says to the compiler, "Somewhere in my program will be a variable with this name, and this is what type it is." A definition says, "Right here is this variable with this name and this type."

A variable can be declared many times, but it must be defined exactly once. For this reason, definitions do not belong in header files, where they might get #included into more than one place in your program.

17. Can static variables be declared in a header file?
You can't declare a static variable without defining it as well (this is because the storage class modifiers static and extern are mutually exclusive). A static variable can be defined in a header file, but this would cause each source file that included the header file to have its own private copy of the variable, which is probably not what was intended.

18. What is the benefit of using const for declaring constants?
The benefit of using the const keyword is that the compiler might be able to make optimizations based on the knowledge that the value of the variable will not change. In addition, the compiler will try to ensure that the values won't be changed inadvertently.

Of course, the same benefits apply to #defined constants. The reason to use const rather than #define to define a constant is that a const variable can be of any type (such as a struct, which can't be represented by a #defined constant). Also, because a const variable is a real variable, it has an address that can be used, if needed, and it resides in only one place in memory.

19. If errno contains a nonzero number, is there an error?
The global variable errno is used by many standard C library functions to pass back to your program an error code that denotes specifically which error occurred. However, your program should not check the value of errno to determine whether an error occurred.

Usually, the standard C library function you are calling returns with a return code which denotes that an error has occurred and that the value of errno has been set to a specific error number. If no error has occurred or if you are using a library function that does not reference errno, there is a good chance that errno will contain an erroneous value. For performance enhancement, the errno variable is sometimes not cleared by the functions that use it.

You should never rely on the value of errno alone; always check the return code from the function you are calling to see whether errno should be referenced. Refer to your compiler's library documentation for references to functions that utilize the errno global variable and for a list of valid values for errno.

20. What is a stream?
A stream is a continuous series of bytes that flow into or out of your program. Input and output from devices such as the mouse, keyboard, disk, screen, modem, and printer are all handled with streams. In C, all streams appear as files - not physical disk files necessarily, but rather logical files that refer to an input/output source. The C language provides five "standard" streams that are always available to your program. These streams do not have to be opened or closed. These are the five standard streams:

Name Description Example
stdin-Standard Input-Keyboard
stdout-Standard Output-Screen
stderr-Standard Error-Screen
stdprn-Standard Printer-LPT1: port
stdaux-Standard Auxiliary-COM1: port

Note that the stdprn and stdaux streams are not always defined. This is because LPT1: and COM1: have no meaning under certain operating systems. However, stdin, stdout, and stderr are always defined. Also, note that the stdin stream does not have to come from the keyboard; it can come from a disk file or some other device through what is called redirection. In the same manner, the stdout stream does not have to appear on-screen; it too can be redirected to a disk file or some other device. See the next FAQ for an explanation of redirection.

21. How do you redirect a standard stream?
Most operating systems, including DOS, provide a means to redirect program input and output to and from different devices. This means that rather than your program output (stdout) going to the screen, it can be redirected to a file or printer port. Similarly, your program's input (stdin) can come from a file rather than the keyboard. In DOS, this task is accomplished using the redirection characters, < and >. For example, if you wanted a program named PRINTIT.EXE to receive its input (stdin) from a file named STRINGS.TXT, you would enter the following command at the DOS prompt:

C:>PRINTIT < STRINGS.TXT

Notice that the name of the executable file always comes first. The less-than sign (<) tells DOS to take the strings contained in STRINGS.TXT and use them as input for the PRINTIT program.

Redirection of standard streams does not always have to occur at the operating system. You can redirect a standard stream from within your program by using the standard C library function named freopen(). For example, if you wanted to redirect the stdout standard stream within your program to a file named OUTPUT.TXT, you would implement the freopen() function as shown here:

...
freopen("output.txt", "w", stdout);
...

Now, every output statement (printf(), puts(), putch(), and so on) in your program will appear in the file OUTPUT.TXT.

22. How can you restore a redirected standard stream?
The preceding example showed how you can redirect a standard stream from within your program. But what if later in your program you wanted to restore the standard stream to its original state? By using the standard C library functions named dup() and fdopen(), you can restore a standard stream such as stdout to its original state.

The dup() function duplicates a file handle. You can use the dup() function to save the file handle corresponding to the stdout standard stream. The fdopen() function opens a stream that has been duplicated with the dup() function. Thus, as shown in the following example, you can redirect standard streams and restore them:

#include 
void main(void);

void main(void)
{
     int orig_stdout;
     /* Duplicate the stdout file handle and store it in orig_stdout. */
     orig_stdout = dup(fileno(stdout));
     /* This text appears on-screen. */
     printf("Writing to original stdout...n");
     /* Reopen stdout and redirect it to the "redir.txt" file. */
     freopen("redir.txt", "w", stdout);
     /* This text appears in the "redir.txt" file. */
     printf("Writing to redirected stdout...n");
     /* Close the redirected stdout. */
     fclose(stdout);
     /* Restore the original stdout and print to the screen again. */
     fdopen(orig_stdout, "w");
     printf("I'm back writing to the original stdout.n");
}

23. Can stdout be forced to print somewhere other than the screen?
Although the stdout standard stream defaults to the screen, you can force it to print to another device using something called redirection. For instance, consider the following program:

/* redir.c */
#include 
void main(void);
void main(void)
{
     printf("Let's get redirected!n");
}

At the DOS prompt, instead of entering just the executable name, follow it with the redirection character >, and thus redirect what normally would appear on-screen to some other device. The following example would redirect the program's output to the prn device, usually the printer attached on LPT1:

C:>REDIR > PRN

Alternatively, you might want to redirect the program's output to a file, as the following example shows:

C:>REDIR > REDIR.OUT

In this example, all output that would have normally appeared on-screen will be written to the file REDIR.OUT.

24. What is the difference between text and binary modes?
Streams can be classified into two types: text streams and binary streams. Text streams are interpreted, with a maximum length of 255 characters. With text streams, carriage return/line feed combinations are translated to the newline n character and vice versa. Binary streams are uninterpreted and are treated one byte at a time with no translation of characters. Typically, a text stream would be used for reading and writing standard text files, printing output to the screen or printer, or receiving input from the keyboard.

A binary text stream would typically be used for reading and writing binary files such as graphics or word processing documents, reading mouse input, or reading and writing to the modem.

25. How do you determine whether to use a stream function or a low-level function?
Stream functions such as fread() and fwrite() are buffered and are more efficient when reading and writing text or binary data to files. You generally gain better performance by using stream functions rather than their unbuffered low-level counterparts such as read() and >write().

In multiuser environments, however, when files are typically shared and portions of files are continuously being locked, read from, written to, and unlocked, the stream functions do not perform as well as the low-level functions. This is because it is hard to buffer a shared file whose contents are constantly changing.

Generally, you should always use buffered stream functions when accessing nonshared files, and you should always use the low-level functions when accessing shared files.

26. How do you list files in a directory?
A file's date and time are stored in the find_t structure returned from the _dos_findfirst() and _dos_findnext() functions.

The date and time stamp of the file is stored in the find_t.wr_date and find_t.wr_time structure members. The file date is stored in a two-byte unsigned integer as shown here:

Element Offset Range
Seconds-5 bits-0-9 (multiply by 2 to get the seconds value)
Minutes-6 bits-0-59
Hours-5 bits-0-23

Similarly, the file time is stored in a two-byte unsigned integer, as shown here:

Element Offset Range
Day-5 bits-1-31
Month-4 bits-1-12
Year-7 bits-0-127 (add the value "1980" to get the year value)

Because DOS stores a file's seconds in two-second intervals, only the values 0 to 29 are needed. You simply multiply the value by 2 to get the file's true seconds value. Also, because DOS came into existence in 1980, no files can have a time stamp prior to that year. Therefore, you must add the value "1980" to get the file's true year value.

The following example program shows how you can get a directory listing along with each file's date and time stamp:

#include 
#include 
#include 
#include 
#include 
#include 
typedef struct find_t FILE_BLOCK;
void main(void);
void main(void)
{
     FILE_BLOCK f_block;   /* Define the find_t structure variable */
     int ret_code;         /* Define a variable to store return codes */
     int hour;             /* We're going to use a 12-hour clock! */
     char* am_pm;          /* Used to print "am" or "pm" */
     printf("nDirectory listing of all files in this directory:nn");
     /* Use the "*.*" file mask and the 0xFF attribute mask to list all files in the directory, including system files, hidden files, and subdirectory names. */
     ret_code = _dos_findfirst("*.*", 0xFF, &f_block );
     /* The _dos_findfirst() function returns a 0 when it is successful and has found a valid filename in the directory. */
     while (ret_code == 0)
     {
          /* Convert from a 24-hour format to a 12-hour format. */


          hour = (f_block.wr_time >> 11);
          if (hour > 12)
          {
               hour  = hour - 12;
               am_pm = "pm";
          }
          else
               am_pm = "am";
          /* Print the file's name, date stamp, and time stamp. */
          printf("%-12s  %02d/%02d/%4d  %02d:%02d:%02d %sn",
                    f_block.name,                      /* name  */
                    (f_block.wr_date >> 5) & 0x0F,     /* month */
                    (f_block.wr_date) & 0x1F,          /* day   */
                    (f_block.wr_date >> 9) + 1980,     /* year  */
                    hour,                              /* hour  */
                    (f_block.wr_time >> 5) & 0x3F,     /* minute  */
                    (f_block.wr_time & 0x1F) * 2,      /* seconds */
                    am_pm);
          /* Use the _dos_findnext() function to look for the next file in the directory. */
          ret_code = _dos_findnext(&f_block );
     }
     printf("nEnd of directory listing.n");
}

Notice that a lot of bit-shifting and bit-manipulating had to be done to get the elements of the time variable and the elements of the date variable. If you happen to suffer from bitshiftophobia (fear of shifting bits), you can optionally code the preceding example by forming a union between the find_t structure and your own user-defined structure, such as this:

/* This is the find_t structure as defined by ANSI C. */
struct find_t
{
     char reserved[21];
     char attrib;
     unsigned wr_time;
     unsigned wr_date;
     long size;
     char name[13];
}
/* This is a custom find_t structure where we separate out the bits used for date and time. */
struct my_find_t
{
     char reserved[21];
     char attrib;
     unsigned seconds:5;
     unsigned minutes:6;
     unsigned hours:5;
     unsigned day:5;
     unsigned month:4;
     unsigned year:7;
     long size;
     char name[13];
}
/* Now, create a union between these two structures so that we can more easily access the elements of wr_date and wr_time. */
union file_info
{
     struct find_t ft;
     struct my_find_t mft;
}

Using the preceding technique, instead of using bit-shifting and bit-manipulating, you can now extract date and time elements like this:

...
file_info my_file;
...
printf("%-12s  %02d/%02d/%4d  %02d:%02d:%02d %sn",
    my_file.mft.name,          /* name    */
    my_file.mft.month,         /* month   */
    my_file.mft.day,           /* day     */
    (my_file.mft.year + 1980), /* year    */
    my_file.mft.hours,         /* hour    */
    my_file.mft.minutes,       /* minute  */
    (my_file.mft.seconds * 2), /* seconds */
    am_pm);
		  
		  

27. How do you list a file's date and time?
A file's date and time are stored in the find_t structure returned from the _dos_findfirst() and _dos_findnext() functions.

The date and time stamp of the file is stored in the find_t.wr_date and find_t.wr_time structure members. The file date is stored in a two-byte unsigned integer as shown here:

Element Offset Range
Seconds-5 bits-0-9 (multiply by 2 to get the seconds value)
Minutes-6 bits-0-59
Hours-5 bits-0-23

Similarly, the file time is stored in a two-byte unsigned integer, as shown here:

Element Offset Range
Day-5 bits-1-31
Month-4 bits-1-12
Year-7 bits-0-127 (add the value "1980" to get the year value)

Because DOS stores a file's seconds in two-second intervals, only the values 0 to 29 are needed. You simply multiply the value by 2 to get the file's true seconds value. Also, because DOS came into existence in 1980, no files can have a time stamp prior to that year. Therefore, you must add the value "1980" to get the file's true year value.

The following example program shows how you can get a directory listing along with each file's date and time stamp:

#include 
#include 
#include 
#include 
#include 
#include 
typedef struct find_t FILE_BLOCK;
void main(void);
void main(void)
{
     FILE_BLOCK f_block;   /* Define the find_t structure variable */
     int ret_code;         /* Define a variable to store return codes */
     int hour;             /* We're going to use a 12-hour clock! */
     char* am_pm;          /* Used to print "am" or "pm" */
     printf("nDirectory listing of all files in this directory:nn");
     /* Use the "*.*" file mask and the 0xFF attribute mask to list all files in the directory, including system files, hidden files, and subdirectory names. */
     ret_code = _dos_findfirst("*.*", 0xFF, &f_block );
     /* The _dos_findfirst() function returns a 0 when it is successful and has found a valid filename in the directory. */
     while (ret_code == 0)
     {
          /* Convert from a 24-hour format to a 12-hour format. */


          hour = (f_block.wr_time >> 11);
          if (hour > 12)
          {
               hour  = hour - 12;
               am_pm = "pm";
          }
          else
               am_pm = "am";
          /* Print the file's name, date stamp, and time stamp. */
          printf("%-12s  %02d/%02d/%4d  %02d:%02d:%02d %sn",
                    f_block.name,                      /* name  */
                    (f_block.wr_date >> 5) & 0x0F,     /* month */
                    (f_block.wr_date) & 0x1F,          /* day   */
                    (f_block.wr_date >> 9) + 1980,     /* year  */
                    hour,                              /* hour  */
                    (f_block.wr_time >> 5) & 0x3F,     /* minute  */
                    (f_block.wr_time & 0x1F) * 2,      /* seconds */
                    am_pm);
          /* Use the _dos_findnext() function to look for the next file in the directory. */
          ret_code = _dos_findnext(&f_block );
     }
     printf("nEnd of directory listing.n");
}

Notice that a lot of bit-shifting and bit-manipulating had to be done to get the elements of the time variable and the elements of the date variable. If you happen to suffer from bitshiftophobia (fear of shifting bits), you can optionally code the preceding example by forming a union between the find_t structure and your own user-defined structure, such as this:

/* This is the find_t structure as defined by ANSI C. */
struct find_t
{
     char reserved[21];
     char attrib;
     unsigned wr_time;
     unsigned wr_date;
     long size;
     char name[13];
}
/* This is a custom find_t structure where we separate out the bits used for date and time. */
struct my_find_t
{
     char reserved[21];
     char attrib;
     unsigned seconds:5;
     unsigned minutes:6;
     unsigned hours:5;
     unsigned day:5;
     unsigned month:4;
     unsigned year:7;
     long size;
     char name[13];
}
/* Now, create a union between these two structures so that we can more easily access the elements of wr_date and wr_time. */
union file_info
{
     struct find_t ft;
     struct my_find_t mft;
}

Using the preceding technique, instead of using bit-shifting and bit-manipulating, you can now extract date and time elements like this:

...
file_info my_file;
...
printf("%-12s  %02d/%02d/%4d  %02d:%02d:%02d %sn",
    my_file.mft.name,          /* name    */
    my_file.mft.month,         /* month   */
    my_file.mft.day,           /* day     */
    (my_file.mft.year + 1980), /* year    */
    my_file.mft.hours,         /* hour    */
    my_file.mft.minutes,       /* minute  */
    (my_file.mft.seconds * 2), /* seconds */
    am_pm);
		  
		  

28. How do you sort filenames in a directory?
When you are sorting the filenames in a directory, the one-at-a-time approach does not work. You need some way to store the filenames and then sort them when all filenames have been obtained. This task can be accomplished by creating an array of pointers to find_t structures for each filename that is found. As each filename is found in the directory, memory is allocated to hold the find_t entry for that file. When all filenames have been found, the qsort() function is used to sort the array of find_t structures by filename.

The qsort() function can be found in your compiler's library. This function takes four parameters: a pointer to the array you are sorting, the number of elements to sort, the size of each element, and a pointer to a function that compares two elements of the array you are sorting. The comparison function is a user-defined function that you supply. It returns a value less than zero if the first element is less than the second element, greater than zero if the first element is greater than the second element, or zero if the two elements are equal. Consider the following example:

#include 
#include 
#include 
#include 
#include 
#include 
typedef struct find_t FILE_BLOCK;
int  sort_files(FILE_BLOCK**, FILE_BLOCK**);
void main(void);
void main(void)
{
     FILE_BLOCK f_block;       /* Define the find_t structure variable */
     int ret_code;             /* Define a variable to store the return codes */
     FILE_BLOCK** file_list;   /* Used to sort the files */
     int file_count;           /* Used to count the files */
     int x;                    /* Counter variable */
     file_count = -1;
     /* Allocate room to hold up to 512 directory entries. */
     file_list = (FILE_BLOCK**) malloc(sizeof(FILE_BLOCK*) * 512);
     printf("nDirectory listing of all files in this directory:nn");
     /* Use the "*.*" file mask and the 0xFF attribute mask to list all files in the directory, including system files, hidden files, and subdirectory names. */
     ret_code = _dos_findfirst("*.*", 0xFF, &f_block);
     /* The _dos_findfirst() function returns a 0 when it is successful and has found a valid filename in the directory. */
     while (ret_code == 0 && file_count < 512)
     {
          /* Add this filename to the file list */
          file_list[++file_count] =
              (FILE_BLOCK*) malloc(sizeof(FILE_BLOCK));
          *file_list[file_count] = f_block;
          /* Use the _dos_findnext() function to look for the next file in the directory. */
          ret_code = _dos_findnext(&f_block);
     }
     /* Sort the files */
     qsort(file_list, file_count, sizeof(FILE_BLOCK*), sort_files);
     /* Now, iterate through the sorted array of filenames and print each entry. */
     for (x=0; x)
     {
          printf("%-12sn", file_list[x]->name);
     }
     printf("nEnd of directory listing.n");
}
int sort_files(FILE_BLOCK** a, FILE_BLOCK** b)
{
     return (strcmp((*a)->name, (*b)->name));
}

This example uses the user-defined function named sort_files() to compare two filenames and return the appropriate value based on the return value from the standard C library function strcmp(). Using this same technique, you can easily modify the program to sort by date, time, extension, or size by changing the element on which the sort_files() function operates.

29. How do you determine a file's attributes?
The file attributes are stored in the find_t.attrib structure member. This structure member is a single character, and each file attribute is represented by a single bit. Here is a list of the valid DOS file attributes:

Value Description Constant
0x00-Normal-(none)
0x01-Read Only-FA_RDONLY
0x02-Hidden File-FA_HIDDEN
0x04-System File-FA_SYSTEM
0x08-Volume Label-FA_LABEL
0x10-Subdirectory-FA_DIREC
0x20-Archive File-FA_ARCHIVE

To determine the file's attributes, you check which bits are turned on and map them corresponding to the preceding table. For example, a read-only hidden system file will have the first, second, and third bits turned on. A "normal" file will have none of the bits turned on. To determine whether a particular bit is turned on, you do a bit-wise AND with the bit's constant representation.

The following program uses this technique to print a file's attributes:

#include 
#include 
#include 
#include 
#include 
#include 
typedef struct find_t FILE_BLOCK;
void main(void);
void main(void)
{
     FILE_BLOCK f_block;  /* Define the find_t structure variable */
     int ret_code;     /* Define a variable to store the return codes */
     printf("nDirectory listing of all files in this directory:nn");
     /* Use the "*.*" file mask and the 0xFF attribute mask to list all files in the directory, including system files, hidden files, and subdirectory names. */
     ret_code = _dos_findfirst("*.*", 0xFF, &f_block);
     /* The _dos_findfirst() function returns a 0 when it is successful and has found a valid filename in the directory. */
     while (ret_code == 0)
     {
          /* Print the file's name */
          printf("%-12s  ", f_block.name);
          /* Print the read-only attribute */
          printf("%s ", (f_block.attrib & FA_RDONLY) ? "R" : ".");
          /* Print the hidden attribute */
          printf("%s ", (f_block.attrib & FA_HIDDEN) ? "H" : ".");
          /* Print the system attribute */
          printf("%s ", (f_block.attrib & FA_SYSTEM) ? "S" : ".");
          /* Print the directory attribute */
          printf("%s ", (f_block.attrib & FA_DIREC)  ? "D" : ".");
          /* Print the archive attribute */
          printf("%sn", (f_block.attrib & FA_ARCH)  ? "A" : ".");
          /* Use the _dos_findnext() function to look for the next file in the directory. */
          ret_code = _dos_findnext(&f_block);
     }
     printf("nEnd of directory listing.n");
}

30. How do you view the PATH?
Your C compiler library contains a function called getenv() that can retrieve any specified environment variable. It has one argument, which is a pointer to a string containing the environment variable you want to retrieve. It returns a pointer to the desired environment string on successful completion. If the function cannot find your environment variable, it returns NULL.

The following example program shows how to obtain the PATH environment variable and print it on-screen:

#include 
#include 
void main(void);
void main(void)
{
     char* env_string;
     env_string = getenv("PATH");
     if (env_string == (char*) NULL)
          printf("nYou have no PATH!n");
     else
          printf("nYour PATH is: %sn", env_string);
}

31. How can I open a file so that other programs can update it at the same time?
Your C compiler library contains a low-level file function called sopen() that can be used to open a file in shared mode. Beginning with DOS 3.0, files could be opened in shared mode by loading a special program named SHARE.EXE. Shared mode, as the name implies, allows a file to be shared with other programs as well as your own. Using this function, you can allow other programs that are running to update the same file you are updating.

The sopen() function takes four parameters: a pointer to the filename you want to open, the operational mode you want to open the file in, the file sharing mode to use, and, if you are creating a file, the mode to create the file in. The second parameter of the sopen() function, usually referred to as the "operation flag" parameter, can have the following values assigned to it:

Constant Description
O_APPEND-Appends all writes to the end of the file
O_BINARY-Opens the file in binary (untranslated) mode
O_CREAT-If the file does not exist, it is created
O_EXCL-If the O_CREAT flag is used and the file exists, returns an error
O_RDONLY-Opens the file in read-only mode
O_RDWR-Opens the file for reading and writing
O_TEXT-Opens the file in text (translated) mode
O_TRUNC-Opens an existing file and writes over its contents
O_WRONLY-Opens the file in write-only mode

The third parameter of the sopen() function, usually referred to as the "sharing flag," can have the following values assigned to it:

Constant Description
SH_COMPAT-No other program can access the file
SH_DENYRW-No other program can read from or write to the file
SH_DENYWR-No other program can write to the file
SH_DENYRD-No other program can read from the file
SH_DENYNO-Any program can read from or write to the file

If the sopen() function is successful, it returns a non-negative number that is the file's handle. If an error occurs, -1 is returned, and the global variable errno is set to one of the following values:

Constant Description
ENOENT-File or path not found
EMFILE-No more file handles are available
EACCES-Permission denied to access file
EINVACC-Invalid access code

The following example shows how to open a file in shared mode:

#include 
#include 
#include 
#include 
#include 
void main(void);
void main(void)
{
     int file_handle;
     /* Note that sopen() is not ANSI compliant */
     file_handle = sopen("C:DATATEST.DAT", O_RDWR, SH_DENYNO);
     close(file_handle);
}

Whenever you are sharing a file's contents with other programs, you should be sure to use the standard C library function named locking() to lock a portion of your file when you are updating it.

32. How can I make sure that my program is the only one accessing a file?
By using the sopen() function, you can open a file in shared mode and explicitly deny reading and writing permissions to any other program but yours. This task is accomplished by using the SH_DENYWR shared flag to denote that your program is going to deny any writing or reading attempts by other programs. For example, the following snippet of code shows a file being opened in shared mode, denying access to all other files:

/* Note that the sopen() function is not ANSI compliant... */
fileHandle = sopen("C:DATASETUP.DAT", O_RDWR, SH_DENYWR);

By issuing this statement, all other programs are denied access to the SETUP.DAT file. If another program were to try to open SETUP.DAT for reading or writing, it would receive an EACCES error code, denoting that access is denied to the file.

33. How can I prevent another program from modifying part of a file that I am modifying?
If your C compiler library comes with a function named locking() that can be used to lock and unlock portions of shared files.

The locking function takes three arguments: a handle to the shared file you are going to lock or unlock, the operation you want to perform on the file, and the number of bytes you want to lock. The file lock is placed relative to the current position of the file pointer, so if you are going to lock bytes located anywhere but at the beginning of the file, you need to reposition the file pointer by using the lseek() function.

The following example shows how a binary index file named SONGS.DAT can be locked and unlocked:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
void main(void);
void main(void)
{
     int file_handle, ret_code;
     char* song_name = "Six Months In A Leaky Boat";
     char rec_buffer[50];
     file_handle = sopen("C:DATASONGS.DAT", O_RDWR, SH_DENYNO);
     /* Assuming a record size of 50 bytes, position the file pointer to the 10th record. */
     lseek(file_handle, 450, SEEK_SET);
     /* Lock the 50-byte record. */
     ret_code = locking(file_handle, LK_LOCK, 50);
     /* Write the data and close the file. */
     memset(rec_buffer, '�', sizeof(rec_buffer));
     sprintf(rec_buffer, "%s", song_name);
     write(file_handle, rec_buffer, sizeof(rec_buffer));
     lseek(file_handle, 450, SEEK_SET);
     locking(file_handle, LK_UNLCK, 50);
     close(file_handle);
}

Notice that before the record is locked, the record pointer is positioned to the 10th record (450th byte) by using the lseek() function. Also notice that to write the record to the file, the record pointer has to be repositioned to the beginning of the record before unlocking the record.

34. How can I avoid the Abort, Retry, Fail messages?
When DOS encounters a critical error, it issues a call to interrupt 24, the critical error handler. Your C compiler library contains a function named hardler() that takes over the handling of calls to interrupt 24. The hardler() function takes one argument, a pointer to a function that is called if there is a hardware error.

Your user-defined hardware error-handling function is passed information regarding the specifics of the hardware error that occurred. In your function, you can display a user-defined message to avoid the ugly Abort, Retry, Fail message. This way, your program can elegantly handle such simple user errors as your not inserting the disk when prompted to do so.

When a hardware error is encountered and your function is called, you can either call the C library function hardretn() to return control to your application or call the C library function hardresume() to return control to DOS. Typically, disk errors can be trapped and your program can continue by using the hardresume() function. Other device errors, such as a bat FAT (file allocation table) error, are somewhat fatal, and your application should handle them by using the hardretn() function. Consider the following example, which uses the hardler() function to trap for critical errors and notifies the user when such an error occurs:

#include 
#include 
#include 
#include 
void main(void);
void far error_handler(unsigned, unsigned, unsigned far*);
void main(void)
{
     int file_handle, ret_code;
     /* Install the custom error-handling routine. */
     _harderr(error_handler);
     printf("nEnsure that the A: drive is empty, n");
     printf("then press any key.nn");
     getch();
     printf("Trying to write to the A: drive...nn");
     /* Attempt to access an empty A: drive... */
     ret_code = _dos_open("A:FILE.TMP", O_RDONLY, &file_handle);
     /* If the A: drive was empty, the error_handler() function was called. Notify the user of the result of that function. */
     switch (ret_code)
     {
          case 100: printf("Unknown device error!n");
                    break;
          case 2:   printf("FILE.TMP was not found on drive A!n");
                    break;
          case 0:   printf("FILE.TMP was found on drive A!n");
                    break;
          default:  printf("Unknown error occurred!n");
                    break;
     }
}
void far error_handler(unsigned device_error, unsigned error_val, unsigned far* device_header)
{
     long x;
     /* This condition will be true only if a nondisk error occurred. */
     if (device_error & 0x8000)
          _hardretn(100);
     /* Pause one second. */
     for (x=0; x<2000000; x++);
         /* Retry to access the drive. */
         _hardresume(_HARDERR_RETRY);
}

In this example, a custom hardware error handler is installed named error_handler(). When the program attempts to access the A: drive and no disk is found there, the error_handler() function is called. The error_handler() function first checks to ensure that the problem is a disk error. If the problem is not a disk error, it returns 100 by using the hardretn() function. Next, the program pauses for one second and issues a hardresume() call to retry accessing the A: drive.

35. How can I read and write comma-delimited text?
Many of today's popular programs use comma-delimited text as a means of transferring data from one program to another, such as the exported data from a spreadsheet program that is to be imported by a database program. Comma-delimited means that all data (with the exception of numeric data) is surrounded by double quotation marks ("") followed by a comma. Numeric data appears as-is, with no surrounding double quotation marks. At the end of each line of text, the comma is omitted and a newline is used.

To read and write the text to a file, you would use the fprintf() and fscanf() standard C library functions. The following example shows how a program can write out comma-delimited text and then read it back in.

#include 
#include 
typedef struct name_str
{
     char       first_name[15];
     char       nick_name[30];
     unsigned years_known;
} NICKNAME;
NICKNAME nick_names[5];
void main(void);
void set_name(unsigned, char*, char*, unsigned);
void main(void)
{
     FILE*     name_file;
     int       x;
     NICKNAME tmp_name;
     printf("nWriting data to NICKNAME.DAT, one moment please...n");
     /* Initialize the data with some values... */
     set_name(0, "Sheryl", "Basset",    26);
     set_name(1, "Joel",   "Elkinator",  1);
     set_name(2, "Cliff",  "Shayface",  12);
     set_name(3, "Lloyd",  "Lloydage",  28);
     set_name(4,  "Scott", "Pie",      9);
     /* Open the NICKNAME.DAT file for output in text mode. */
     name_file = fopen("NICKNAME.DAT", "wt");
     /* Iterate through all the data and use the fprintf() function to write the data to a file. */
     for (x=0; x<5; x++)
     {
          fprintf(name_file, ""%s", "%s", %un",
                 nick_names[x].first_name,
                 nick_names[x].nick_name,
                 nick_names[x].years_known);
     }
     /* Close the file and reopen it for input. */
     fclose(name_file);
     printf("nClosed NICKNAME.DAT, reopening for input...n");
     name_file = fopen("NICKNAME.DAT", "rt");
     printf("nContents of the file NICKNAME.DAT:nn");
     /* Read each line in the file using the scanf() function and print the file's contents. */
     while (1)
     {
          fscanf(name_file, "%s %s %u",
                     tmp_name.first_name,
                     tmp_name.nick_name,
                     &tmp_name.years_known);
          if (feof(name_file))
               break;
          printf("%-15s %-30s %un",
                     tmp_name.first_name,
                     tmp_name.nick_name,
                     tmp_name.years_known);
     }
     fclose(name_file);
}
void set_name(unsigned name_num, char* f_name, char* n_name, unsigned years)
{
   strcpy(nick_names[name_num].first_name, f_name);
   strcpy(nick_names[name_num].nick_name,  n_name);
   nick_names[name_num].years_known = years;
}

User Answer

  1. Be the first one to express your answer.

Miscellaneous interview question and answer for C programming