system() Function
The system()
function is declared in the header file <stdlib.h>
.
The system()
function enables a program to execute an operating system command by invoking the command processor. It provides a simple interface to run shell commands directly from your C code, allowing integration with the host system’s command environment.
Syntax of system()
int system(const char* command);
Parameters
Parameter | Description |
---|---|
command | A C-string containing the command to be executed. Alternatively, a null pointer can be passed to check if a command processor is available. |
Note that if command
is a null pointer, system()
will not execute any command but will simply check for the presence of a command processor. The behavior and return value when executing a command may vary depending on the system and library implementation.
Return Value
If command
is a null pointer, the function returns a nonzero value if a command processor is available and zero if not. If command
is not a null pointer, the return value is typically the status code returned by the executed command, though this is system-dependent.
Examples for system()
Example 1: Executing a Simple Shell Command
This example demonstrates how to use system()
to execute a simple shell command that prints a message to the console.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
/* Execute a simple command to print a message */
int status = system("echo Hello, World!");
/* Print the status code returned by the command */
printf("Command exited with status: %d\n", status);
return 0;
}
Explanation:
- The program calls
system()
with the command"echo Hello, World!"
to print a message. - The status code returned by the command is stored in the variable
status
. - The status code is printed to the console.
Program Output:
Hello, World!
Command exited with status: 0
Example 2: Checking for a Command Processor
This example demonstrates how to use system()
with a null pointer to check whether a command processor is available on the system.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
/* Check if a command processor is available */
int available = system(NULL);
if (available) {
printf("A command processor is available.\n");
} else {
printf("No command processor available.\n");
}
return 0;
}
Explanation:
- The program calls
system(NULL)
to check for the availability of a command processor. - The return value is nonzero if a command processor is available and zero otherwise.
- A corresponding message is printed based on the result.
Program Output:
A command processor is available.
Example 3: Executing a Directory Listing Command
This example uses system()
to execute a command that lists the contents of the current directory. Note that the specific command may vary between operating systems.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
/* Execute a directory listing command */
#if defined(_WIN32) || defined(_WIN64)
int status = system("dir");
#else
int status = system("ls -l");
#endif
printf("Directory listing command exited with status: %d\n", status);
return 0;
}
Explanation:
- The program conditionally compiles the appropriate command for the operating system:
"dir"
for Windows and"ls -l"
for Unix-like systems. - The
system()
function is used to execute the directory listing command. - The exit status of the command is captured and printed.
Program Output:
total 20
-rwxr-xr-x 1 tutorialkart tutorialkart 16008 Feb 23 13:39 a.out
-rw-r--r-- 1 tutorialkart tutorialkart 331 Feb 23 13:39 main.c
Directory listing command exited with status: 0