Other Topics

Page Contents:

Variable-Length Argument Lists:
Command -Line Arguments:
exit and atexit functions:
C++ volatile type Qualifier:
Suffixes for integer and Floating-Point Constants:
C++ Signal Handling:

Variable-Length Argument Lists:

It is possible to create functions that receive an unspecified number of arguments. An ellipsis in a function’s prototype indicates that the function receives a variable number of arguments of any type. Note that the ellipsis must always be placed at the end of the parameter list, and there must be at least one argument before the ellipsis. The macros and definitions of the variable arguments header<cstddarg> provide the capabilities necessary to build functions with variable-length argument lists.

identifierDescription
va_list A type suitable for holding information needed by macros va_start. va_arg and va_end.To access the arguments in a variable-length arguments list, an object of type va_list must be declared
va_start A macro that is invoked before the arguments of a variable-length argument list can be accessed.The macro initializes the object declared with va_list for use by the va-arg and va_end macros
va_arg A macro that expands to an expression of the value and type of the next argument in the variable-length argument list.Each invocation of va_arg modifies the object declared with va_list so that the object points to the next argument in the list
va_end A macro that performs termination housekepping in a function whose variable-length argument list was referred to by the va_start macro

Write a program using variable-length argument lists

#include<iostream>
using std::cout;
using std::endl;
using std::ios;
#include<iomanip>
using std::setw;
using std::setprecision;
using std::setiosflags;
using std::fixed;
#include<cstdarg>
double average(int, ...);
int main()
{
double double1=37.5;
double double2=22.5;
double double3=1.7;
double double4=10.2;
cout<<fixed<<setprecision(1)<<"double1="
    <<double1<<"\ndouble2="<<double2<<"\ndouble3"
    <<double3<<"\ndouble4="<<double4<<endl
   <<setprecision(3)
   <<"\nThe average of double1 and double2 is"
   <<average(2,double1,double2)
   <<"\nThe average of double1,double2,and double3 is"
   <<average(3,double1,double2,double3)
   <<"\nThe average of double1,double2,double3"
   <<"and doubled is"
   <<average(4,double1,double2,double3,double4)
   <<endl;
return 0;
}
double average(int count,...)
{
double total=0;
va_list list;
va_start(list,count);
for(int i=1;i<=count;i++)
total+=va_arg(list,double);
va_end(list);
return total/count;
}

OUTPUT:

double1=37.5
double2=22.5
double3=1.7
double4=10.2

The average of double1 and double2 is 30.000
The average of double1,double2, and double3 is 20.567
The average of double1,double2, double3 and double4 is 17.975

Command -Line Arguments:

It is possible to pass arguments to main from a command line by including parameters like int argc and char *argv[] in the parameter list of main. Parameters argc receives the number of command-line arguments. Parameters argv is an array of char*’s pointing to strings in which the actual command-line arguments are stored. Common uses of command-line arguments include printing the arguments, passing options to a program and passing filenames to a program.

Write a program using command-line arguments

#include<iostream>
using std::cout;
using std::endl;
using std::ios;
#include<fstream>
using std::ifstream;
using std::ofstream;
int main(int argc,char *argv[])
{
if(argc!=3)
cout<<"Usage:copyFile infile_name outfile_name"<<endl;
else
{
ifstream inFile(argv[1],ios::in);
if(!inFile)
{
cout<<argv[1]<<"could not be opened"<<endl;
return -1;
}
ofstream outFile(argv[2],ios::out);
if(!outFile)
{
cout<<argv[2]<<"could not be opened"<<endl;
inFile.close();
return -2;
}
char c=inFile.get();
while(inFile)
{
outFile.put(c);
c=inFile.get();
}
}
return 0;
}

OUTPUT:

Usage:copyFile infile_name outfile_name 

exit and atexit functions:

The general utility library (<cstdlib>) provides methods of terminating program execution other than a conventional return from function main. Function exit forces a program to terminate as if it executed normally. The function often is used to terminate a program when an error is detected in the input or if a file to be processed by the program cannot be opened. Function atexit registers a function in the program to be called when the program terminates by reaching the end of main or when the exit is invoked.

Function atexit takes a pointer to a function(i.e. the function name) as an argument. Functions called at program termination cannot have arguments and cannot return a value.

Function exit takes one argument. The argument is normally the symbolic constant EXIT_SUCCESS or EXIT_FALIURE. If exit is called with EXIT_SUCCESS, the implementation-defined value for successful termination is returned to the calling environment. If the exit is called with EXIT_FALIURES, the implementation-defined value for unsuccessful termination is returned. When function exit is invoked, any functions previously registered with atexit are invoked in the reverse order of their registration, all streams associated with the program are flushed and closed, and control returns to the host environment.

Write a program using exit and atexit functions

#include<iostream>
using std::cout;
using std::endl;
using std::cin;
#include<cstdlib>
void print();
int main()
{
atexit(print);
cout<<"Enter 1 to terminate program with function exit"
    <<"\nEnter 2 to terminate program normally\n";
int answer;
cin>>answer;
if(answer==1)
{
cout<<"\nTerminating program with function exit\n";
exit(EXIT_SUCCESS);
}
cout<<"\nTerminating program by reaching the end of main"
    <<endl;
return 0; 
}
void print()
{
cout<<"Executing function print at program termination\n"
    <<"Program terminated"<<endl;
}

OUTPUT:

Enter 1 to terminate program with function exit
Enter 2 to terminate program normally
1

Terminating program with function exit
Executing function print at program termination                                          Program terminated 

C++ volatile type Qualifier:

The volatile type qualifier is applied to a definition of a variable that may be altered from outside the program(i.e. the variable is not completely under the control of the program). Thus, the compiler cannot perform optimizations(such as speeding program execution or reducing memory consumption) that depend on “knowing that a variable’s behavior is influenced only by program activities the compiler can observe”.

Suffixes for integer and Floating-Point Constants:

C++ provides integer and floating-point suffixes for specifying the types of integer and floating-point constants. The integer suffixes are u or U for an unsigned integer,l or L for a long integer, and ul or UL for an unsigned long integer. The following are the examples for unsigned, long and unsignedlong

174u
8358L
28373ul

If an integer constant is not suffixed, its type is int; if the constant cannot be stored in an int it is stored in a long.

The floating-point suffixes are f or F for a float and l and L for a long double.The following constants are type long double and float respectively.

3.14159L
1.28f

A floating-point constant that is not suffixed is of type double. A constant with improper suffix results in either a compiler warning or an error.

C++ Signal Handling:

An unexpected event, or signal, can terminate a program prematurely. Some unexpected events include interrupts, illegal instructions, segmentation violations, termination orders from the operating systems and floating-point exceptions. The signal-handling library provides a function signal to trap unexpected events. The function signal receives two arguments an integer signal number and a pointer to the signal-handling function. Signals can be generated by function raise, which takes an integer signal number as an argument.

Signals defined in header<csignal>

Signal Explanation
SIGABRT Abnormal termination of the program(such as a call to abort)
SIGFPE An erroneous arithmetic operation, such as a divide by zero or an operation resulting in overflow.
SIGILL Detection of an illegal instruction.
SIGINT Receipt of an interactive attention signal.
SIGSEGV An invalid access to storage.
SIGTERM A termination request sent to the program

Write a Program using signal handling

#include<iostream>
using std::cout;
using std::cin;
using std::endl;
#include<iomanip>
using std::setw;
#include<csignal>
#include<cstdlib>
#include<ctime>
void signalHandler(int);
int main()
{
signal(SIGNIT,signalHandler);
srand(time(0));
for(int i=1;i<=100;i++)
{
int x=1+rand()%50;
if(x==25)
raise(SIGINT);
cout<<setw(4)<<i;
if(i%10==0)
cout<<endl;
}
return 0;
}
void signalHandler(int signalValue)
{
cout<<"\nInterrupt signal("<<signalValue
    <<") received.\n"
    <<"Do you wish to continue (1=yes or 2=no)";
int response;
cin>>response;
while(response!=1&&response !=2)
{
cout<<"(1=yes or 2=no)?";
cin>>response;
}
if(response!=1)
exit(EXIT_SUCCESS);
signal(SIGINT,signalHandler);
}

OUTPUT:

1   2   3   4   5   6   7   8   9  10
11  12  13  14  15  16  17  18  19  20
21  22  23  24  25  26  27  28  29  30
31  32  33  34  35  36  37  38  39  40
41  42  43  44                                                                  
Interrupt signal(2) received.
Do you wish to continue (1=yes or 2=no)   

Translate »