Language in C Interview Questions and Answers
Ques 26. What is environment and how do I get environment for a specific entry?
While working in DOS, it stores information in a memory region called environment. In this region we can place configuration settings such as command path, system prompt, etc. Sometimes in a program we need to access the information contained in environment. The function getenv( ) can be used when we want to access environment for a specific entry. Following program demonstrates the use of this function.
#include <stdio.h>
#include <stdlib.h>
main( )
{
char *path = NULL ;
path = getenv ( "PATH" ) ;
if ( *path != NULL )
printf ( "nPath: %s", path ) ;
else
printf ( "nPath is not set" ) ;
}
Ques 27. How do I display current date in the format given below?
Saturday October 12, 2002
Following program illustrates how we can display date in above given format.
#include
#include
main( )
{
struct tm *curtime ;
time_t dtime ;
char str[30] ;
time ( &dtime ) ;
curtime = localtime ( &dtime ) ;
strftime ( str, 30, "%A %B %d, %Y", curtime ) ;
printf ( "n%s", str ) ;
}
Here we have called time( ) function which returns current time. This time is returned in terms of seconds, elapsed since 00:00:00 GMT, January 1, 1970. To extract the week day, day of month, etc. from this value we need to break down the value to a tm structure. This is done by the function localtime( ). Then we have called strftime( ) function to format the time and store it in a string str.
Ques 28. If we have declared an array as global in one file and we are using it in another file then why doesn't the sizeof operator works on an extern array?
An extern array is of incomplete type as it does not contain the size. Hence we cannot use sizeof operator, as it cannot get the size of the array declared in another file. To resolve this use any of one the following two solutions:
1. In the same file declare one more variable that holds the size of array. For example,
array.c
int arr[5] ;
int arrsz = sizeof ( arr ) ;
myprog.c
extern int arr[] ;
extern int arrsz ;
2. Define a macro which can be used in an array
declaration. For example,
myheader.h
#define SZ 5
array.c
#include "myheader.h"
int arr[SZ] ;
myprog.c
#include "myheader.h"
extern int arr[SZ] ;
Ques 29. How do I write printf( ) so that the width of a field can be specified at runtime?
This is shown in following code snippet.
main( )
{
int w, no ;
printf ( "Enter number and the width for the
number field:" ) ;
scanf ( "%d%d", &no, &w ) ;
printf ( "%*d", w, no ) ;
}
Here, an '*' in the format specifier in printf( ) indicates that an int value from the argument list should be used for the field width.
Ques 30. How to find the row and column dimension of a given 2-D array?
Whenever we initialize a 2-D array at the same place where it has been declared, it is not necessary to mention the row dimension of an array. The row and column dimensions of such an array can be determined programmatically as shown in following program.
void main( )
{
int a[][3] = { 0, 1, 2,
9,-6, 8,
7, 5, 44,
23, 11,15 } ;
int c = sizeof ( a[0] ) / sizeof ( int ) ;
int r = ( sizeof ( a ) / sizeof ( int ) ) / c ;
int i, j ;
printf ( "nRow: %dnCol: %dn", r, c ) ;
for ( i = 0 ; i < r ; i++ )
{
for ( j = 0 ; j < c ; j++ )
printf ( "%d ", a[i][j] ) ;
printf ( "n" ) ;
}
}
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?