Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to create a program in C language that would allow me to run a command in the terminal.

I have made a program in shell script which would give me the ip of any website that is opened in my browser. This shell script is executed by entering this command in the terminal:

sudo tcpdump -n dst port 80 -i eth

My professor told me to create a program in C language which would open the terminal and enter this command and then my shell script would work.

Please tell me how to create such a program.

share|improve this question

2 Answers 2

you could use the system() function available in stdlib.h to run commands.


system() executes a command specified in string by calling /bin/sh -c string, and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.

You could read more about it over here http://linux.about.com/library/cmd/blcmdl3_system.htm

share|improve this answer
This is certainly one way, but you probably don't want to run commands from a c program, instead, most Linux programs have a lib that can just the programs' code directly. For example ssh can use libssh. There's usually a VERY limited reason to run system commands from code. If your going it a lot your almost certainly doing something wrong. –  coteyr Dec 9 '12 at 16:28
I made a program in C like this –  Tehseen Malik Dec 10 '12 at 4:04

Hello i will write for you an example code, explain it to you and really hope this helps you. the function's prototype is something like:

int system(const char* cmd);

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

#define MAX_CMN_LEN 100

int main(int argc, char *argv[])
    char cmd[MAX_CMN_LEN] = "", **p;

    if (argc < 2) /*no command specified*/
        fprintf(stderr, "Usage: ./program_name terminal_command ...");
        strcat(cmd, argv[1]);
        for (p = &argv[2]; *p; p++)
            strcat(cmd, " ");
            strcat(cmd, *p);

    return 0;

1). open up a terminal and compile the program

2). run it (for example in Ubuntu) ./program_name comman_name -anything - anything

example: ./a.out locale -a

this example prints all locales supported by my compiler which is gcc.

more info:

p is a poniter to pointer to char (like argv is) p = &argv[2], points to -anything string i cat all -anythings to my cmd string i quit the loop when *p points to NULL look at this: -> i will use this symbol to say points to (dont confuse it with right arrow selection operator).

argv[0] -> program_name

argv[1] -> command_name (in this example command name will be locale, but enter the command you want to check instead)

argv[2] -> -anything (in this example -a, which is all locales)

argv[3] -> NULL (in this example, this quits the loop)

ok thats it, i think.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.