Basic C programming question
I've just started to learn C and it's going pretty slow...I wanted to write a program that takes in an integer argument and returns it's doubled value (aka take in integer, multiply by 2, and printf that value).
I purposely did not want to use the scanf function. Here's what I have so far and what is not compiling...
#include <stdio.h>
int main(int index)
{
if (!(index)) {
printf("No index given");
return 1;
}
a = index*2;
printf("Mult by 2 %d",a);
return 0;
}
So basically when the program is executed I want to supply the index integer. So, in cygwin, I would write something like ./a 10
and 10 would be stored into the index variable.
Also, I want to program to return "No index given" and exit if no index value was supplied...
Anyone care to help what I'm doing wrong?
EDIT:
This code returns 1 error upon compilation and is based on the help by @James:
#include <stdio.h>
int main(int 1, char index)
{
int index, a;
if (!(index)) {
printf("No index given");
return 1;
}
a = index*2;
printf("Mult by 2 %d",a);
return 0;
}
EDIT 2: Consider a simpler program where a value is just taken and echoed back (as shown below)
#include <stdio.h>
int main(int argc, char* argv[])
{
int index;
index = argv[1];
printf("Index is %d, ", index);
/*if (!(index)) {
printf("No index given");
return 1;
}
a = index*2;
printf("Mult by 2 %d",a);*/
return 0;
}
This program fails to compile...Any ideas?!? Ugh.
EDIT 3: This is the code that finally compiled and works. Thanks all!
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* a开发者_Go百科rgv[])
{
if (argc <= 1)
{
printf("No index given");
return 1;
}
int i;
i = atoi(argv[1]); // convert string in argv[1] to integer
int a;
a = i*2;
printf("Mult by 2: %d",a);
return 0;
}
Thanks! Amit
There are only two guaranteed-to-work prototypes for the main
function: one takes no arguments (int main(void)
), the other takes two arguments and looks like this:
int main(int argc, char* argv[])
argc
is the number of arguments passed to the program, and argv
is an array containing the arguments passed to the program.
If you want to pass an argument when you run the program (and not prompt the user for input), you will need to
- use the form of
main
taking arguments, - check to make sure
argc
is greater than one (argv[0]
is the program name, or should be), - convert
argv[1]
from its string representation to an integer, usingstrtol
,sscanf
, or some other library function (avoidatoi
: it provides no usable error reporting), - then use that integer.
As you probably know, main() is a "special" function, which expects either 0 or two arguments: int argc
, and char **argv
, where argc
is automagically assigned the number of arguments in the argument array argv
. So, whatever arguments you pass to main()
will be stored in argv, and that is from where you need to access your arguments.
This link should help.
The arguments you are passing into the program are text, and so main will receive them as strings. It splits the command line arguments by whitespace and passes them in as an array of strings, along with a number stating the number of parameters it is giving you. The program will always have at least one argument, the name of the file you ran the program as (which is in this case "a"
). This string is always found at argv[0].
As the other answers stated, the correct signature for main is int main(int argc, char* argv[])
. When you run ./a 10
, argc will be 2 and argv[1] will be the string "10"
. You will need to convert this string to an integer to multiply it by 2, using something like int i = atoi(argv[1]);
or int i; sscanf(argv[1], "%d", &i);
.
Here is a correction to your code using the proper prototype for a Main with command line arguments.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
if (argc <= 1)
{
printf("No index given");
return 1;
}
int i = atoi(argv[1]); // convert string in argv[1] to integer
int a = i*2;
printf("Mult by 2 %d",a);
return 0;
}
精彩评论