How to Pass Command Line Arguments to a C Program

How to pass command line arguments to a c program

In a Windows environment you just pass them on the command line like so:

myProgram.exe arg1 arg2 arg3

argv[1] contain arg1 etc

The main function would be the following:

int main (int argc, char *argv[])

Pass command line argument to a sub function

The common approach is to "decouple" the two; the functions further down the call tree really shouldn't care or know about main()'s arguments, i.e. the command argument vector itself.

Instead, it should be abstracted into application-specific options, which are passed from main(), which parses the options out of the command line arguments, down to all application-specific functions that need them.

command line argument to run a c program

Open up a terminal(command-line) and type "gcc nameOfFile.c argument1 argument2"
don't type the quotes though. Each argument you type will get passed in to your program and can be accessed by argv[0], argv[1], etc

Pass arguments into C program from command line

You could use getopt.

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

int
main (int argc, char **argv)
{
int bflag = 0;
int sflag = 0;
int index;
int c;

opterr = 0;

while ((c = getopt (argc, argv, "bs")) != -1)
switch (c)
{
case 'b':
bflag = 1;
break;
case 's':
sflag = 1;
break;
case '?':
if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 1;
default:
abort ();
}

printf ("bflag = %d, sflag = %d\n", bflag, sflag);

for (index = optind; index < argc; index++)
printf ("Non-option argument %s\n", argv[index]);
return 0;
}

Cant pass command line arguments in C programming from the terminal

You need to compile first then run. They are two separate steps.

cc -o countdown countdown.c
./countdown 4 display

The first command compiles the C code into a countdown binary. Assuming that succeeds, the second command runs the binary with the required arguments.

Passing a integer through command line in C?

The signature for the main function in C would be this:

int main(int argc, char *argv[]);

argc is the number of arguments passed to your program, including the program name its self.

argv is an array containing each argument as a string of characters.

So if you invoked your program like this:

./program 10

argc would be 2

argv[0] would be the string program

argv[1] would be the string 10

You could fix your code like this:

#include <stdio.h>
#include <stdlib.h>
#define PI 3.1416
int
main (int argc, char *argv[])

{
double r,area, circ;

char *a = argv[1];
int num = atoi(a);

printf("You have entered %d",num);

r= num/2;
area = PI * r * r;
circ= 2 * PI * r;

printf ("A circle with a diameter of %d ", num);
printf ("has an area of %5.3lf cm2\n", area);
printf ("and a circumference of %4.2lf cm.\n", circ);

return (0);

}

You probably also want to add line breaks into your print statements for readability.

How to pass command line arguments to C program using execlp

Arguments to programs are always strings.

int exec_arg_1, exec_arg_2;

if (pid == 0){
printf("Repeat Number: %d, Process Number: %d\n", exec_arg_1, exec_arg_2);
char arg1[20], arg2[20];
snprintf(arg1, sizeof(arg1), "%d", exec_arg_1);
snprintf(arg2, sizeof(arg2), "%d", exec_arg_2);
execlp( "/home/drlight/Desktop/asp/Assignment_3/philosopher.o",
"philosopher.o", arg_1, arg_2, NULL );
fprintf(stderr, "Exec didn't work...\n");
exit(1);
}

Note that execlp() is really only useful with a fixed number of arguments (or, a least, when there is a small fixed upper bound on the number of arguments). Most often, execvp() is a better choice.



Related Topics



Leave a reply



Submit