Thursday, November 20, 2008

what is output of c programs ?

46. How do I print the contents of environment variables?

Ans:. The following program shows how to achieve this:
main( int argc, char *argv[ ], char *env[ ] )
{
int i = 0 ;
clrscr( ) ;
while ( env[ i ] )
printf ( "\n%s", env[ i++ ] ) ;
}
main( ) has the third command line argument env, which is an array of pointers to the strings. Each pointer points to an environment variable from the list of environment variables.

47. 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 
void main( )
{
div_t res ;
res = div ( 32, 5 ) ;
printf ( "\nThe quotient = %d and remainder = %d ", res.quot, res.rem ) ;
}

48. What would the second and the third printf( ) output the following program?
main( )
{
char *str[ ] = {
"Good Morning"
"Good Evening"
"Good Afternoon"
} ;
printf ( "\nFirst string = %s", str[0] ) ;
printf ( "\nSecond string = %s", str[1] ) ;
printf ( "\nThird string = %s", str[2] ) ;
}
Ans: For the above given program, we expect the output as Good Evening and Good Afternoon, for the second and third printf( ). However, the output would be as shown below.

First string = Good MorningGood EveningGood Afternoon
Second string = ( null )
Third string =
What is missing in the above given code snippet is a comma separator which should separate the strings Good Morning, Good Evening and Good Afternoon. On adding comma, we would get the output as shown below.
First string = Good Morning
Second string = Good Evening
Third string = Good Afternoon

49. 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.

50. 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.

51. 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.

52. 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 
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

53. Can we convert an unsigned long integer value to a string?

Ans: The function ultoa( ) can be used to convert an unsigned long integer value to a string. This function takes three arguments, first the value that is to be converted, second the base address of the buffer in which the converted number has to be stored (with a string terminating null character '\0') and the last argument specifies the base to be used in converting the value. Following example demonstrates the use of this function.
#include 
void main( )
{
unsigned long ul = 3234567231L ;
char str[25] ;

ultoa ( ul, str, 10 ) ;
printf ( "str = %s unsigned long = %lu\n", str, ul ) ;
}

54. ceil( ) and floor( )
The math function ceil( ) takes a double value as an argument. This function finds the smallest possible integer to which the given number can be rounded up. Similarly, floor( ) being a math function, takes a double value as an argument and returns the largest possible integer to which the given double value can be rounded down. The following program demonstrates the use of both the functions.

#include 
void main( )
{
double no = 1437.23167 ;
double down, up ;
down = floor ( no ) ;
up = ceil ( no ) ;
printf ( "The original number %7.5lf\n", no ) ;
printf ( "The number rounded down %7.5lf\n", down ) ;
printf ( "The number rounded up %7.5lf\n", up ) ;
}
The output of this program would be,
The original number 1437.23167
The number rounded down 1437.00000
The number rounded up 1438.00000

No comments:

Blog List