C-Interview Questions level 5
Q1) What are memory models?
Answer:- 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.
,
Answer:- 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]
( ( i < 10 ) ? j : k ) = l * 2 + p ;
Answer:- No! The above statement is invalid. We cannot use the conditional operators in this fashion.
The conditional operators like most operators, yields a value, and we cannot assign the value of
an _expression to a value. However, we can use conditional operators as shown in following code snippet.
main( ) { int i, j, k, l ; i = 5 ; j = 10 ; k = 12, l = 1 ; * ( ( i < 10 ) ? &j : &k ) = l * 2 + 14 ; printf ( "i = %d j = %d k = %d l = %d", i, j, k, l ) ; } The output of the above program would be as given below: i = 5 j = 16 k = 12 l = 1
Answer:- The following code snippet shows how to get the day of week from the given date.
dayofweek ( int yy, int mm, int dd ) { /*Monday = 1 and Sunday = 0 */ /* month number >= 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 < 3 ; return ( yy + yy / 4 - yy / 100 + yy / 400 + arr[ mm - 1] + dd ) % 7 ; } void main( ) { printf ( "\n\n\nDay of week : %d ", dayofweek ( 2002, 5, 18 ) ) ; }
struct str1 { ... } ; typedef struct { ... } str2 ;
Answer:- : The first form declares a structure tag whereas the second declares a typedef. The main
difference is that the second declaration is of a slightly more abstract type -- its users don't
necessarily know that it is a structure, and the keyword struct is not used when declaring instances of it.
Answer:- The following program shows how to achieve this:
main( int argc, char *argv[ ], char *env[ ] )
{ int i = 0 ; clrscr( ) ; while ( env[ i ] ) printf ( "\n%s", env[ i++ ] ) ; }
main( ) has the third command line argument env, which is an array of pointers to the strings. Each pointer points to an environment variable from the list of environment variables.
Answer:- The function div( ) divides two integers and returns the quotient and remainder. This function takes
two integer values as arguments; divides first integer with the second one and returns the answer of
division of type div_t. The data type div_t is a structure that contains two long ints, namely quot
and rem, which store quotient and remainder of division
respectively. The following example shows the use of div( ) function.
#includevoid main( ) { div_t res ; res = div ( 32, 5 ) ; printf ( "\nThe quotient = %d and remainder = %d ", res.quot, res.rem ) ; }
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] ) ; }
Answer:- 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 Afternoon
Second 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 Morning
Second string = Good Evening
Third string = Good Afternoon
First string = Good MorningGood EveningGood Afternoon
Second 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 Morning
Second string = Good Evening
Third string = Good Afternoon
Answer:- int dd, mm, yy ;
char ch ; /* for char '-' */
printf ( "\nEnter the date in the form of dd-mm-yy : " ) ;
scanf( "%d%c%d%c%d", &dd, &ch, &mm, &ch, &yy ) ;
And another best way is to use suppression character * as...
int dd, mm, yy ;
scanf( "%d%*c%d%*c%d", &dd, &mm, &yy ) ;
The suppression character * suppresses the input read from the standard input buffer for the assigned control character.
char ch ; /* for char '-' */
printf ( "\nEnter the date in the form of dd-mm-yy : " ) ;
scanf( "%d%c%d%c%d", &dd, &ch, &mm, &ch, &yy ) ;
And another best way is to use suppression character * as...
int dd, mm, yy ;
scanf( "%d%*c%d%*c%d", &dd, &mm, &yy ) ;
The suppression character * suppresses the input read from the standard input buffer for the assigned control character.
Answer:- This can be achieved through the use of suppression char '*' in the format string of printf( ) as shown in the following program.
main( ) { int i = 2 ; float f = 23.34568734 ; printf ( "%.*f", i, f ) ; }
The output of the above program would be 23.35.
Copyright © 2022 Shineskill Software Pvt. Ltd., All rights reserved.