w3resource

C Exercises: Invoke the command processor to execute a command

C Variable Type: Exercise-1 with Solution

Write a C program that invokes the command processor to execute a command.

Sample Solution:

C Code:

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

int main ()
{
int x;

    // Print a message indicating that the command processor availability is being checked
printf ("\n\nIs command processor available?\n");

    // Check if the command processor is available
if (system(NULL))
    {
        // If it is available, print a message indicating so
printf ("Command processor available!\n");
    }
else
    {
        // If it is not available, print a message and exit with a status of 1
printf ("Command processor not available!\n");
exit (1);
    }

    // Print a message indicating that the command 'DIR' is being executed
printf ("Executing command DIR\n");

    // Execute the 'DIR' command and store its return value in 'x'
    x=system ("dir");

    // Print the returned value
printf ("Returned value is: %d.\n",x);

return 0;
} 
  

Sample Output:

Is command processor available?                                                                               
Command processor available!                                                                                  
Executing command DIR                                                                                         
0101d560-54e9-11e7-a85b-5dffbb229414    7d2db170-55a9-11e7-be91-9d836d04a23e                                  
0137a4a0-55a8-11e7-89d9-6907ca0db017    7d3f5910-5195-11e7-8c7b-836f726e36d0                                  
015b94c0-4a8e-11e7-8e36-ebbcdd3971ae    7d630300-57fe-11e7-9381-9d994cae8e36                                  
015c43a0-55a8-11e7-89d9-6907ca0db017    7de4f070-51ac-11e7-ae6a-cd387a54803c                                  
01991c80-4a9a-11e7-a463-0d368a9e12b1    7e280e40-57de-11e7-ba21-b9739f8cb956                                  
01ae5930-51a0-11e7-80b0-cbb971bc6112    7e814da0-445d-11e7-b771-21ad0f863a1c                                  
01d3bd20-49ed-11e7-8ea6-611e29526b70    7eb35360-4b5e-11e7-a793-e3da3ad86a97                                  
01d7b150-4a88-11e7-804e-0b936a3310fd    7f2db220-54e3-11e7-b89b-7d525be75d15                                
-----
7d056f80-50f4-11e7-8ba9-adac345928ff  temp.txt                                                                
7d2db170-55a9-11e7-be91-9d836d04a23e  test.png                                                                
7d3f5910-5195-11e7-8c7b-836f726e36d0  test.txt                                                                
7d630300-57fe-11e7-9381-9d994cae8e36                                                                          
Returned value is: 0.

Flowchart:

C Exercises Flowchart: Invoke the command processor to execute a command

Solution

C Programming Code Editor:

Previous: C Variable Type Exercises Home
Next: Write a C program to convert a string to an unsigned long integer.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming-exercises/variable-type/c-variable-type-exercises-1.php