Syntax of the System() Function in C Language
Description of the System() Function in C Language
The system() function executes a command in the Linux system shell or another OS. If the command is executed correctly, system() returns “0”. This function has a pointer to the string str as its only input argument, which contains the command to be executed in the system verbatim and without syntax errors. The system() function executes the commands in the Linux system, but does not retrieve any information or return any results of the executed command.
System() is part of the “stdlib” standard library. To use it, we must include it in our code file as follows:
Once the “stdlib.h” library is included, we can use the system() function. Next, we will see some examples of using the system() function to execute the various commands in the Linux interpreter.
Example 1: How to Execute a Command in the Linux Interpreter Using the System() Function in C
In this example, we open a file with the fopen() function and use the system() function to send a beep to the system in case of an opening error.
The fopen() function returns 0 if an error occurs when opening the file. We put this value as a condition in an if-condition and execute the “beep” command in the interpreter with system( ) to inform the user about its error.
We see the code for this purpose in the following illustration. In the path that specifies the file, we put the name of a non-existent file to generate an error:
Example 2: How to Recognize Whether the Command Executed with the System() Function Is Interpreted Correctly
In this example, we explain how to determine whether the command is executed correctly in the Linux console. An error in the execution of the command itself does not refer to an error in the execution of the system() function. Therefore, the system does not log this exception in the error code variable, “errno”. The system() function executes the commands on the system, but does not return the results. The output argument of this function is an integer which returns “0” if the command is successfully executed on the system. Otherwise, it returns another value.
Next, we see a code fragment where we use the return of the system() function to determine if the command is executed correctly.
In this code, we send the “beep” command. But for the practice of this example, we can send the various correct and incorrect commands into the input argument of the system() function to see the different results.
We use the return of the system() function as a condition in an if-else condition. If the command is executed correctly, a message is displayed on the screen. Here is the code:
If you are getting an error try this command see it work well:
Conclusion
In this Linux Hint article, we explained how to use the system() function to execute the commands in the Linux shell. We also looked at the input argument and the data it accepts, as well as the output argument and the return value when the command is successfully executed. In addition, we have given some practical examples with code fragments and images that show how to use this function by calling and executing the various commands of the Linux interpreter via system(). We hope that this article is useful for you. For more articles like this, use our search engine on the website.