Tuesday, July 10, 2018

C Questions And Answers – 2018A10

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.

 

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

 

Suppose I have a structure having fields name, age, salary and have passed address of age to a function fun( ). How I can access the other member of the structure using the address of age?

 

Ans:

 

struct emp

{

char name[20] ;

int age ;

float salary ;

} ;

 

main( )

{

struct emp e ;

printf ( "\nEnter name: " ) ;

scanf ( "%s", e.name ) ;

printf ( "\nEnter age: " ) ;

scanf ( "%d", &e.age ) ;

printf ( "\nEnter salary: " ) ;

scanf ( "%f", &e.salary ) ;

fun ( &e.age ) ;

}

 

fun (int *p)

{

struct emp *q ;

int offset ;

offset = ( char * ) ( & ( ( struct emp * ) 0 ) -> age ) - ( char * ) ( (

struct emp* ) 0 ) ;

q = ( struct emp * ) ( ( char * ) p - offset ) ;

printf ( "\nname: %s", q -> name ) ;

printf ( "\nage: %d", q -> age ) ;

printf ( "\nsalary: %f", q -> salary ) ;

}

 

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

 

How to restrict the program's output to a specific screen region?

 

Ans:

A C function window( ) can be used to restrict the screen output to a specific region. The

window( ) function defines a text-mode window. The parameters passed to this function

defines the upper-left and lower-right corner of the region within which you want the output.

 

In the following program, the string 'Hello!' gets printed within the specified region. To print the string we must use cprintf( ) function which prints directly on the text-mode window.

 

#include <conio.h>

 

main( )

{

int i, j ;

window ( 20, 8, 60, 17 ) ;

for ( i = 0 ; i < 8 ; i++ )

for ( j = 0 ; j < 10 ; j++ )

cprintf ( "Hello!" ) ;

}

 

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

 

How do I write a user-defined function, which deletes each character in a string str1, which matches any character in string str2?

 

Ans:

The function is as shown below:

Compress ( char str1[], char str2[] )

{

int i, j, k ;

for ( i = k = 0 ; str1[i] != ‘\0’ ; i++ )

{

for (j=0;str2[j] != ‘\0’ && str2[j] !=str1[i] ; j++ )

;

if ( str2[j] == ‘\0’ )

str1[k++] = str1[I] ;

}

str1[k] = ‘\0’

}

 

 

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

 

How does free( ) know how many bytes to free?

 

Ans:

The malloc( ) / free( ) implementation remembers the size of each block allocated and

returned, so it is not necessary to remind it of the size when freeing.

 

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

 

What is the use of randomize( ) and srand( ) function?

 

Ans:

While generating random numbers in a program, sometimes we require to control the series of numbers that random number generator creates. The process of assigning the

random number generators starting number is called seeding the generator. The randomize( ) and srand( ) functions are used to seed the random number generators. The randomize( ) function uses PC's clock to produce a random seed, whereas the srand( ) function allows us to specify the random number generator's starting value.

 

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

 

How do I determine amount of memory currently available for allocating?

 

Ans:

We can use function coreleft( ) to get the amount of memory available for allocation.

However, this function does not give an exact amount of unused memory. If, we are using a

small memory model, coreleft( ) returns the amount of unused memory between the top of the heap and stack. If we are using a larger model, this function returns the amount of memory between the highest allocated memory and the end of conventional memory. The function returns amount of memory in terms of bytes.

 

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

 

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

No comments:

Post a Comment