C system() Function

Beginner
7 min read
Updated: Nov 2026
3 examples
<stdlib.h>

Definition

The C system() function passes a command to the host environment. With a non-null command, it invokes an implementation-defined command processor and waits for completion. With NULL, it reports whether a processor is available.

This function is declared in <stdlib.h>. Its contract matters as much as its call: validate every required precondition, check any documented sentinel, and keep ownership or host effects clear in the surrounding code. A nonzero NULL query means a processor is available; a command call returns an implementation-defined status. A NULL query returning zero means no processor. Other command failures and status meanings are platform-specific. The examples below move from the direct call to an edge condition and a practical use.

Syntax

Start with this concrete system() call:

C
system("echo Hello");
CallWhat it does
system("echo Hello");Passes a command to the host environment using the concrete values shown.

Include <stdlib.h>. Also include headers that declare any types, limits, locale tools, or I/O functions used around the call.

Library declaration
C
int system(const char *command);

Parameters

ParameterDescription
commandCommand string for the host command processor, or NULL to query availability.

Return Value

Return ValueDescription
Implementation-defined statusThe command processor was invoked; decode it using platform rules.
0 for NULL queryNo command processor is available.

C Examples

Example 1 — Query command processor support

This complete program demonstrates a distinct part of the system() contract and checks the result or precondition that matters for the scenario.

C
#include <stdio.h>
#include <stdlib.h>
int main(void){printf("processor: %s\n",system(NULL)?"available":"unavailable");}

How It Works

  1. Step 1. Pass NULL instead of a command.
  2. Step 2. The library queries command-processor availability.
  3. Step 3. A nonzero result means one is present.
  4. Step 4. No shell command is executed by this query.

Example 2 — Run a fixed demonstration command

This complete program demonstrates a distinct part of the system() contract and checks the result or precondition that matters for the scenario.

C
#include <stdio.h>
#include <stdlib.h>
int main(void){int status=system("echo Hello");printf("command invoked: %s\n",status==-1?"no":"yes");}

How It Works

  1. Step 1. Use a fixed literal rather than user-controlled text.
  2. Step 2. system() asks the host processor to execute it.
  3. Step 3. Save the implementation-defined status.
  4. Step 4. Interpret detailed exit status only with platform-specific facilities.

Example 3 — Avoid concatenating user input

This complete program demonstrates a distinct part of the system() contract and checks the result or precondition that matters for the scenario.

C
#include <stdio.h>
int main(void){const char *user="report.txt";printf("would process safely: %s\n",user);return 0;}

How It Works

  1. Step 1. Treat external text as data, not shell syntax.
  2. Step 2. Do not concatenate it into a system() command.
  3. Step 3. Prefer a direct API that accepts separate arguments.
  4. Step 4. This example deliberately prints rather than invoking a shell.

Frequently Asked Questions

system() passes a command to the host environment. With a non-null command, it invokes an implementation-defined command processor and waits for completion. With NULL, it reports whether a processor is available.
Include <stdlib.h> before calling system().
A nonzero NULL query means a processor is available; a command call returns an implementation-defined status. A NULL query returning zero means no processor. Other command failures and status meanings are platform-specific.
Never build a command from untrusted text. Shell interpretation can create command-injection vulnerabilities. Prefer direct process APIs when arguments come from users.
The function is specified by the C standard library. Exact limits, locale behavior, environment behavior, allocation capacity, or host effects can still vary where relevant.

Next: wcstombs()

Continue through the C stdlib.h tutorials.

wcstombs() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful