System() Function in C/C++

The system() function is a part of the C standard library that executes system commands. It is used to pass commands that can be executed in the command processor or terminal of the operating system, and returns the command's exit status after completion.

Note: To use the system() function, include <stdlib.h> header file.

Syntax

int system(const char *command);

Parameters

  • command − A pointer to a null-terminated string containing the command to be executed. If NULL, checks if command processor is available.

Return Value

  • Returns the exit status of the executed command
  • Returns 0 if the command executes successfully
  • If command is NULL, returns non-zero if command processor is available, 0 otherwise

Example 1: Executing System Commands

This example shows how to execute different system commands using system() function −

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

int main() {
    int result;
    
    printf("Executing 'date' command:\n");
    result = system("date");
    printf("Command exit status: %d\n\n", result);
    
    printf("Executing 'echo' command:\n");
    result = system("echo Hello from system function!");
    printf("Command exit status: %d\n", result);
    
    return 0;
}
Executing 'date' command:
Thu Dec 28 10:30:15 UTC 2023
Command exit status: 0

Executing 'echo' command:
Hello from system function!
Command exit status: 0

Example 2: Using Variables to Store Commands

You can store commands in character arrays and pass them to system()

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

int main() {
    char cmd[50];
    
    strcpy(cmd, "ls -la");
    printf("Executing command: %s\n", cmd);
    system(cmd);
    
    return 0;
}
Executing command: ls -la
total 8
drwxr-xr-x 2 user user 4096 Dec 28 10:30 .
drwxr-xr-x 3 user user 4096 Dec 28 10:30 ..
-rwxr-xr-x 1 user user    0 Dec 28 10:30 main

Example 3: Checking Command Processor Availability

You can check if the command processor is available by passing NULL to system()

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

int main() {
    if (system(NULL)) {
        printf("Command processor is available\n");
        system("echo Testing system function");
    } else {
        printf("Command processor is not available\n");
    }
    
    return 0;
}
Command processor is available
Testing system function

Key Points

  • Security Risk: Never use system() with user input as it can lead to command injection attacks
  • Portability: Commands are platform-specific (Windows vs. Linux/Unix)
  • Performance: system() creates a new process, which has overhead
  • Error Handling: Always check the return value to handle command failures

Conclusion

The system() function provides a simple way to execute system commands from C programs. However, use it carefully due to security implications and always validate the return value for proper error handling.

Updated on: 2026-03-15T12:38:41+05:30

38K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements