Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Language in C Interview Questions and Answers

Test your skills through the online practice test: Language in C Quiz Online Practice Test

Ques 51. How do I use function ecvt( ) in a program?

The function ecvt( ) converts a floating-point value to a null terminated string. This function takes four arguments, such as, the value to be converted to string, the number of digits to be converted to string, and two integer pointers. The two-integer pointer stores the position of the decimal point (relative to the string) and the sign of the number, respectively. If the value in a variable, used to store sign is 0, then the number is positive and, if it is non-zero, then the number is negative. The function returns a pointer to the string containing digits. Following program demonstrates the use of this function.

#include <stdlib.h>
main( )
{
char *str ;
double val ;
int dec, sign ;
int ndig = 4 ;

val = 22 ;
str = ecvt ( val, ndig, &dec, &sign ) ;
printf ( "string = %s dec = %d sign = %dn", str, dec, sign ) ;

val = -345.67 ;
ndig = 8 ;
str = ecvt ( val, ndig, &dec, &sign ) ;
printf ( "string = %s dec = %d sign = %dn", str, dec, sign ) ;

// number with a scientific notation
val = 3.546712e5 ;
ndig = 5 ;
str = ecvt ( val, ndig, &dec, &sign ) ;
printf ( "string = %s dec = %d sign = %dn", str, dec, sign ) ;
}

The output of this program would be

string = 2200 dec = 2 sign = 0
string = 34567000 dec = 3 sign = 1
string = 35467 dec = 6 sign = 0

Is it helpful? Add Comment View Comments
 

Ques 52. How to run DIR command programmatically?

We can use the system( ) function to execute the DIR command along with its options. Following program shows how this can be achieved:

// mydir.c

main ( int argc, char *argv[ ] )
{
char str[30] ;

if ( argc < 2 )
exit ( 0 ) ;

sprintf ( str, "dir %s %s", argv[1], argv[2] ) ;
system ( str ) ;
}

If we run the executable file of this program at command prompt passing the command line arguments as follows:

> mydir abc.c /s

This will search the file 'abc.c' in the current directory.

Is it helpful? Add Comment View Comments
 

Ques 53. 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?

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 ) ;
}

Is it helpful? Add Comment View Comments
 

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

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!" ) ;
}

Is it helpful? Add Comment View Comments
 

Ques 55. Sometimes you need to prompt the user for a password. When the user types in the password, the characters the user enters should not appear on the screen. A standard library function getpass( ) can be used to perform such function. Maximum number of characters that can be entered as password is 8.

main( )
{
char *pwd ;

pwd = getpass ( "Enter Password" ) ;

if ( strcmp ( pwd, "orgcity" ) )
printf ( "nPassword %s is incorrect", pwd ) ;
else
printf ( "nCorrect Password" ) ;
}

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2024 WithoutBook