Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a integer through command line in C?

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

}
like image 844
JVAN Avatar asked Apr 01 '17 19:04

JVAN


People also ask

How do you take integer inputs using command line arguments?

Example: Numeric Command-Line Argumentsint argument = Intege. parseInt(str); Here, the parseInt() method of the Integer class converts the string argument into an integer.

How do I pass a command line argument in CMD?

A command line argument is simply anything we enter after the executable name, which in the above example is notepad.exe. So for example, if we launched Notepad using the command C:\Windows\System32\notepad.exe /s, then /s would be the command line argument we used.

What is the passing argument through command line?

Command-line arguments are given after the name of the program in command-line shell of Operating Systems. To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments.

What is int argument in C?

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.


1 Answers

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.

like image 55
Jack Avatar answered Oct 22 '22 03:10

Jack