I am trying to learn what we can do using the system () call in C.
No need to learn: you can do everything you can do from the command line. Regardless of the arguments you pass to the system , it is basically "typed" on the command line. But that is why you should avoid this feature at all costs! This is unsafe, it is slow, there is no reliable way to handle errors, and almost always there is a better way to accomplish what you want.
This case is a good example. Using system to call shutdown.exe is the wrong way to shut down Windows programmatically. There is an API for this: ExitWindowsEx . The uFlags parameter uFlags used to indicate whether you want to log off from the user who owns the process that is calling the function, restart the system, or disconnect the system.
You use it from C as follows:
#include <stdio.h> #include <stdlib.h> #include <Windows.h> // need this to call Windows APIs int main(void) { char ch; printf("Do you want to shutdown your computer now (y/n)\n"); scanf("%c",&ch); if (ch == 'y' || ch == 'Y') ExitWindowsEx(EWX_POWEROFF, 0); return 0; }
Of course, as with shutdown.exe using system , the application will need the appropriate permissions to be able to initiate a system shutdown. Simply put, you will need to run the application with administrator rights. As David suggests , from an elevated command prompt. The related documentation for the ExitWindowsEx function contains more detailed information about the security model if you are interested.
The obvious criticism is that ExitWindowsEx is platform-based. You cannot call this function on a Unix-based computer because it is not implemented. You will need to call the appropriate POSIX API (or whatever on your platform). If you really want to write platform-independent code for which you need to use conditional compilation (i.e. #if ) that detect the target OS and call the corresponding APIs, which is ugly.
The problem is that the system function has exactly the same limitation. No matter what arguments you pass, they execute as if you entered them on the command line. And the command-line environment on different operating systems is very different. This is just a coincidence if both Windows and some other operating systems have a shutdown . And even then it is unlikely to work the same way. That way, you still have ugly platform-specific code. You can also call the correct system APIs.
The main use of the system function is that it allows you to run another application from your own application. But there is an API function for this: CreateProcess . It is much more powerful, taking various arguments, to precisely control how the running process should behave. And most importantly, it provides a standardized way of reporting and error handling.
Oh, and hard coding paths are always wrong , regardless of whether you use system , CreateProcess or a low-level batch file. The system drive may not be "C". Windows cannot be installed in a directory called "Windows." And so on. This is another advantage of invoking the corresponding OS APIs: they do not require hard-coding paths or special knowledge of the default command-line environment (i.e., Path).