The C language came up with such a function that can be used to exit the program execution without executing the next lines of codes. The C/C++ computer language’s exit() method is a fundamental operation that is used to immediately terminate the invoking activity (function). By supplying an int number, it notifies the OS of the condition of the program’s end. When an unanticipated program crashes occur, it is typically employed. It is also acknowledged as the present condition of the program. Irrespective of how many method operations are made, a program can be terminated at any time using the exit method.
We will list the files and directories of the current working directory of Linux with the use of the “ls” list instruction. We have a single folder named “works” in it. Let’s move within this folder with the “cd” instruction followed by the name of a folder, “works”. We create a new C file with the Vim editor within this folder named “exitFunc.c”.
The follwoing code is utilized for our first example. We started this code with the use of some standard C libraries, i.e. “studio.h” for the standard input and output stream and the “stdlib.h” as the standard library of C to get used to the standard functions. This code consists of a single main() function. We use the first two lines of the printf() function to tell that the function has been started and it’s going to exit very soon with a line breakvia the “\n” character. The “exit()” function is used with the 0 value to terminate the function without executing the next lines of code. The 4th line contains another “printf” function to display that the program is now going to end. Save this code and exit the vim file.
We compile this C code with the “gcc” compiler of C and create an object file named “exitFun.out” with the “.out” extension within the same working directory via the “-o” option. After using the list “ls” instruction, we get the contents of the “works” folder displayed. The creation of the object file is also visible. The output, along with the relevant command, can be checked from the affixed image.
Now, we use this object file of our C code to see the output via the “./” execution instruction. The output displays the two text lines of this code that are printed using the printf function in the main() method. After that, our program code exits via the “exit” 3 C function, and the last lines of code are left executed. The output, along with the relevant command, can be checked from the affixed image.
Let’s examine another C sample that makes use of the Kali Linux’s exit 3 C function. The same header files are included in the code with the #include keyword. Two integer variables, dividend, and divisor are declared to begin the main() function. The printf() function statement of C is used to show and ask a user to enter the value for the “dividend” variable.
After this, the scanf() function of C is used to get the value for the “dividend” variable as an input at runtime. After this, the same printf() function is used to ask the value for a “divisor” variable from the user. The scanf() function is utilized again to get the input value from the user at runtime. Here, comes the “if-else” statement of C to check for the standard mathematical rule for division – if the divisor is 0, then you cannot divide the dividend with the divisor. The “if” statement is checked if the “divisor” variable value is equal to 0 or not. If so, the printf() statement within it displays that the “divisor cannot be zero” and the exit() function will terminate the program at the spot.
Within the “else” part of the statement, we calculate the float division value “result” using the dividend divided by the divisor formulae after converting both to float. The printf() statement is here to display the dividend, divisor, and the result value and exit the program via the “exit()” function. Save this code and compile it to see the results. The code for this sample illustration is affixed in the following image:
Let’s compile this code with the same gcc compiler and create an object file named “exitFunc.c” with the –o” option.
After that, we execute its object file with the “./” instruction. The program execution is asked for the dividend value first and then the divisor value. We have added 12 and 5, respectively. The result of the division is calculated and displayed on our screen in float, i.e. 2.40000. The output, along with the relevant command, can be checked from the affixed image.
Again, execute the object file and add 12 and 0, respectively. Since the “0” value for the divisor is not allowed, the division has not taken place, i.e. the divisor cannot be displayed as zero. The output, along with the relevant command, can be checked from the affixed image.
This article is all about the use of the C exit() 3 function in the Kali Linux system within the C program to terminate the execution of a C program. It turns out that it exits the execution very smoothly and without any hurdle. We discussed the two examples in our article to display and discuss its usage especially when you are utilizing some conditional statements in the C codes.