C Interview Questions Part 4

Q:Are the following two statements identical?
char str[6] = "Kicit" ;
char *str = "Kicit" ;
A: No! Arrays are not pointers. An array is a single, pre-allocated chunk of contiguous elements (all of the same type), fixed in size and location. A pointer on the other hand, is a reference to any data element (of a particular type) located anywhere. A pointer must be assigned to point to space allocated elsewhere, but it can be reassigned any time. The array declaration char str[6] ; requests that space for 6 characters be set aside, to be known by name str. In other words there is a location named str at which six characters are stored. The pointer declaration char *str ; on the other hand, requests a place that holds a pointer, to be known by the name str. This pointer can point almost anywhere to any char, to any contiguous array of chars, or nowhere.

Q:Is the following code fragment correct?
const int x = 10 ;
int arr[x] ;
A: No! Here, the variable x is first declared as an int so memory is reserved for it. Then it is qualified by a const qualifier. Hence, const qualified object is not a constant fully. It is an object with read only attribute, and in C, an object associated with memory cannot be used in array dimensions.


Q:How do I write code to retrieve current date and time from the system and display it as a string?
A:Use time( ) function to get current date and time and then ctime( ) function to display it as a string. This is shown in following code snippet.
#include
void main( )
{
time_t curtime ;
char ctm[50] ;
time ( &curtime ) ; //retrieves current time &
stores in curtime
printf ( "\nCurrent Date & Time: %s", ctime (
&curtime ) ) ;
}


Q:How do I change the type of cursor and hide a cursor?
A: We can change the cursor type by using function _setcursortype( ). This function can change the cursor type to solid cursor and can even hide a cursor. Following code shows how to change the cursor type and hide cursor.
#include
main( )
{
/* Hide cursor */
_setcursortype ( _NOCURSOR ) ;
/* Change cursor to a solid cursor */
_setcursortype ( _SOLIDCURSOR ) ;
/* Change back to the normal cursor */
_setcursortype ( _NORMALCURSOR ) ;
}