Wednesday, July 11, 2018

C Questions And Answers – 2018A11

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.

 

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

 

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

}

 

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

 

How to obtain the current drive through C ?

 

Ans:

We can use the function _getdrive( ) to obtain the current drive. The _getdrive( ) function

uses DOS function 0X19 to get the current drive number

 

#include <direct.h>

main( )

{

int disk ;

disk = _getdrive( ) + 'A' - 1 ;

printf ( "The current drive is: %c\n", disk ) ;

}

 

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

 

How come the output for both the programs is different when the logic is same?

 

main( )

{

int i, j ;

for ( i = 1, j = 1 ; i <= 5, j <= 100 ; i++, j++ )

{

gotoxy ( 1, 1, ) ;

printf ( "%d %d", i, j ) ;

}

}

 

main( )

{

int i, j ;

for ( i =1, j = 1; j <= 100, i <= 5; i++, j++ )

{

gotoxy ( 1, 1 ) ;

printf ( "%d %d", i, j ) ;

}

}

 

Output -> 5 5

 

Even if logic of both the programs is same the output of the first program comes out to be

100, 100, but of the second program it is 5, 5. The comma operator plays a vital role inside

the for loop. It always considers the value of the latest variable. So, at the time of testing the condition in for loop, the value of j will be considered in the first program and value of i in the second.

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

 

How does a C program come to know about command line arguments?

 

Ans:

When we execute our C program, operating system loads the program into memory. In case of DOS, it first loads 256 bytes into memory, called program segment prefix. This contains file table, environment segment, and command line information. When we compile the C program the compiler inserts additional code that parses the command, assigning it to the argv array, making the arguments easily accessible within our C program.

 

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

 

When we open a file, how does functions like fread( )/fwrite( ), etc. get to know from

where to read or to write the data?

 

Ans:

When we open a file for read/write operation using function like fopen( ), it returns a pointer to the structure of type FILE. This structure stores the file pointer called position pointer, which keeps track of current location within the file. On opening file for read/write operation, the file pointer is set to the start of the file. Each time we read/write a character, the position pointer advances one character. If we read one line of text at a step from the file, then file pointer advances to the start of the next line. If the file is opened in append mode, the file pointer is placed at the very end of the file. Using fseek( )function we can set the file pointer to some other place within the file.

 

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

 

The sizeof( ) function doesn’t return the size of the block of memory pointed to by a pointer. Why?

 

Ans:

The sizeof( ) operator does not know that malloc( ) has been used to allocate a pointer. sizeof( ) gives us the size of pointer itself. There is no handy way to find out the size of a

block allocated by malloc( ).

 

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

 

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

No comments:

Post a Comment