Tuesday, July 3, 2018

C Questions And Answers – 2018A3

There are many commonly asked questions regarding C programming language. Below are some collected such question-answer examples. The questions are usually related with Turbo C IDE in windows or GCC under Linux environment [not always].

For more such examples, click C_Q&A label.

 

-------------------------------------------------------------------------------------------------

 

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 columnmajor order. When the compiler places elements in rows of an array first then it is called rowmajor 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]

 

-------------------------------------------------------------------------------------------------

 

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 ;

 

Ans:

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

 

-------------------------------------------------------------------------------------------------

 

How do I compare character data stored at two different memory locations?

 

Ans:

Sometimes in a program we require to compare memory ranges containing strings. In

such a situation we can use functions like memcmp( ) or memicmp( ). The basic difference between two functions is that memcmp( ) does a case-sensitive comparison whereas memicmp( ) ignores case of characters. Following program illustrates the use of both the functions.

 

#include <mem.h>

 

main( )

{

char *arr1 = "Kicit" ;

char *arr2 = "kicitNagpur" ;

int c ;

c = memcmp ( arr1, arr2, sizeof ( arr1 ) ) ;

if ( c == 0 )

printf("\nStrings arr1 & arr2 compared using memcmp are identical" ) ;

else

printf ( "\nStrings arr1 & arr2 compared using memcmp are not identical") ;

c = memicmp ( arr1, arr2, sizeof ( arr1 ) ) ;

if ( c == 0 )

printf ( "\nStrings arr1 and arr2 compared using memicmp are identical" );

else

printf ( "\nStrings arr1 and arr2 compared using memicmp are not

           identical" ) ;

}

 

-------------------------------------------------------------------------------------------------

 

Fixed-size objects are more appropriate as compared to variable size data objects. Using

variable-size data objects saves very little space. Variable size data objects usually have some overhead. Manipulation of fixed-size data objects is usually faster and easier. Use fixed size when maximum size is clearly bounded and close to average. And use variable-size data objects when a few of the data items are bigger than the average size.

 

For example,

 

char *num[10] =

{ "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" } ;

 

Instead of using the above, use

 

char num[10][6] =

{ "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" } ;

 

The first form uses variable-size data objects. It allocates 10 pointers, which are pointing to 10 string constants of variable size. Assuming each pointer is of 4 bytes, it requires 90 bytes. On the other hand, the second form uses fixed size data objects. It allocates 10 arrays of 6 characters each. It requires only 60 bytes of space. So, the variable-size in this case does not offer any advantage over fixed size.

 

-------------------------------------------------------------------------------------------------

 

…till next post, bye-bye & take care.

No comments:

Post a Comment