Saturday, July 7, 2018

C Questions And Answers – 2018A7

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.

 

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

 

Are the expressions *ptr++ and ++*ptr same?

 

Ans:

No. *ptr++ increments the pointer and not the value pointed by it, whereas ++*ptr

increments the value being pointed to by ptr.

 

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

 

strpbrk( )

 

The function strpbrk( ) takes two strings as parameters. It scans the first string, to find, the

first occurrence of any character appearing in the second string. The function returns a pointer to the first occurrence of the character it found in the first string.

 

The following program demonstrates the use of string function strpbrk( ).

 

#include <string.h>

main( )

{

char *str1 = "Hello!" ;

char *str2 = "Better" ;

char *p ;

p = strpbrk ( str1, str2 ) ;

if ( p )

printf ( "The first character found in str1 is %c", *p ) ;

else

printf ( "The character not found" ) ;

}

 

The output of the above program would be the first character found in str1 is e

div( )...

 

The function div( ) divides two integers and returns the quotient and remainder. This function takes two integer values as arguments; divides first integer with the second one and returns the answer of division of type div_t. The data type div_t is a structure that contains two long ints, namely quot and rem, which store quotient and remainder of division respectively.

 

The following example shows the use of div( ) function.

 

#include <stdlib.h>

void main( )

{

div_t res ;

res = div ( 32, 5 ) ;

printf ( "\nThe quotient = %d and remainder = %d ", res.quot, res.rem ) ;

 

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

 

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

}

 

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

 

How do I write code that would get error number and display error message if any

standard error occurs?

 

Ans:

Following code demonstrates this.

 

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

 

main( )

{

char *errmsg ;

FILE *fp ;

fp = fopen ( "C:\file.txt", "r" ) ;

 

if ( fp == NULL )

{

errmsg = strerror ( errno ) ;

printf ( "\n%s", errmsg ) ;

}

}

Here, we are trying to open 'file.txt' file. However, if the file does not exist, then it would cause an error. As a result, a value (in this case 2) related to the error generated would get set in errno. errno is an external int variable declared in 'stdlib.h' and also in 'errno.h'. Next, we have called sterror( ) function which takes an error number and returns a pointer to standard error message related to the given error number.

 

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

 

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

No comments:

Post a Comment