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. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 2 responses to “C Reference function system()”

Why not let us know what you think by adding your own comment!

  1. Maruf on December 19th, 2012:

    Thank you sir, for helpful information in this site.

    Dear sir, i want to know one thing, that is if any system has not available command processor then how i get the command processor.

  2. Shashank on March 29th, 2014:

    Hello sir ,

    Please tell me how to open two files simultaneouly using system();

    I want to open two file
    and I am doing it like
    system(“C:\temp\file1.doc”);
    system(“C:\temp\file2.doc”);
    But here till the file 1 is open file 2 is not opening as the control is not able to reach the second system call , Is there a way to do open them simultaneouly.

    Thanks
    Shashank