Sunday, April 23, 2006

FAQ's of C.. Check out your C Knowledge... Part IV

37. Allocating memory for a 3-D array#include "alloc.h"#define MAXX 3#define MAXY 4#define MAXZ 5main( ){int ***p, i, j, k ;p = ( int *** ) malloc ( MAXX * sizeof ( int ** ) ) ;for ( i = 0 ; i < j =" 0" k =" 0" i =" 0" j =" 0">#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-------------------------------------------------------------------------------------------------
39. Can we get the mantissa and exponent form of a given number?
Ans: 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 #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 ; }-------------------------------------------------------------------------------------------------40.
How do I write code that executes certain function only at program termination?Ans: Use atexit( ) function as shown in following program.
#include main( ){ int ch ;void fun ( void ) ;atexit ( fun ) ;// code }void fun( void ){ printf ( "\nTerminate program......" ) ;getch( ) ; }-------------------------------------------------------------------------------------------------41. What are memory models?Ans: The compiler uses a memory model to determine how much memory is allocated to the program. The PC divides memory into blocks called segments of size 64 KB. Usually, program uses one segment for code and a second segment for data. A memory model defines the number of segments the compiler can use for each. It is important to know which memory model can be used for a program. If we use wrong memory model, the program might not have enough memory to execute. The problem can be solved using larger memory model. However, larger the memory model, slower is your program execution. So we must choose the smallest memory model that satisfies our program needs. Most of the compilers support memory models like tiny, small, medium, compact, large and huge.
36. What is a stack ?Ans: 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.-------------------------------------------------------------------------------------------------37. Allocating memory for a 3-D array#include "alloc.h"#define MAXX 3#define MAXY 4#define MAXZ 5main( ){int ***p, i, j, k ;p = ( int *** ) malloc ( MAXX * sizeof ( int ** ) ) ;for ( i = 0 ; i < j =" 0" k =" 0" i =" 0" j =" 0">#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-------------------------------------------------------------------------------------------------
39. Can we get the mantissa and exponent form of a given number?
Ans: 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 #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 ; }-------------------------------------------------------------------------------------------------40.
How do I write code that executes certain function only at program termination?Ans: Use atexit( ) function as shown in following program.
#include main( ){ int ch ;void fun ( void ) ;atexit ( fun ) ;// code }void fun( void ){ printf ( "\nTerminate program......" ) ;getch( ) ; }-------------------------------------------------------------------------------------------------41. What are memory models?Ans: The compiler uses a memory model to determine how much memory is allocated to the program. The PC divides memory into blocks called segments of size 64 KB. Usually, program uses one segment for code and a second segment for data. A memory model defines the number of segments the compiler can use for each. It is important to know which memory model can be used for a program. If we use wrong memory model, the program might not have enough memory to execute. The problem can be solved using larger memory model. However, larger the memory model, slower is your program execution. So we must choose the smallest memory model that satisfies our program needs. Most of the compilers support memory models like tiny, small, medium, compact, large and huge.
42. How does C compiler store elements in a multi-dimensional array?
Ans: The compiler maps multi-dimensional arrays in two ways—Row major order and Column order. When the compiler places elements in columns of an array first then it is called column-major order. When the compiler places elements in rows of an array first then it is called row-major order. C compilers store multidimensional arrays in row-major order. For example, if there is a multi-dimensional array a[2][3], then according row-major order, the elements would get stored in memory following order: a[0][0], a[0][1], a[0][2], a[1][0], a[1][1], a[1][2]-------------------------------------------------------------------------------------------------43. If the result of an _expression has to be stored to one of two variables, depending on a condition, can we use conditional operators as shown below?
( ( i < i =" 5" j =" 10" k =" 12," l =" 1" i =" %d" j =" %d" k =" %d" l =" %d" i =" 5" j =" 16" k =" 12" l =" 1-------------------------------------------------------------------------------------------------44." monday =" 1" sunday =" 0">= 1 and <= 12, yy > 1752 or so */static int arr[ ] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 } ;yy = yy - mm < i =" 0">void main( ){ div_t res ;
res = div ( 32, 5 ) ;printf ( "\nThe quotient = %d and remainder = %d ", res.quot, res.rem ) ; }
-----------------------------------------------------------------------------------------------
48. What would the second and the third printf( ) output the following program?
main( ){ char *str[ ] = { "Good Morning""Good Evening""Good Afternoon" } ; printf ( "\nFirst string = %s", str[0] ) ;printf ( "\nSecond string = %s", str[1] ) ;printf ( "\nThird string = %s", str[2] ) ; } Ans: For the above given program, we expect the output as Good Evening and Good Afternoon, for the second and third printf( ). However, the output would be as shown below.
First string = Good MorningGood EveningGood AfternoonSecond string = ( null ) Third string =
What is missing in the above given code snippet is a comma separator which should separate the strings Good Morning, Good Evening and Good Afternoon. On adding comma, we would get the output as shown below.First string = Good MorningSecond string = Good EveningThird string = Good Afternoon-------------------------------------------------------------------------------------------------

No comments: