Running a command within another program. How to do this with C.

This code snippet will run the date command. The execl() function is very useful for executing a command within your C program.You replace the NULL identifiers with any extra arguments to pass to the program. I prefer this over the system() function.

#include 

int main(void) {

	execl("/bin/date", "%c", NULL, NULL, NULL);
	return 0;
}

The system() function would be used this way. It is a little simpler to use.

#include <stdio.h>

int main (void)
{
	system("date +%s");
}

The man execl command on Linux will bring up some more information about using the various related functions for executing another program within your C program.

No comments have been made. Use this form to start the conversation :)

Leave a Reply