C-Interview Questions level 4
Q1) If we have declared an array as global in one file and we are using it in another file then why doesn't the sizeof operator works on an extern array?
Answer:- An extern array is of incomplete type as it does not contain the size. Hence we cannot use
sizeof operator, as it cannot get the size of the array declared
in another file. To resolve this use any of one the following two solutions:
1. In the same file declare one more variable that holds the size of array. For example,
array.c int arr[5] ; int arrsz = sizeof ( arr ) ; myprog.c extern int arr[] ; extern int arrsz ; 2. Define a macro which can be used in an array declaration. For example, myheader.h #define SZ 5 array.c #include "myheader.h" int arr[SZ] ; myprog.c #include "myheader.h" extern int arr[SZ] ;
Answer:- This is shown in following code snippet.
main( ) { int w, no ; printf ( "Enter number and the width for the number field:" ) ; scanf ( "%d%d", &no, &w ) ; printf ( "%*d", w, no ) ; }
Here, an '*' in the format specifier in printf( ) indicates that an int value from the argument list should be used for the field width.
Answer:- Whenever we initialize a 2-D array at the same place where it has been declared, it is not
necessary to mention the row dimension of an array. The row and column dimensions of such an
array can be determined programmatically as shown in following program.
void main( ) { int a[][3] = { 0, 1, 2, 9,-6, 8, 7, 5, 44, 23, 11,15 } ; int c = sizeof ( a[0] ) / sizeof ( int ) ; int r = ( sizeof ( a ) / sizeof ( int ) ) / c ; int i, j ; printf ( "\nRow: %d\nCol: %d\n", r, c ) ; for ( i = 0 ; i < r ; i++ ) { for ( j = 0 ; j < c ; j++ ) printf ( "%d ", a[i][j] ) ; printf ( "\n" ) ; } }
Answer:-
#includemain( ) { char fname[67] ; printf ( "\nEnter name of file to open" ) ; gets ( fname ) ; if ( access ( fname, 0 ) != 0 ) { printf ( "\nFile does not exist." ) ; return ; } }
Answer:- : Use function gcvt( ) to convert a floating-point number to a string. Following program demonstrates the use of this function.
#includemain( ) { char str[25] ; float no ; int dg = 5 ; /* significant digits */ no = 14.3216 ; gcvt ( no, dg, str ) ; printf ( "String: %s\n", str ) ; }
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.
#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" ) ; } }Data Structures
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 the following figure...
This above figure shows 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.
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#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
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#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 ; }
Answer:- Use atexit( ) function as shown in following program.
#includemain( ) { int ch ; void fun ( void ) ; atexit ( fun ) ; // code } void fun( void ) { printf ( "\nTerminate program......" ) ; getch( ) ; }
Copyright © 2022 Shineskill Software Pvt. Ltd., All rights reserved.