C-Interview Questions level 2
Q1) How do I write code to retrieve current date and time from the system and display it as a string?
Answer:- 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.,br>
void main( ) { time_t curtime ; char ctm[50] ; time ( &curtime ) ; //retrieves current time & stores in curtime printf ( "\nCurrent Date & Time: %s", ctime ( &curtime ) ) ; }
int x = 3000, y = 2000 ;
long int z = x * y ;
Answer:- 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. ,br>
#includemain( ) { /* Hide cursor */ _setcursortype ( _NOCURSOR ) ; /* Change cursor to a solid cursor */ _setcursortype ( _SOLIDCURSOR ) ; /* Change back to the normal cursor */ _setcursortype ( _NORMALCURSOR ) ; }
Answer:- Following code demonstrates this.
#include#include #include main( ) { char *errmsg ; FILE *fp ; fp = fopen ( "C:\file.txt", "r" ) ; if ( fp == NULL ) { errmsg = strerror ( errno ) ; printf ( "\n%s", errmsg ) ; } }
Here, we are trying to open 'file.txt' file. However, if the file does not exist, then it would cause an error. As a result, a value (in this case 2) related to the error generated would get set in errno. errno is an external int variable declared in 'stdlib.h' and also in 'errno.h'. Next, we have called sterror( ) function which takes an error number and returns a pointer to standard error message related to the given error number.
Answer:- The function getdisk( ) returns the drive number of current drive. The drive number 0 indicates 'A' as the current drive, 1 as 'B' and so on. The Setdisk( ) function sets the current drive. This function takes one argument which is an integer indicating the drive to be set. Following program demonstrates use of both the functions.
#includemain( ) { int dno, maxdr ; dno = getdisk( ) ; printf ( "\nThe current drive is: %c\n", 65 + dno ) ; maxdr = setdisk ( 3 ) ; dno = getdisk( ) ; printf ( "\nNow the current drive is: %c\n", 65 + dno ) ; }
Answer:- : The functions memcmp( ) and memicmp( ) compares first n bytes of given two blocks of memory or
strings. However, memcmp( ) performs comparison as unsigned chars whereas memicmp( ) performs
comparison as chars but ignores case (i.e. upper or lower case). Both the functions return an integer value where 0 indicates that two memory buffers compared are identical. If the value returned is greater than 0 then it indicates that
the first buffer is bigger than the second one. The value less than 0 indicate that the first buffer is less than the second buffer. The following code snippet demonstrates use of both
#include#include main( ) { char str1[] = "This string contains some characters" ; char str2[] = "this string contains" ; int result ; result = memcmp ( str1, str2, strlen ( str2 ) ) ; printf ( "\nResult after comapring buffer using memcmp( )" ) ;show ( result ) ; result = memicmp ( str1, str2, strlen ( str2 ) ) ; printf ( "\nResult after comapring buffer using memicmp( )" ) ; show ( result ) ; } show ( int r ) { if ( r == 0 ) printf ( "\nThe buffer str1 and str2 hold identical data" ) ; if ( r > 0 ) printf ( "\nThe buffer str1 is bigger than buffer str2" ) ; if ( r < 0 ) printf ( "\nThe buffer str1 is less than buffer str2" ) ; }
Answer:- Use getdfree( ) function as shown in follow code.
#include#include #include #include main( ) { int dr ; struct dfree disk ; long freesp ; dr = getdisk( ) ; getdfree ( dr + 1 , &disk ) ; if ( disk.df_sclus == 0xFFFF ) { printf ( "\ngetdfree( ) function failed\n"); exit ( 1 ) ; } freesp = ( long ) disk.df_avail * ( long ) disk.df_bsec * ( long ) disk.df_sclus ; printf ( "\nThe current drive %c: has %ld bytes available as free space\n", 'A' + dr, freesp ) ;
switch ( color ) { case 0 : ch = 'R' ; break ; case 1 : ch = 'G' ; break ; case 2 : ch = 'B' ; break ; }
Answer:-
char *num[10] = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" } ; char num[10][6] = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" } ;
In place of switch-case we can make use of the value in color as an index for a character array. How to do this is shown in following code snippet.
char *str = "RGB' ; char ch ; int color ; // code ch = str[ color ] ;
Answer:- The function whose address is passed to atexit( ) gets called before the termination of program.
If atexit( ) is called for more than one function then the functions are called in "first in last
out" order. You can verify that from the output.
#include#include void fun1( ) { printf("Inside fun1\n"); } void fun2( ) { printf("Inside fun2\n"); } main( ) { atexit ( fun1 ) ; /* some code */ atexit ( fun2 ) ; printf ( "This is the last statement of program?\n" ); }
Answer:- The function is as shown below:
Compress ( char str1[], char str2[] )
Compress ( char str1[], char str2[] )
{ int i, j, k ; for ( i = k = 0 ; str1[i] != ‘\0’ ; i++ ) { for ( j = 0 ; str2[j] != ‘\0’ && str2[j] != str1[i] ; j++ ) ; if ( str2[j] == ‘\0’ ) str1[k++] = str1[I] ; } str1[k] = ‘\0’ }
Answer:- The malloc( ) / free( ) implementation remembers the size of each block allocated and returned,
so it is not necessary to remind it of the size when freeing.
Copyright © 2022 Shineskill Software Pvt. Ltd., All rights reserved.