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:


int main ()
		int x;

		printf ("\n\nIs command processor available?\n");
		if (system(NULL))
			printf ("Command processor available!\n");
			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                                                                
Returned value is: 0.


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


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

Why do C and C++ compilers allow array lengths in function signatures when they're never enforced?

It is a quirk of the syntax for passing arrays to functions.

Actually it is not possible to pass an array in C. If you write syntax that looks like it should pass the array, what actually happens is that a pointer to the first element of the array is passed instead.

Since the pointer does not include any length information, the contents of your [] in the function formal parameter list are actually ignored.

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