Open In App
Related Articles

CLI programs in C for playing media and shut down the system

Improve Article
Save Article
Like Article

Command Line Interface:

  • CLI is a text-based user interface (UI) used to view and manage computer files.
  • Command-line interfaces are also called command-line user interfaces, the console uses interfaces and characters uses interfaces.
  • In programming, the user gives input during the execution of a program i.e., in the running phase. But there are a lot of cases when input is required before the program enters execution.
  • For generalization purpose, take an example of Petrol pumps:
    • The vendor at the petrol pump enters the amount in the machine and then the machine accordingly gives that much quantity of petrol.
    • So it means that the machine is accepting the amount as the parameter of a function which in return tells the machine how many liters of petrol must be released.
    • This is a real-world application of the Command-Line Arguments Program. The machine asks for the parameter before entering the execution phase (or even before running).
  • Before going into the programs, one of the important aspects of running the command line argument programs is that the arguments are given through the command line.

How to run it?

  • Make sure that after creating the program only save and compile it.
  • Compiling is very important (because only after compiling the exe file will be updated).
  • Don’t run it as it is meant to be run through CLI.
  • So after saving it open the command prompt and then navigate to the folder where the program is saved.
  • When done with the path, write the name of the program followed by exe.


  • In the shutdown program, a command-line argument program will be made which will take a time parameter and thus accordingly shut down the computer.
  • Below given few important points:
    • sprintf library function: It writes formatted data to a string i.e., it composes a string with the same text that would be printed if the format was used on printf, but instead of being printed, the content is stored as a C string in the buffer pointed by str. The size of the buffer should be large enough to contain the entire resulting string. It contains three parameters:
      • str: Pointer to a buffer where the resulting string is stored.
      • format: C string that contains a format string that follows the same specifications as a format in printf()
      • additional arguments: Depending on the format string, the function may expect a sequence of additional arguments, each containing a value to be used to replace a format specifier in the format string.
  • Shutdown Process: Whenever a system is shut down/sleep the PC, a shutdown executable file is triggered. The path of the file in the directory is given by: C:\\Windows\\System32\\shutdown.

Below is the program to shut down the computer:


// C program to shut down the computer
#include <stdio.h>
#include <stdlib.h>
// Driver Code
int main(int argc, char* argv[])
    char buf[32];
    int time;
    // Function converting to integer
    val = atoi(argv[1]);
    sprintf(buf, "C:\\Windows\\System32\\shutdown /s /t %d", time);
    // System function to run CLI commands
    // Confirming that program is
    // running properly
    printf("Running successfully. "
           "Shutting down in %d sec",
    return 0;



  • In the above program, argc and an array argv of infinite length are used as command-line arguments in the main function.
  • The argc takes the number of arguments passed in the CLI, the array argv will store those parameters.
  • The length of argv array depends on argc.
  • A buff character is used to store the command. As in this program, only the time parameter is passed, so at argv[1] there will be having that parameter because by default argv[0] will be assigned to the file location of this program.
  • That string is converted to a number with the help of atoi method.


  • The path of a directory is “C:\Windows and so on” but in our program, “C:\\Windows” is used that is the backslashes, it is because if only one backslash is used then the further part of the string will not be evaluated.
  • So with the help of escape sequences programmer tells the compiler to not treat the second backslash as the end of the line.

Earlier the command C:\\Windows\\System32\\shutdown will be the command resulting in a shutdown. There are various parameters that can be used along with the shutdown command.

shutdown [/i | /l | /s | /sg | /r | /g | /a | /p | /h | /e | /o] [/hybrid] [/fw] [/f] [/m \\computer][/t xxx][/d [p|u:]xx:yy [/c "comment"]]
where /s stands for shut down, /r stands for restart and so on.

  • The /t defines the time-out period before shutdown.
  • By default /t has a value of 30 seconds (if not specified particularly).
  • Say if the programmer wants to shut down the computer after 45 seconds then I have to execute the command shutdown /s /t 45.
  • That is why use the command: C:\\Windows\\System32\\shutdown /s /t and after which the value entered by the user will be added there in the string.
  • So say if through CLI 40 is entered then the final string will look like this:

C:\\Windows\\System32\\shutdown /s /t 40

  • If there is a need to execute the command on CLI when the system is running, the system function comes to the rescue as the system() function is used to execute SHELL commands which can’t be directly used in the C program. So in this manner,  the code executes.

My Player:

In this program, the task is to make a command-line argument program through which insert a name of the mp3 file, and then the song will be played. So the same concepts are used. There are two ways to open an mp3 file through the command line. They are as follows:

  • Directly Opening the File: If the user wants to open test.mp3 which is residing in the song folder. So write the following command: “G:\\song\\test.mp3”.
  • Opening the mp3 file through Windows Media Player: If the user uses the following way, then the song will be played through the default Windows Media Player. The command is start wmplayer G:\\song\\test.mp3

Below is the program to illustrate the My Player program through CLI:


// C program illustrating myplayer
// program through CLI
#include <stdio.h>
#include <stdlib.h>
// Driver Code
int main(int argc, char* argv[])
    char tbo[100];
    // First version of command:
    sprintf(tbo, "G:\\song\\%s.mp3", argv[1]);
    printf("The song %s.mp3 is going "
           "to be played in a "
           "second... :",
    return 0;


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 15 Mar, 2021
Like Article
Save Article
Similar Reads