Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Language in C Interview Questions and Answers

Test your skills through the online practice test: Language in C Quiz Online Practice Test

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

Is it helpful? Add Comment View Comments
 

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.

Is it helpful? Add Comment View Comments
 

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

Is it helpful? Add Comment View Comments
 

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.

Is it helpful? Add Comment View Comments
 

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

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2024 WithoutBook