C Reference function system()

The function system() will invoke the command processor to execute a command. If the command execution is terminated the processor will give the control back to the program that has called the system command.

Usage of system():

int system ( const char * command );

It will return an integer value, which is interpretation is different on various systems, thus system dependent.
If you want to check whether there is a command processor exist then you use the NULL argument in the function.

Parameters:

A C string that is containing a system command name.

Return value:

When the argument passed is NULL then the function will return a nonzero value if the command processor is available.
If it is not available then zero returned.

Source code example of system():


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

	int main ()
	{
		int i;

		printf ("Is command processor available?\n");
		if (system(NULL))
		{
			printf ("Command processor available!\n");
		}
		else
		{
			printf ("Command processor not available!\n");
			exit (1);
		}

		/*On Windows un-comment these lines
		and place comments on the Linux or Unix lines.*/
		printf ("Executing command DIR\n");
		i=system ("dir");

		/*On Linux or Unix un-comment these lines
		and place comments on the Windows lines.*/
		/*printf ("Executing command ls\n);
		i=system ("ls");*/

		printf ("Returned value is: %d.\n",i);
		return 0;
	}

This entry was posted in C Reference stdlib.h Functions. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. Tweet This! Tweet This! or use to share this post with others.

Leave a Reply: