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 ) ) ;
}
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.
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.
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
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.
"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:
- What will be the output of the following code?
void main ()
{ int i = 0 , a[3] ;
a[i] = i++;
printf ("%d",a[i]) ;
}
int x = 3000, y = 2000 ;
long int z = x * y ;
' target='_blank'>Why doesn't the following code give the desired result?
int x = 3000, y = 2000 ;
long int z = x * y ;
char str[ ] = "Hello" ;
strcat ( str, '!' ) ;
' target='_blank'>Why doesn't the following statement work?
char str[ ] = "Hello" ;
strcat ( str, '!' ) ;- How do I know how many elements an array can hold?
- How do I compare character data stored at two different memory locations?