16

I was wondering if someone could explain how passing arguments through command line works? I'm really confused by how it works. Right now I'm trying to pass one integer into the main program. How would I go about doing this?

EDIT: keep getting the initialization makes integer from pointer without a cast [-Wint-conversion] error?

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

{ 
  double r,area, circ;

  char a = argv[1];
  int num =  a - '0';

  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);

}
JVAN
  • 193
  • 1
  • 2
  • 11
  • Have you tried this or searched/read about how command line argument processing works in C? – pvg Apr 01 '17 at 19:16
  • http://stackoverflow.com/questions/4176326/arguments-to-main-in-c – Tony Tuttle Apr 01 '17 at 19:18
  • There are many tutorials about how to handle command-line arguments in C programs. The important thing you have to remember is that the`argv` array is an array of *strings*. And start by making a program that loops over the arguments and prints them (as strings). Once you do that it should all become pretty easy to understand. – Some programmer dude Apr 01 '17 at 19:20
  • Possible duplicate of [Pass arguments into C program from command line](http://stackoverflow.com/questions/498320/pass-arguments-into-c-program-from-command-line) – Paul Hankin Apr 01 '17 at 19:31
  • Related: https://stackoverflow.com/questions/7021725/how-to-convert-a-string-to-integer-in-c – Ciro Santilli OurBigBook.com Jun 15 '21 at 20:26

1 Answers1

22

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.

Jack Gore
  • 3,874
  • 1
  • 23
  • 32
  • so in order for me to use the string '10', I would have to convert it into an integer? – JVAN Apr 01 '17 at 19:24
  • @JVAN To use it as an int yes. – Jack Gore Apr 01 '17 at 19:28
  • 1
    @JVAN Yes. Each argument to C `main` is a C-style string. It's up to you to do integer conversions, or any other processing you may need. – DUman Apr 01 '17 at 19:28
  • @JackGore I have updated my code trying to convert char to int. However,I keep getting an error. Any help? – JVAN Apr 01 '17 at 20:22