You are on page 1of 4

C Interview Questions And Answ ers

[C Frequently

Asked Questions ,C FAQ ]

What are the advantages of the functions?


-

Debugging is easier
It is easier to understand the logic involved in the program
Testing is easier
Recursive call is possible
Irrelevant details in the user point of view are hidden in functions
Functions are helpful in generalizing the program

Is NULL always defined as 0?


NULL is defined as either 0 or (void*)0. These values are almost identical; either a
literal zero or a void pointer is converted automatically to any kind of pointer, as
necessary, whenever a pointer is needed (although the compiler cant always tell
when a pointer is needed).

What is the difference between NULL and NUL?


NULL is a macro defined in for the null pointer.
NUL is the name of the first character in the ASCII character set. It corresponds to
a zero value. Theres no standard macro NUL in C, but some people like to define it.
The digit 0 corresponds to a value of 80, decimal. Dont confuse the digit 0 with the
value of (NUL)! NULL can be defined as ((void*)0), NUL as .

Can the sizeof operator be used to tell the size of an array passed to a
function?
No. Theres no way to tell, at runtime, how many elements are in an array
parameter just by looking at the array parameter itself. Remember, passing an array
to a function is exactly the same as passing a pointer to the first element.

Is using exit() the same as using return?


No. The exit() function is used to exit your program and return control to the
operating system. The return statement is used to return from a function and
return control to the calling function. If you issue a return from the main() function,
you are essentially returning control to the calling function, which is the operating
system. In this case, the return statement and exit() function are similar.

Can math operations be performed on a void pointer?


No. Pointer addition and subtraction are based on advancing the pointer by a
number of elements. By definition, if you have a void pointer, you dont know what
its pointing to, so you dont know the size of what its pointing to. If you want
pointer arithmetic to work on raw addresses, use character pointers.

Can the size of an array be declared at runtime?


No. In an array declaration, the size must be known at compile time. You cant

specify a size thats known only at runtime. For example, if i is a variable, you cant
write code like this:
char array[i]; /* not valid C */
Some languages provide this latitude. C doesnt. If it did, the stack would be more
complicated, function calls would be more expensive, and programs would run a lot
slower. If you know that you have an array but you wont know until runtime how
big it will be, declare a pointer to it and use malloc() or calloc() to allocate the array
from the heap.

Can you add pointers together? Why would you?


No, you cant add pointers together. If you live at 1332 Lakeview Drive, and your
neighbor lives at 1364 Lakeview, whats 1332+1364? Its a number, but it doesnt
mean anything. If you try to perform this type of calculation with pointers in a C
program, your compiler will complain.
The only time the addition of pointers might come up is if you try to add a pointer
and the difference of two pointers.

Are pointers integers?


No, pointers are not integers. A pointer is an address. It is merely a positive number
and not an integer.

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 programs 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.
The following example would redirect the programs output to the prn device,
usually the printer attached on LPT1:
C :> REDIR > PRN
Alternatively, you might want to redirect the programs 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.
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.

What is a method?
Method is a way of doing something, especially a systematic way; implies an orderly
logical arrangement (usually in steps).

What is the easiest searching method to use?


Just as qsort() was the easiest sorting method, because it is part of the standard
library, bsearch() is the easiest searching method to use. If the given array is in the
sorted order bsearch() is the best method.
Following is the prototype for bsearch():
void *bsearch(const void *key, const void *buf, size_t num, size_t size, int (*comp)
(const void *, const void*));
Another simple searching method is a linear search. A linear search is not as fast as
bsearch() for searching among a large number of items, but it is adequate for many
purposes. A linear search might be the only method available, if the data isnt
sorted or cant be accessed randomly. A linear search starts at the beginning and
sequentially compares the key to each element in the data set.

Is it better to use a pointer to navigate an array of values, or is it better to use a


subscripted array name?
Its easier for a C compiler to generate good code for pointers than for subscripts.

What is indirection?
If you declare a variable, its name is a direct reference to its value. If you have a
pointer to a variable, or any other object in memory, you have an indirect reference
to its value.

How are portions of a program disabled in demo versions?


If you are distributing a demo version of your program, the preprocessor can be
used to enable or disable portions of your program. The following portion of code
shows how this task is accomplished, using the preprocessor directives #if and
#endif:
int save_document(char* doc_name)

{
#if DEMO_VERSION
printf(Sorry! You cant save documents using the DEMO version of this
programming);
return(0);
#endif

...

You might also like