Skip to content
Tutorials Center
My learning

Topics

C Reference ยท stdlib.h

C system() function

Reference for C system(): run a command through the shell, test whether a shell exists, decode the exit status with WEXITSTATUS, and avoid injection bugs.

system() hands a command string to the operating system's command interpreter (/bin/sh -c on Unix, cmd.exe /c on Windows), waits until it finishes, and returns its status. It is the quickest way to run an external program, but it is also one of the easiest ways to introduce a security hole.

The command's own output goes straight to the same standard output and error as your program, not into a C variable. To capture it you need popen() (POSIX) or a pipe.

C
#include <stdlib.h>

int system(const char *command);

Try it

C Running a command and reading its status

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Why building commands from input is dangerous

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
int system(const char *command);
Name Description
command The command line, interpreted by the shell. NULL asks whether a shell exists.

Returns: with a NULL argument, nonzero if a command processor is available and 0 if not. Otherwise the value is implementation-defined. On POSIX it is the child's wait status, or -1 if the child could not be created; decode it with the macros WIFEXITED, WEXITSTATUS, WIFSIGNALED and WTERMSIG from <sys/wait.h>. If the shell itself could not be run, the status looks like exit(127).

Errors / undefined behavior: none in the standard beyond the above; the effects of the command are whatever the command does.

Usage notes

  • Never put untrusted text into the command string. The shell interprets ;, &&, $(), backticks, quotes and redirections, as the second example shows. Validate against an allow-list, or avoid the shell with fork and execv (exec family).
  • The status is not the exit code. On Linux an exit code of 3 appears as 3 << 8 (768). Always use WEXITSTATUS after checking WIFEXITED; on Windows the return value is the exit code directly.
  • Flush before calling. Buffered text in your process is written when you flush, not in program order relative to the child's output.
  • It is slow and heavy: it creates a process and a shell for each call. Calling it in a loop thousands of times is expensive.
  • Portability: command syntax differs per system (ls or dir, clear or cls). Calling system("pause") or system("cls") to hold a console window is a common beginner habit that you should avoid.
  • Signals: while system waits, SIGCHLD is blocked, and SIGINT and SIGQUIT are ignored in the parent on POSIX.
  • In a sandbox such as this website's runner the command runs inside the restricted environment: no network and few programs.