The system() function is used to invoke an operating system command from a C/C++ program. For example, we can call system(“dir”) on Windows and system(“ls”) in a Unix-like environment to list the contents of a directory.
It is a standard library function defined in header in C and in C++.
The syntax of system() function is:
int system(const char *command);In this program, we will use the echo command to print the “Hello World” string.
Hello, World! Command executed successfully.
We can invoke gcc from our program using system(). See below the code written for Linux. We can easily change code to run on Windows.
To convert the above code for Windows we need to make some changes. The executable file extension is .exe on Windows. So, when we run the compiled program, we use a.exe instead of ./a.out.
Some common uses of system() in Windows OS are:
However, making a call to system command should be avoided due to the following reasons:
Let us take a simple C++ code to output Hello World using the system(“pause”) :
The output of the above program in Windows OS:
Hello World!This program is OS-dependent and uses the following heavy steps:
Instead of using the system(“pause”), we can also use the functions that are defined natively in C. Let us take a simple example to output Hello World with cin.get():
Hello World!
Thus, we see that both system(“pause”) and cin.get() are actually performing a wait for a key to be pressed, but, cin.get() is not OS dependent and neither does it follow the above-mentioned steps to pause the program.
The common way to check if we can run commands using system() in an OS is to check if a command processor (shell) exists in the operating system.
Using the following way, we can check if a command processor exists in an OS:
If we pass a null pointer in place of a string for the command parameter,
Command processor exists
Subhankar Das Like Article -->Note : The above programs may not work on online compiler as System command is disabled in most of the online compilers including GeeksforGeeks IDE .
First Method Printing current date and time using time() Second Method // CPP program to print current date and time // using time and ctime. #include <stdio.h> #include <stdlib.h> #include <time.h> int main() < // declaring argument of time() time_t my_time = time(NULL); // ctime() used to give the present time printf("%s
2 min read Timer in C++ using system callsThe task is to create timer without using any graphics or animation. The timer will be made using system calls wherever necessary. Timer in this context means a stopwatch with up-counting of time.The timer is created in Linux. Following system calls of Linux are used: sleep() : It will make the program sleep for number of seconds provided as argume
2 min read Bank account system in C using File handlingThis article focuses on how to create a bank account system using C language and File handling in C. Approach:Let's discuss the approach in detail, covering all the functions and their explanation in detail- Create a Menu in the main function and create different functions for the Menu, which will be called using switch case statements. There are f
12 min read Menu-Driven Program for Bank Management SystemPrerequisite: Switch Case in C/C++Problem Statement: Write a program to build a simple Bank Management System using C++ which can perform the following operations: Open accountDeposit MoneyWithdraw MoneyDisplay Account Approach: Below is the approach to do the above operations: Open Account: This method takes details from the customer like name, ad
10 min read Getting System and Process Information Using C Programming and Shell in LinuxWhenever you start a new process in Linux it creates a file in /proc/ folder with the same name as that of the process id of the process. In that folder, there is a file named "status" which has all the details of the process. We can get those Process Information Through shell as follows: cat /proc/1/status As can be seen, it displays most of the i
2 min read How does Volatile qualifier of C works in Computing SystemPrerequisite: Computing systems, Processing unit Processing Unit:Processing units also have some small memory called registers.The interface between the processor (processing unit) and memory should work on the same speed for better performance of the system.Memory: In memory, there are two types, SRAM and DRAM. SRAM is costly but fast and DRAM is
3 min read Java System.exit(0) vs C++ return 0Java and C++ are languages with different applications and design goals. C++ is an extension of procedural programming language C and Java relies on a Java virtual machine to be secure and highly portable. This leads them to many differences. In this article, we will see the difference between C++ return 0 and Java System.exit(0). Before getting in
3 min read Difference between system() and execl() callA system() call is a way for the programs to interact with the operating system. It is used to execute a command within a process. The system call provides the services of the operating system to the user programs via Application Program Interface (API). It provides an interface between a process and an operating system and it does not replace the
4 min read Department Store Management System(DSMS) using C++A Department Store Management System (DSMS) in C++ is a software tool used to manage the inventory and sales of items in a department store. In this article, we will be discussing the implementation of a DSMS on a command-line user interface using C++. The system allows various features to admin such as add, view, update, and delete items in the st
8 min read Telecom Billing System in CIn this article, we are going to create a basic Telecom Billing System using the C programming language. This system allows users to perform operations such as adding new customer records, viewing the list of records, modifying existing records, viewing payment details, searching for specific records, and deleting records. What is a Telecom Billing
9 min read Hospital Management System in CA Hospital Management System is software that facilitates its users some basic operations that take place in a typical hospital. This Hospital Management System in C language is a project for Hospitals having the following functionalities: Printing Hospital DataPrint Patients DataSort by Bed PriceSort by Available BedsSort by NameSort by Rating and
11 min read File System Library in C++17In this article, we will learn about the File System library in C++17 and with examples. <filesystem> header was added in C++17 and introduces a set of classes, functions, and types that simplify file system operations. In simple words, we can say that the filesystem library provides tools that help us to simplify working with files and direc
6 min read Online Voting System in CVoting in elections is one of the most fundamental organs of any Democracy and in the modern world, voting is done through digital machines. In this article, we will try to write a simple Online Voting System using C programming language. Features of Online Voting System in CThe online voting system offers the following functions: Taking a Vote fro
5 min read Attendance Marking System in C++In today's modern and fast world, maintaining accurate attendance records is crucial for monitoring and managing student or employee attendance. We can automate this process by creating an Attendance Management System in C++. In this article, we will see how to implement an Attendence Management System using C++. Prerequisite: C++ OOPs, C++ File Ha
10 min read Input-output system calls in C | Create, Open, Close, Read, WriteSystem calls are the calls that a program makes to the system kernel to provide the services to which the program does not have direct access. For example, providing access to input and output devices such as monitors and keyboards. We can use various functions provided in the C Programming language for input/output system calls such as create, ope
10 min read Program to shutdown a systemHow to shutdown your computer in Linux and/or Windows? The idea is to use system() in C. This function is used to invoke operating system commands from C program. Linux OS: C/C++ Code // C program to shutdown in Linux #include <stdio.h> #include <stdlib.h> int main() < // Running Linux OS command using system system(
2 min read Amazing stuff with system() in C / C++system() plays a very special role in executing Operating System Commands. By using this library function we can run all those terminal commands that our Operating System allows us to perform, just by using our C program. Now, we will learn a very simple code to fetch the IP Address - to identify each computer using that very internet Protocol to c
2 min read pipe() System callPrerequisite : I/O System calls Conceptually, a pipe is a connection between two processes, such that the standard output from one process becomes the standard input of the other process. In UNIX Operating System, Pipes are useful for communication between related processes(inter-process communication). Pipe is one-way communication only i.e we can
4 min read Simple Personal Diary Management System in CKeeping a personal diary has been a timeless practice for a lot of individuals to reflect on their thoughts and experiences. In this article, we will look at a simple personal diary management application in C. Prerequisite: Basic C Knowledge, File Handling in C, and Time in C. Features of Diary Management System in C This simple diary management a
3 min read Number System Conversion in CNumber system conversion is a fundamental concept in computer science and programming. It involves changing the representation of a number from one base to another, such as converting a decimal number to binary or a hexadecimal number to binary. In this article, we will create a console program in the C language to perform various number system con
7 min read Wait System Call in CPrerequisite : Fork System callA call to wait() blocks the calling process until one of its child processes exits or a signal is received. After the child process terminates, parent continues its execution after wait system call instruction. Child process may terminate due to any of these: It calls exit();It returns (an int) from mainIt receives a
4 min read Bus Reservation System in CBus Reservation System is a tool that allows users to book tickets for their journey in advance. It offers multiple features to provide a hassle-free experience to a traveler. This article aims at building a rudimentary Bus Reservation System. Components of the Bus Reservation SystemLogin System: Users can access the system by entering their userna
9 min read C++ Programming LanguageC++ is the most used and most popular programming language developed by Bjarne Stroustrup. C++ is a high-level and object-oriented programming language. This language allows developers to write clean and efficient code for large applications and software development, game development, and operating system programming. It is an expansion of the C pr
9 min read C Programming Language TutorialIn this C Tutorial, you’ll learn all C programming basic to advanced concepts like variables, arrays, pointers, strings, loops, etc. This C Programming Tutorial is designed for both beginners as well as experienced professionals, who’re looking to learn and enhance their knowledge of the C programming language. What is C?C is a general-purpose, pro
8 min read 30 OOPs Interview Questions and Answers (2024) UpdatedObject-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is widely used in many popular languages like Java,
15+ min read C ProgramsTo learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and more. These C Examples cover a range of questions,
8 min read Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()Since C is a structured language, it has some fixed rules for programming. One of them includes changing the size of an array. An array is a collection of items stored at contiguous memory locations. As can be seen, the length (size) of the array above is 9. But what if there is a requirement to change this length (size)? For example, If there is
9 min read Vector in C++ STLVectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators. In vectors, data is inserted at the end. Inse
11 min read Object Oriented Programming in C++Object-oriented programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data
10 min read Data Types in CEach variable in C has an associated data type. It specifies the type of data that the variable can store like integer, character, floating, double, etc. Each data type requires different amounts of memory and has some specific operations which can be performed over it. The data type is a collection of data with values having fixed values, meaning