We have already known that we
can jump out of a loop using either the break statement or goto statement. In a
similar way, we can jump out of a program by using the library function exit(
). In case, due to some reason, we wish to break out of a program and return to
the operating system.
The general
syntax is
- - - - -
- - - - -
if (condition) exit (0) ;
- - - - -
- - - - -
The exit( ) function takes an integer value as its
argument. Normally zero is used to indicate normal termination and non zero
value to indicate termination due to some error or abnormal condition. The use
of exit( ) function requires the inclusion of the header file <stdio.h>.
/* Program to
demonstrate exit( ) */
#include<stdio.h>
#include<stdlib.h>
main()
{
int
choice ;
while(1)
{
printf
(“ 1. Create database\n”) ;
printf
(“ 2. Insert new record\n”) ;
printf
(“ 3. Modify a record\n”) ;
printf
(“ 4. Delete a record\n”) ;
printf
(“ 5. Display all records\n”) ;
printf
(“ 6. Exit\n”) ;
printf
(“Enter your choice :” ) ;
scanf
(“%d”, &choice) ;
switch
(choice)
{
case1:
printf
(“:datase created - - -\n\n”) ;
break ;
case2:
printf
(“Record inserted - - -\n\n”) ;
break ;
case3:
printf
(“Record modified - - -\n\n”) ;
break ;
case4:
printf
(“Record deleted - - -\n\n”) ;
break ;
case5:
printf
(“Record displayed - - -\n\n”) ;
break ;
case6:
exit(1)
default:
printf
(“Wrong choice\n”) ;
} /* end
of switch */
} /* end
of while */
} /* end
of main() */
Comments
Post a Comment