Friday, July 6, 2018

C Questions And Answers – 2018A6

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 do I use scanf( ) to read the date in the form 'dd-mm-yy' ?

 

Ans:

There are two ways to read the date in the form of 'dd-mm-yy' one possible way is...

 

int dd, mm, yy ;

char ch ; /* for char '-' */

printf ( "\nEnter the date in the form of dd-mm-yy : " ) ;

scanf( "%d%c%d%c%d", &dd, &ch, &mm, &ch, &yy ) ;

 

And another best way is to use suppression character * as...

 

int dd, mm, yy ;

scanf( "%d%*c%d%*c%d", &dd, &mm, &yy ) ;

 

The suppression character * suppresses the input read from the standard input buffer for the assigned control character.

 

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

 

How do I print a floating-point number with higher precision say 23.34568734 with only precision up to two decimal places?

 

Ans:

This can be achieved through the use of suppression char '*' in the format string of printf(

) as shown in the following program.

 

main( )

{

int i = 2 ;

float f = 23.34568734 ;

printf ( "%.*f", i, f ) ;

}

 

The output of the above program would be 23.35.

 

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

 

Is the following code fragment correct?

 

const int x = 10 ;

int arr[x] ;

 

Ans:

No! Here, the variable x is first declared as an int so memory is reserved for it. Then it is qualified by a const qualifier. Hence, const qualified object is not a constant fully. It is an object with read only attribute, and in C, an object associated with memory cannot be used in array dimensions.

 

How do I write code to retrieve current date and time from the system and display it as a string?

 

Ans:

Use time( ) function to get current date and time and then ctime( ) function to display it

as a string. This is shown in following code snippet.

 

#include <sys\types.h>

void main( )

{

time_t

curtime ;

char ctm[50] ;

time ( &curtime ) ; //retrieves current time & stores in curtime

printf ( "\nCurrent Date & Time: %s", ctime (&curtime ) ) ;

}

 

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

 

How do I change the type of cursor and hide a cursor?

 

Ans:

We can change the cursor type by using function _setcursortype( ). This function can change the cursor type to solid cursor and can even hide a cursor. Following code shows how to change the cursor type and hide cursor.

 

#include <conio.h>

 

main( )

{

 /* Hide cursor */

_setcursortype ( _NOCURSOR ) ;

 

/* Change cursor to a solid cursor */

_setcursortype ( _SOLIDCURSOR ) ;

 

/* Change back to the normal cursor */

_setcursortype ( _NORMALCURSOR ) ;

}

 

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

 

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

No comments:

Post a Comment