Exams Attended

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Language in C Interview Questions and Answers

Ques 41. How can I find the day of the week of a given date?

The following code snippet shows how to get the day of week from the given date.

dayofweek ( int yy, int mm, int dd )
{
/*Monday = 1 and Sunday = 0 */
/* month number >= 1 and <= 12, yy > 1752 or so */
static int arr[ ] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 } ;
yy = yy - mm < 3 ;
return ( yy + yy / 4 - yy / 100 + yy / 400 + arr[ mm - 1] + dd ) % 7 ;
}

void main( )
{
printf ( "nnnDay of week : %d ", dayofweek ( 2002, 5, 18 ) ) ;
}

Is it helpful? Add Comment View Comments
 

Ques 42. What's the difference between these two declarations?

struct str1 { ... } ;
typedef struct { ... } str2 ;

The first form declares a structure tag whereas the second declares a typedef. The main difference is that the second declaration is of a slightly more abstract type -- its users don't necessarily know that it is a structure, and the keyword struct is not used when declaring instances of it.

Is it helpful? Add Comment View Comments
 

Ques 43. How do I print the contents of environment variables?

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

Is it helpful? Add Comment View Comments
 

Ques 44. 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] ) ;
}

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

Is it helpful? Add Comment View Comments
 

Ques 45. How do I use scanf( ) to read the date in the form 'dd-mm-yy' ?

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.

Is it helpful? Add Comment View Comments
 
"Good Evening"
"Good Afternoon"
} ;
printf ( "nFirst string = %s", str[0] ) ;
printf ( "nSecond string = %s", str[1] ) ;
printf ( "nThird string = %s", str[2] ) ;
}
5) How do I use scanf( ) to read the date in the form 'dd-mm-yy' ?
" />

Most helpful rated by users:

©2022 WithoutBook