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

Related differences

Ques 76. How do I write a function that takes variable number of arguments?

The following program demonstrates this.

#include <stdio.h>
#include <stdarg.h>

void main( )
{
int i = 10 ;
float f = 2.5 ;
char *str = "Hello!" ;
vfpf ( "%d %f %sn", i, f, str ) ;
vfpf ( "%s %s", str, "Hi!" ) ;
}

void vfpf ( char *fmt, ... )
{
va_list argptr ;
va_start ( argptr, fmt ) ;
vfprintf ( stdout, fmt, argptr ) ;
va_end ( argptr ) ;
}

Here, the function vfpf( ) has called vfprintf( ) that take variable argument lists. va_list is an array that holds information required for the macros va_start and va_end. The macros va_start and va_end provide a portable way to access the variable argument lists. va_start would set up a pointer argptr to point to the first of the variable arguments being passed to the function. The macro va_end helps the called function to perform a normal return.

Is it helpful? Add Comment View Comments
 

Ques 77. Can we change the system date to some other date?

Yes, We can! The function stime( ) sets the system date to the specified date. It also sets the system time. The time and date is measured in seconds from the 00:00:00 GMT, January 1, 1970. The following program shows how to use this function.
#include <stdio.h>
#include <time.h>

void main( )
{
time_t tm ;
int d ;

tm = time ( NULL ) ;

printf ( "The System Date : %s", ctime ( &tm ) ) ;
printf ( "nHow many days ahead you want to set the date : " ) ;
scanf ( "%d", &d ) ;

tm += ( 24L * d ) * 60L * 60L ;

stime ( &tm ) ;
printf ( "nNow the new date is : %s", ctime ( &tm ) ) ;
}
In this program we have used function ctime( ) in addition to function stime( ). The ctime( ) function converts time value to a 26-character long string that contains date and time.

Is it helpful? Add Comment View Comments
 

Ques 78. How to use function strdup( ) in a program?

The string function strdup( ) copies the given string to a new location. The function uses malloc( ) function to allocate space required for the duplicated string. It takes one argument a pointer to the string to be duplicated. The total number of characters present in the given string plus one bytes get allocated for the new string. As this function uses malloc( ) to allocate memory, it is the programmerâ??s responsibility to deallocate the memory using free( ).
#include <stdio.h>
#include <string.h>
#include <alloc.h>

void main( )
{
char *str1, *str2 = "double";

str1 = strdup ( str2 ) ;
printf ( "%sn", str1 ) ;
free ( str1 ) ;
}

Is it helpful? Add Comment View Comments
 

Ques 79. On including a file twice I get errors reporting redefinition of function.
How can I avoid duplicate inclusion?

Redefinition errors can be avoided by using the following macro definition. Include this definition in the header file.
#if !defined filename_h
#define filename_h
/* function definitions */
#endif
Replace filename_h with the actual header file name. For example, if name of file to be included is 'goto.h' then replace filename_h with 'goto_h'.

Is it helpful? Add Comment View Comments
 

Ques 80. How to write a swap( ) function which swaps the values of the variables using bitwise operators.

Here is the swap( ) function.
swap ( int *x, int *y )
{
*x ^= *y ;
*y ^= *x ;
*x ^= *y ;
}
The swap( ) function uses the bitwise XOR operator and does not require any temporary variable for swapping.

Is it helpful? Add Comment View Comments
 

Most helpful rated by users: