C faqs question and answers
C interview question1:How do I write code that executes certain function only at program termination?Answer: 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( ) ;
}
main( )
{
int ch ;
void fun ( void ) ;
atexit ( fun ) ;
// code
}
void fun( void )
{
printf ( "\nTerminate program......" ) ;
getch( ) ;
}
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.
C interview question3:How does C compiler store elements in a multi-dimensional array?
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]
C interview question4: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 < 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
C interview question5:How can I find the day of the week of a given date? {
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 ) ) ;
}
C interview question6: What's the difference between these two declarations? {
/*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.
C interview question7: How do I print the contents of environment variables?
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.
keywords:{
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.
memory models in embedded c
data memory in microcontroller
memory models in c
ram in embedded system
composing memory in embedded system
memory mapping in 8051
external memory in embedded system
memory organization of 8051
data in embedded c
program termination in c
how to end a program in c
exit(0) in c
c exit codes
exit() function in c example
how to exit c program output screen
exit(-1) in c
c quit
explain exit statement in c with example
multi dimensional array in c
two dimensional array in c definition
three dimensional array in c
two dimensional array in c++
two dimensional array in c using pointers
two dimensional array in c pdf
two dimensional string array in c
sum of elements in 2d array in c
if statement
logical operators
if statement python
boolean expression
conditional statement
if statement logic
if else statement
python if greater than or equal to
conditional operator c#
conditional operator java
conditional operator javascript
conditional operator c++
conditional operator in c
list operators used in if conditional statement in python
ternary operator
list operators used in if conditional statement in java
conditional operator in python
No comments:
Post a Comment