Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C Exercises: Invoke the command processor to execute a command

C Variable Type: Exercise-1 with Solution

Write a C program which will invoke the command processor to execute a command.

Sample Solution:

C Code:

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

int main ()
	{
		int x;

		printf ("\n\nIs command processor available?\n");
		if (system(NULL))
		{
			printf ("Command processor available!\n");
		}
		else
		{
			printf ("Command processor not available!\n");
			exit (1);
		}
		printf ("Executing command DIR\n");
		x=system ("dir");
		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:

Improve this sample solution and post your code through Disqus.

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.



C Programming: Tips of the Day

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP