🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
The system() function in C is a standard library function that executes a system command. It is part of the C standard library (stdlib.h). This function is useful for running shell commands from within a C program, allowing the program to interact with the operating system.
system() Function Syntax
The syntax for the system() function is as follows:
int system(const char *command);
Parameters:
command: A C string that contains the command to be executed. IfcommandisNULL, thesystem()function checks if a command processor is available.
Returns:
- The function returns an implementation-defined value, typically the exit status of the command executed. If the
commandisNULLand a command processor is available, it returns a non-zero value; otherwise, it returns0.
Understanding system() Function
The system() function passes the command string to the host environment to be executed by the command processor. The behavior and return value of this function can vary depending on the system and command executed. It is important to check the return value to determine if the command was executed successfully.
Examples
Executing a Simple Command
To demonstrate how to use system() to execute a simple command, we will write a program that lists the contents of the current directory.
Example
#include <stdio.h>
#include <stdlib.h>
int main() {
// Execute the "ls" command (or "dir" on Windows)
int result = system("ls");
// Check the return value
if (result == -1) {
printf("Error: Command processor not available.\n");
return 1;
}
// Print the result of the command
printf("Command executed with return value: %d\n", result);
return 0;
}
Output:
[contents of the current directory]
Command executed with return value: 0
Checking the Return Status of a Command
This example shows how to check the return status of a command executed by system().
Example
#include <stdio.h>
#include <stdlib.h>
int main() {
// Execute a command that is expected to fail
int result = system("nonexistent_command");
// Check the return value
if (result == -1) {
printf("Error: Command processor not available.\n");
return 1;
} else if (result != 0) {
printf("Command failed with return value: %d\n", result);
} else {
printf("Command executed successfully.\n");
}
return 0;
}
Output:
sh: nonexistent_command: command not found
Command failed with return value: 32512
Real-World Use Case
Running a Backup Script
In real-world applications, the system() function can be used to run scripts or commands that automate system tasks, such as backing up files.
Example: Running a Backup Script
#include <stdio.h>
#include <stdlib.h>
int main() {
// Command to run a backup script
const char *backup_command = "/path/to/backup_script.sh";
// Execute the backup script
int result = system(backup_command);
// Check the return value
if (result == -1) {
printf("Error: Command processor not available.\n");
return 1;
} else if (result != 0) {
printf("Backup script failed with return value: %d\n", result);
} else {
printf("Backup script executed successfully.\n");
}
return 0;
}
Output:
[output of the backup script]
Backup script executed successfully.
Conclusion
The system() function allows a C program to execute a shell command as if it were typed at the command line. This function can be used to perform various system-related tasks, such as file operations, invoking other programs, or managing processes.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment