Showing posts with label c interview book. Show all posts
Showing posts with label c interview book. Show all posts

Preprocessors in c


1. What is a macro, and how do you use it?
A macro is a preprocessor directive that provides a mechanism for token replacement in your source code. Macros are created by using the #define statement. Here is an example of a macro:
#define VERSION_STAMP "1.02"
The macro being defined in this example is commonly referred to as a symbol. The symbol VERSION_STAMP is simply a physical representation of the string "1.02". When the preprocessor is invoked, every occurrence of the VERSION_STAMP symbol is replaced with the literal string "1.02". Here is another example of a macro:
#define CUBE(x) ((x) * (x) * (x))
The macro being defined here is named CUBE, and it takes one argument, x. The rest of the code on the line represents the body of the CUBE macro. Thus, the simplistic macro CUBE(x) will represent the more complex expression ((x) * (x) * (x)). When the preprocessor is invoked, every instance of the macro CUBE(x) in your program is replaced with the code ((x) * (x) * (x)).
Macros can save you many keystrokes when you are coding your program. They can also make your program much more readable and reliable, because you enter a macro in one place and use it in potentially several places. There is no overhead associated with macros, because the code that the macro represents is expanded in-place, and no jump in your program is invoked. Additionally, the arguments are not type-sensitive, so you don't have to worry about what data type you are passing to the macro.
Note that there must be no white space between your macro name and the parentheses containing the argument definition. Also, you should enclose the body of the macro in parentheses to avoid possible ambiguity regarding the translation of the macro. For instance, the following example shows the CUBE macro defined incorrectly:
#define CUBE (x) x * x * x
You also should be careful with what is passed to a macro. For instance, a very common mistake is to pass an incremented variable to a macro, as in the following example:
#include <stdio.h>
#define CUBE(x) (x*x*x)
void main(void);
void main(void)
{
     int x, y;
     x = 5;
     y = CUBE(++x);
     printf("y is %d\n", y);
}
What will y be equal to? You might be surprised to find out that y is not equal to 125 (the cubed value of 5) and not equal to 336 (6 * 7 * 8), but rather is 512. This is because the variable x is incremented while being passed as a parameter to the macro. Thus, the expanded CUBE macro in the preceding example actually appears as follows:
y = ((++x) * (++x) * (++x));
Each time x is referenced, it is incremented, so you wind up with a very different result from what you had intended. Because x is referenced three times and you are using a prefix increment operator, x is actually 8 when the code is expanded. Thus, you wind up with the cubed value of 8 rather than 5. This common mistake is one you should take note of because tracking down such bugs in your software can be a very frustrating experience. I personally have seen this mistake made by people with many years of C programming under their belts. I recommend that you type the example program and see for yourself how surprising the resulting value (512) is.
Macros can also utilize special operators such as the stringizing operator (#) and the concatenation operator (##). The stringizing operator can be used to convert macro parameters to quoted strings, as in the following example:
#define DEBUG_VALUE(v) printf(#v " is equal to %d.\n", v)
In your program, you can check the value of a variable by invoking the DEBUG_VALUE macro: ... int x = 20; DEBUG_VALUE(x); ... The preceding code prints "x is equal to 20." on-screen. This example shows that the stringizing operator used with macros can be a very handy debugging tool.
The concatenation operator (##) is used to concatenate (combine) two separate strings into one single string.
2. What will the preprocessor do for a program?
The C preprocessor is used to modify your program according to the preprocessor directives in your source code. A preprocessor directive is a statement (such as #define) that gives the preprocessor specific instructions on how to modify your source code. The preprocessor is invoked as the first part of your compiler program's compilation step. It is usually hidden from the programmer because it is run automatically by the compiler.
The preprocessor reads in all of your include files and the source code you are compiling and creates a preprocessed version of your source code. This preprocessed version has all of its macros and constant symbols replaced by their corresponding code and value assignments. If your source code contains any conditional preprocessor directives (such as #if), the preprocessor evaluates the condition and modifies your source code accordingly.
Here is an example of a program that uses the preprocessor extensively:
#include <stdio.h>
#define TRUE         1
#define FALSE        (!TRUE)
#define GREATER(a,b) ((a) > (b) ? (TRUE) : (FALSE))
#define PIG_LATIN    FALSE
void main(void);
void main(void)
{
     int x, y;
#if PIG_LATIN
     printf("Easeplay enternay ethay aluevay orfay xnay: ");
     scanf("%d", &x);
     printf("Easeplay enternay ethay aluevay orfay ynay: ");
     scanf("%d", &y);
#else
     printf("Please enter the value for x: ");
     scanf("%d", &x);
     printf("Please enter the value for y: ");
     scanf("%d", &y);
#endif
     if (GREATER(x,y) == TRUE)
     {
#if PIG_LATIN
          printf("xnay islay eatergray anthay ynay!\n");
#else
          printf("x is greater than y!\n");
#endif
     }
     else
     {
     #if PIG_LATIN
          printf("xnay islay otnay eatergray anthay ynay!\n");
#else
          printf("x is not greater than y!\n");
#endif
     }
}
This program uses preprocessor directives to define symbolic constants (such as TRUE, FALSE, and PIG_LATIN), a macro (such as GREATER(a,b)), and conditional compilation (by using the #if statement). When the preprocessor is invoked on this source code, it reads in the stdio.h file and interprets its preprocessor directives, then it replaces all symbolic constants and macros in your program with the corresponding values and code. Next, it evaluates whether PIG_LATIN is set to TRUE and includes either the pig latin text or the plain English text.
If PIG_LATIN is set to FALSE, as in the preceding example, a preprocessed version of the source code would look like this:
/* Here is where all the include files
   would be expanded. */
void main(void)
{
     int x, y;
     printf("Please enter the value for x: ");
     scanf("%d", &x);
     printf("Please enter the value for y: ");
     scanf("%d", &y);
     if (((x) > (y) ? (1) : (!1)) == 1)
     {
          printf("x is greater than y!\n");
     }
     else
     {
          printf("x is not greater than y!\n");
     }
}
This preprocessed version of the source code can then be passed on to the compiler. If you want to see a preprocessed version of a program, most compilers have a command-line option or a standalone preprocessor program to invoke only the preprocessor and save the preprocessed version of your source code to a file. This capability can sometimes be handy in debugging strange errors with macros and other preprocessor directives, because it shows your source code after it has been run through the preprocessor.
3. How can you avoid including a header more than once?
One easy technique to avoid multiple inclusions of the same header is to use the #ifndef and #define preprocessor directives. When you create a header for your program, you can #define a symbolic name that is unique to that header. You can use the conditional preprocessor directive named #ifndef to check whether that symbolic name has already been assigned. If it is assigned, you should not include the header, because it has already been preprocessed. If it is not defined, you should define it to avoid any further inclusions of the header. The following header illustrates this technique:
#ifndef _FILENAME_H
#define _FILENAME_H
#define VER_NUM      "1.00.00"
#define REL_DATE     "08/01/94"
#if __WINDOWS__
#define OS_VER       "WINDOWS"
#else
#define OS_VER       "DOS"
#endif
#endif
When the preprocessor encounters this header, it first checks to see whether _FILENAME_H has been defined. If it hasn't been defined, the header has not been included yet, and the _FILENAME_H symbolic name is defined. Then, the rest of the header is parsed until the last #endif is encountered, signaling the end of the conditional #ifndef _FILENAME_H statement. Substitute the actual name of the header file for "FILENAME" in the preceding example to make it applicable for your programs.
4. Can a file other than a .h file be included with #include?
The preprocessor will include whatever file you specify in your #include statement. Therefore, if you have the line
#include <macros.inc>
in your program, the file macros.inc will be included in your precompiled program. It is, however, unusual programming practice to put any file that does not have a .h or .hpp extension in an #include statement. You should always put a .h extension on any of your C files you are going to include. This method makes it easier for you and others to identify which files are being used for preprocessing purposes.
For instance, someone modifying or debugging your program might not know to look at the macros.inc file for macro definitions. That person might try in vain by searching all files with .h extensions and come up empty. If your file had been named macros.h, the search would have included the macros.h file, and the searcher would have been able to see what macros you defined in it.
 www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

printf() arguments program interview question


Processing printf() arguments

Question: What would be the output of the following code?
#include

int main(void)
{
    int a = 10, b = 20, c = 30;

    printf("\n %d..%d..%d \n", a+b+c, (b = b*2), (c = c*2));

    return 0;
}
Answer: The output of the above code would be :
110..40..60
This is because the arguments to the function are processed from right to left but are printed from left to right.

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

Write program to change process name


Process that changes its own name

Question: Can you write a program that changes its own name when run?
Answer: Following piece of code tries to do the required :
#include

int main(int argc, char *argv[])
{
    int i = 0;
    char buff[100];

    memset(buff,0,sizeof(buff));

    strncpy(buff, argv[0], sizeof(buff));
    memset(argv[0],0,strlen(buff));

    strncpy(argv[0], "NewName", 7);

    // Simulate a wait. Check the process
    // name at this point.
    for(;i<0xffffffff 0="" i="" pre="" return="">
www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

Making changes in Code Segmentation issue


Making changes in Code(or read-only) segment

Question: The following code seg-faults (crashes). Can you tell the reason why?
#include

int main(void)
{
    char *ptr = "Linux";
    *ptr = 'T';

    printf("\n [%s] \n", ptr);

    return 0;
}
Answer: This is because, through *ptr = ‘T’, the code is trying to change the first byte of the string ‘Linux’ kept in the code (or the read-only) segment in the memory. This operation is invalid and hence causes a seg-fault or a crash.

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

* and ++ operators c interview question


* and ++ operators c interview questions

Question: What would be the output of the following code and why?
#include

int main(void)
{
    char *ptr = "Linux";
    printf("\n [%c] \n",*ptr++);
    printf("\n [%c] \n",*ptr);

    return 0;
}
Answer: The output of the above would be :
[L] 

[i]
Since the priority of both ‘++’ and ‘*’ are same so processing of ‘*ptr++’ takes place from right to left. Going by this logic, ptr++ is evaluated first and then *ptr. So both these operations result in ‘L’. Now since a post fix ‘++’ was applied on ptr so the next printf() would print ‘i’.

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

void* and C structures interview question


void* and C structures interview question
Question: Can you design a function that can accept any type of argument and returns an integer? Also, is there a way in which more than one arguments can be passed to it?
Answer: A function that can accept any type of argument looks like :
 int func(void *ptr)
if more than one argument needs to be passed to this function then this function could be called with a structure object where-in the structure members can be populated with the arguments that need to be passed.

www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

Memory Leak interview question c programming


Memory Leak interview question

Question: Will the following code result in memory leak?
#include

void main(void)
{
    char *ptr = (char*)malloc(10);

    if(NULL == ptr)
    {
        printf("\n Malloc failed \n");
        return;
    }
    else
    {
        // Do some processing
    }

    return;
}
Answer: Well, Though the above code is not freeing up the memory allocated to ‘ptr’ but still this would not cause a memory leak as after the processing is done the program exits. Since the program terminates so all the memory allocated by the program is automatically freed as part of cleanup. But if the above code was all inside a while loop then this would have caused serious memory leaks.


www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

c fundamental interview questions


In this articles we would discuss common problem but intresting interview questions in C Programming Language
1) What is gets() function?how to use it?hidden issues with this function?
2) Do you know Strcpy()function?How to use it?
3) What is the  Return type of main()?
4) Memory Leak?Explain?
5) The free() function?Explain?
6) atexit with _exit ?Explain?
7) void* and C structures?Explain
8)  * and ++ operators?Explain?
9)  Making changes in Code(or read-only) segment?
10) Process that changes its own name?Write program?
11) Returning address of local variable?Write program?
12) Processing printf() arguments ?Explain?
www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

C interview questions,C interview reference questions

# What does the term cast refer to? Why is it used?
A Casting is a mechanism built into C that allows the programmer to force the conversion of data types. This may be needed because most C functions are very particular about the data types they process. A programmer may wish to override the default way the C compiler promotes data types.
# In arithmetic expressions, to what data type will the C compiler promote a character?

It will promote it to an integer unless otherwise directed.
# What is the difference between a statement and a block?
A statement is a single C expression terminated with a semicolon. A block is a series of statements, the group of which is enclosed in curly-braces.
# Increment the variable next three different ways.
next = next + 1;
and
next++;
and
next += 1;
# How is a comment formed in C.
Comments in C begin with a slash followed by an asterisk. Any text may then appear including newlines. The comment is finished with an asterisk followed by a slash. Example:
/* This is a comment */
# Can comments be nested?
Not in standard (K&R) C.
# From the standpoint of programming logic, what is the difference between a loop with the test at the top, and a loop where the test is at the bottom?
If the test is at the bottom, the body of the loop will always be executed at least once. When the test is at the top, the body of the loop may never be executed.
# Specify the skeletons of two C loops with the test at the top.
next = 0; /* setup */
while ( next < max) { /* test */ printf("Hello "); /* body */ next++; /* update */ } and for ( next = 0; next < max; next++) /* setup,test */ /* and update */ printf("Hello"); /* body */ # Specify a C loop with the test at the bottom.
next = 0; /* setup */
do { printf("Hello"); /* body */ next++; /* update */ } while ( next < max); /* test */ # What is the switch statement?
It is C's form of multiway-conditional (a.k.a case statement in Pascal).
# What does a break statement do? Which control structures use it?
The break statement unconditionally ends the execution of the smallest enclosing while, do, for or switch statement.
# In a loop, what is the difference between a break and continue statement?
The break terminates the loop. The continue branches immediately to the test portion of the loop.
# Where may variables be defined in C?
Outside a function definition (global scope, from the point of definition downward in the source code). Inside a block before any statements other than variable declarations (local scope with respect to the block).
# What is the difference between a variable definition and a variable declaration?
A definition tells the compiler to set aside storage for the variable. A declaration makes the variable known to parts of the program that may wish to use it. A variable might be defined and declared in the same statement.
# What is the purpose of a function prototype?
A function prototype tells the compiler to expect a given function to be used in a given way. That is, it tells the compiler the nature of the parameters passed to the function (the quantity, type and order) and the nature of the value returned by the function.
# What is type checking?
The process by which the C compiler ensures that functions and operators use data of the appropriate type(s). This form of check helps ensure the semantic correctness of the program.
# To what does the term storage class refer?
This is a part of a variable declaration that tells the compiler how to interpret the variable's symbol. It does not in itself allocate storage, but it usually tells the compiler how the variable should be stored.
# List C's storage classes and what they signify.
static - Variables are defined in a nonvolatile region of memory such that they retain their contents though out the program's execution.
register - Asks the compiler to devote a processor register to this variable in order to speed the program's execution. The compiler may not comply and the variable looses it contents and identity when the function it which it is defined terminates.
extern - Tells the compiler that the variable is defined in another module.
volatile - Tells the compiler that other programs will be modifying this variable in addition to the program being compiled. For example, an I/O device might need write directly into a program or data space. Meanwhile, the program itself may never directly access the memory area in question. In such a case, we would not want the compiler to optimize-out this data area that never seems to be used by the program, yet must exist for the program to function correctly in a larger context.
# State the syntax for the printf() and scanf() functions. State their one crucial difference with respect to their parameters.
Where fmtStr tells printf() how to format the variable list that follows. var1 through varN may be variables of any base type.
scanf( fmtStr, &var1, &var2, &varN);
This routine is the input compliment to printf().
scanf() requires the address of each variable instead of the variable's value (as in printf()). This is subtle source of serious bugs.
# With respect to function parameter passing, what is the difference between call-by-value and call-by-reference? Which method does C use?
In the case of call-by-reference, a pointer reference to a variable is passed into a function instead of the actual value. The function's operations will effect the variable in a global as well as local sense. Call-by-value (C's method of parameter passing), by contrast, passes a copy of the variable's value into the function. Any changes to the variable made by function have only a local effect and do not alter the state of the variable passed into the function.
# What is a structure and a union in C?
A structure is an aggregate data type. It combines one or more base or aggregate data types into a package that may treated as a whole. A structure is like a record in other languages. A union combines two or more data types in the same area of storage. The contents of a union may be one data type at one time and another type at a different time. A union is sometimes called a trick- record.
# Define a structure for a simple name/address record.
struct nameAddr {
char name[30];
char addr[30];
char city[20];
char state[3];
char zip[5];
};
# What does the typedef keyword do?
This keyword provides a short-hand way to write variable declarations. It is not a true data typing mechanism, rather, it is syntactic "sugar coating."
# Use typedef to make a short-cut way to declare a pointer to the nameAddr structure above. Call it addrPtr.
typedef struct nameAddr *addrPtr;
# Declare a variable with addrPtr called address.
addrPtr address;
# Assuming the variable address above, how would one refer to the city portion of the record within a C expression?
address->city
# What is the difference between: #include and #include "stdio.h"
They both specify a file for inclusion into the current source file. The difference is where the file stdio.h is expected to be. In the case of the brackets, the compiler will look in all the default locations. In the case of the quotes, the compiler will only look in the current directory.
# What is #ifdef used for?
It is used for condition compilation. Specifically the source code between #ifdef and #endif (or #else) is compiled if the associated symbol is defined to the compiler.
# How do you define a constant in C?
The C language itself has no provision for constants. However, its companion program, the preprocessor, can be used to make manifest constants. It does this through the use of the #define keyword.
# Why can't you nest structure definitions?
Trick question: You can nest structure definitions.
# Can you nest function definitions?
No. (You can in Pascal, a close relative to C.)
# What is a forward reference?
It is a reference to a variable or function before it is defined to the compiler. The cardinal rule of structured languages is that everything must be defined before it can be used. There are rare occasions where this is not possible. It is possible (and sometimes necessary) to define two functions in terms of each other. One will obey the cardinal rule while the other will need a forward declaration of the former in order to know of the former's existence. Confused?
# What are the following and how do they differ: int, long, float and double?
int An integer, usually +/- 215 in magnitude.
long A larger version of int, usually +/- 231 in magnitude.
float A single precision real (floating point) number. Magnitude varies.
double A double precision real number. Magnitude varies.
# Define a macro called SQR which squares a number.
#define SQR(x) (x * x)
(The parenthesis around "x * x" are extremely important because the macro may be expanded into a place where any embedded spaces could cause the compiler to misinterpret the expression. The consequences could range from a pesky syntax error to wrong answers when the program is run.). Moral: The preprocessor does not know C.
# Is it possible to take the square-root of a number in C. Is there a square-root operator in C?
Yes. There is no square-root operator; such computation is performed though the use of a function.
# Using fprintf() print a single floating point number right-justified in a field of 20 spaces, no leading zeros, and 4 decimal places. The destination should be stderr and the variable is called num.
fprintf( stderr, "%-20.4f", num);
# What is the difference between the & and && operators and the | and || operators?
& and | are bitwise AND and OR operators respectively. They are usually used to manipulate the contents of a variable on the bit level. && and || are logical AND and OR operators respectively. They are usually used in conditionals.
# What is the difference between the -> and . operators?
They both provide access to members of a structure or union. They differ in that -> is used when the variable is a pointer to a structure or union. The dot is used when the variable is itself the structure or union. The -> operator combines the pointer dereferencing operator with the member access operator; it is syntactic "sugar coating."
address->city is equivalent to (*address).city.
# What is the symbol for the modulus operator?
% (the percent symbol)
# From the standpoint of logic, what is the difference between the fragment:
if (next < max) next++; else next = 0; and the fragment: next += (next < max)? (1):(-next); Nothing. They are different ways to express the same logic. # What does the following fragment do?
while((d=c=getch(),d)!=EOF&&(c!='\t'||c!=' '||c!='\b')) *buff++ = ++c;
Do the following until either the end of standard input or the variable c takes on the value of a tab, space, or backspace character: Store the character that succeeds the character stored in c into the current location pointed by buff. Then increment buff to point to the next location in memory. Meanwhile, d is assigned the same value as c and it is the value of d that is used in the comparison to EOF.
# Is C case sensitive (ie: does C differentiate between upper and lower case letters)?
Yes.
# Specify how a filestream called inFile should be opened for random reading and writing. the file's name is in fileName.
inFile = fopen( fileName, "r+");
# What does fopen() return if successful. If unsuccessful?
Upon success fopen() returns a pointer to a filestream. Otherwise it returns the value of NULL.
# What is the void data type? What is a void pointer?
The void data type is used when no other data type is appropriate. A void pointer is a pointer that may point to any kind of object at all. It is used when a pointer must be specified but its type is unknown.
# Declare a pointer called fnc which points to a function that returns an unsigned long.
unsigned long (*fnc)();

# Declare a pointer called pfnc which points to a function that returns a pointer to a structure of type nameAddr.
struct nameAddr *(*pfnc)();
# It is possible for a function to return a character, an integer, and a floating point number. Is it possible for a function to return a structure? Another function?
No. However, it is possible to return pointers to structures and functions.
# What is the difference between an lvalue and an rvalue?
The lvalue refers to the left-hand side of an assignment expression. It must always evaluate to a memory location. The rvalue represents the right-hand side of an assignment expression; it may have any meaningful combination of variables and constants.
# Given the decimal number 27, how would one express it as a hexadecimal number in C?
0x1B
# What is malloc()?
This function allocates heap storage for dynamic data structures.
# What is the difference between malloc() and calloc()?
The malloc() function allocates raw memory given a size in bytes. On the other hand, calloc() clears the requested memory to zeros before return a pointer to it. (It can also compute the request size given the size of the base data structure and the number of them desired.)
# What kind of problems was C designed to solve?
C was designed to be a "universal" assembly language. It is used for producing system software such as operation systems, compilers/interpreters, device drivers, editors, DBMS's and similar things. It is not as well suited to application programs.
# write C code for deleting an element from a linked listy traversing a linked list efficient way of elimiating duplicates from an array
# Declare a void pointer
void *ptr;

# Make the pointer aligned to a 4 byte boundary in a efficient manner
assign the pointer to a long number and the number with 11...1100 add 4 to the number

# What is a far pointer (in DOS)

# Write an efficient C code for 'tr' program. 'tr' has two command line arguments. They both are strings of same length. tr reads an input file, replaces each character in the first string with the corresponding character in the second string. eg. 'tr abc xyz' replaces all 'a's by 'x's, 'b's by 'y's and so on.
have an array of length 26.

put 'x' in array element corr to 'a'
put 'y' in array element corr to 'b'
put 'z' in array element corr to 'c'
put 'd' in array element corr to 'd'
put 'e' in array element corr to 'e'
and so on.
the code
while (!eof)
{
c = getc();
putc(array[c - 'a']);
}
# Write C code to implement strtok() 'c' library function.
# Implement strstr(), strcpy(), strtok() etc
# Reverse a string.
# Given a linked list which is sorted, how will you insert in sorted way.
# Write a function that allocates memory for a two-dimensional array of given size (parameter x & y)
# Write source code for printHex(int i) in C/C++
# Write a function that finds the last instance of a character in a string.

c answers

C answers

C interview question1: What is a stack ?
Answer: The stack is a region of memory within which our programs temporarily store data as they execute. For example, when a program passes parameters to functions, C places the parameters on the stack. When the function completes, C removes the items from the stack. Similarly, when a function declares local variables, C stores the variable's values on the stack during the function's execution. Depending on the program's use of functions and parameters, the amount of stack space that a program requires will differ.

C interview question2:Allocating memory for a 3-D array
Answer: #include "alloc.h"
#define MAXX 3
#define MAXY 4
#define MAXZ 5
main( )
{
int ***p, i, j, k ;
p = ( int *** ) malloc ( MAXX * sizeof ( int ** ) ) ;
for ( i = 0 ; i < MAXX ; i++ )
{
p[i] = ( int ** ) malloc ( MAXY * sizeof ( int * ) ) ;
for ( j = 0 ; j < MAXY ; j++ )
p[i][j] = ( int * ) malloc ( MAXZ * sizeof ( int ) ) ;
}
for ( k = 0 ; k < MAXZ ; k++ )
{
for ( i = 0 ; i < MAXX ; i++ )
{
for ( j = 0 ; j < MAXY ; j++ )
{
p[i][j][k] = i + j + k ;
printf ( "%d ", p[i][j][k] ) ;
}
printf ( "\n" ) ;
}
printf ( "\n\n" ) ;
}
}

C interview question3:How to distinguish between a binary tree and a tree?
Answer: A node in a tree can have any number of branches. While a binary tree is a tree structure in which any node can have at most two branches. For binary trees we distinguish between the subtree on the left and subtree on the right, whereas for trees the order of the subtrees is irrelevant.

Consider two binary trees, but these binary trees are different. The first has an empty right subtree while the second has an empty left subtree. If the above are regarded as trees (not the binary trees), then they are same despite the fact that they are drawn differently. Also, an empty binary tree can exist, but there is no tree having zero nodes.

C interview question4:How do I use the function ldexp( ) in a program?
Answer: The math function ldexp( ) is used while solving the complex mathematical equations. This function takes two arguments, a double value and an int respectively. The order in which ldexp( ) function performs calculations is ( n * pow ( 2, exp ) ) where n is the double value and exp is the integer. The following program demonstrates the use of this function.
#include
void main( )
{
double ans ;
double n = 4 ;
ans = ldexp ( n, 2 ) ;
printf ( "\nThe ldexp value is : %lf\n", ans ) ;
}
Here, ldexp( ) function would get expanded as ( 4 * 2 * 2 ), and the output would be the ldexp value is : 16.000000

C interview question5:Can we get the mantissa and exponent form of a given number?
Answer:The function frexp( ) splits the given number into a mantissa and exponent form. The function takes two arguments, the number to be converted as a double value and an int to store the exponent form. The function returns the mantissa part as a double value. Following example demonstrates the use of this function.
#include
void main( )
{
double mantissa, number ;
int exponent ;
number = 8.0 ;
mantissa = frexp ( number, &exponent ) ;
printf ( "The number %lf is ", number ) ;
printf ( "%lf times two to the ", mantissa ) ;
printf ( "power of %d\n", exponent ) ;
return 0 ;
}
Keywords:
c interview questions
c++ faq
faq on pointers in c
faq on arrays in c
faq videos
c tutorial
faqs on strings in c
steve summit
faq on pointers in c
faq on arrays in c
c++ faq
programming in c textbook
c tutorial
c programming resources
c language reference
cinterviews.com