C Programming

System() Function in C Language

In this Linux Hint article, we will explain everything that you need to know to execute commands from C code using the system() function. We will explain the theoretical framework of this function, its input and output arguments, and the type of data it accepts in each case. We will then apply what we learned in a practical example that includes the code snippets where we execute the various Linux commands from our C code.

Syntax of the System() Function in C Language

int system (const char *str);

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:

#include <stdlib.h>

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:

#include <stdlib.h>
#include <stdio.h>

void main()
{
      FILE *f_Ptr;
      char buffer[250];

      f_Ptr = fopen("Documents/ not exist" , "r");

      if ( f_Ptr == 0 )
      {
         system("beep");
         printf ("No such file\n");
      }
}

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:

#include <stdlib.h>
#include <stdio.h>

void main() {

  int a;

  a = system("beep");

  if ( a == 0 )
      printf ("The command was executed successfully\n");
    else
      printf ("The command was not recognized or could not be executed\n");
}

If you are getting an error try this command see it work well:

#include <stdlib.h>
#include <stdio.h>

void main() {

  int a;

  a = system("date");

  if ( a == 0 )
      printf ("The command was executed successfully\n");
    else
      printf ("The command was not recognized or could not be executed\n");
}

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.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).