Home > OOP344 > Passing arguments to a C program from terminal

Passing arguments to a C program from terminal

Have you ever wondered how some UNIX commands take arguments? It can be done very easily with C.

You can code the rm, cp, ect commands in no time. Here is a sample code which you can use to understand passing arguments.

int main(int argc, char* argv[]){
  int i;
  for(i = 0;i<argc; i++){
    printf("%d: %s\n", i+1, argv[i]);
  }
  return 0;
}

argc is the number of arguments being passed in, and argv is a char pointer array which has the arguments.

Categories: OOP344
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: