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");
Call
What 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
Parameter
Description
command
Command string for the host command processor, or NULL to query availability.
Return Value
Return Value
Description
Implementation-defined status
The command processor was invoked; decode it using platform rules.
0 for NULL query
No 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");}
Output
processor: available
(on typical hosted systems)
How It Works
Step 1. Pass NULL instead of a command.
Step 2. The library queries command-processor availability.
Step 3. A nonzero result means one is present.
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");}
Output
Hello
command invoked: yes
(platform dependent)
How It Works
Step 1. Use a fixed literal rather than user-controlled text.
Step 2. system() asks the host processor to execute it.
Step 3. Save the implementation-defined status.
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;}
Output
would process safely: report.txt
How It Works
Step 1. Treat external text as data, not shell syntax.
Step 2. Do not concatenate it into a system() command.
Step 3. Prefer a direct API that accepts separate arguments.
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.