w3resource logo


C Programming Exercises

C Exercises: Invoke the command processor to execute a command

Secondary Nav

C Variable Type: Exercise-1 with Solution

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

Expected Output :

Is command processor available?                                         
Command processor available!                                            
Executing command DIR                                                   
00c40280-5e27-11e6-bd4f-71e8825f8ea3                                    
01691610-41e1-11e6-901d-35b72ececc72                                    
01822580-4414-11e6-a0ac-5bb3315a1c3b                                    
01f87d40-4430-11e6-a16f-0d5e7f809f57                                    
01fd2c70-4415-11e6-b8b3-716f0bc87a18
...........
ff1ee9c0-4366-11e6-9b0a-216d29b4f7e4                                    
ff7d3890-4375-11e6-a082-9f422746c049                                    
ff827330-443a-11e6-9820-23e2f60d924e                                    
file.txt                                                                
logging_example.out                                                     
test.txt                                                                
Returned value is: 0.

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;
	}   

Solution


#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;
}   

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.

Solve these problems:

Java: How to convert a string to an integer in Java?

C#: Loops in c#

SQL: JOIN using more than 5 tables