我有这样一段代码:如何获得argv []作为int?
int main (int argc, char *argv[])
{
printf("%d\t",(int)argv[1]);
printf("%s\t",(int)argv[1]);
}
,并在外壳我这样做:
./test 7
,但第一个printf的结果是不是7,我怎样才能得到的argv []作为一个int?非常感谢
我有这样一段代码:如何获得argv []作为int?
int main (int argc, char *argv[])
{
printf("%d\t",(int)argv[1]);
printf("%s\t",(int)argv[1]);
}
,并在外壳我这样做:
./test 7
,但第一个printf的结果是不是7,我怎样才能得到的argv []作为一个int?非常感谢
argv[1]
是一个指向字符串的指针。
可以使用打印:printf("%s\n", argv[1]);
要想从你第一次把它转换为字符串的整数。使用strtol
将字符串转换为int
。
#include <errno.h> // for errno
#include <limits.h> // for INT_MAX
#include <stdlib.h> // for strtol
char *p;
int num;
errno = 0;
long conv = strtol(argv[1], &p, 10);
// Check for errors: e.g., the string does not represent an integer
// or the integer is larger than int
if (errno != 0 || *p != '\0' || conv > INT_MAX) {
// Put here the handling of the error, like exiting the program with
// an error message
} else {
// No error
num = conv;
printf("%d\n", num);
}
您可以使用strtol
为:
long x;
if (argc < 2)
/* handle error */
x = strtol(argv[1], NULL, 10);
或者,如果你使用的C99或更好的,你可以探索strtoimax
。
谢谢你的回答,这非常有用。 – Michael 2012-03-17 13:35:38
“string to long”(strtol
)函数是标准的。基本用法:
#include <stdlib.h>
int arg = strtol(argv[1], NULL, 10);
// string to long(string, endptr, base)
由于我们使用十进制系统中,碱为10。endptr
参数将被设置为“第一无效字符”,即第一非数字。如果您不在意,请将参数设置为NULL
而不是传递指针。如果你不希望非数字出现,您可以确保它被设置为“空终结者”(一\0
终止于C字符串):
#include <stdlib.h>
char* p;
int arg = strtol(argv[1], &p, 10);
if (*p != '\0') // an invalid character was found before the end of the string
随着man page提到,您可以使用errno
来检查没有发生错误(在这种情况下溢出或下溢)。
#include <stdlib.h>
#include <errno.h>
char* p;
errno = 0;
int arg = strtol(argv[1], &p, 10);
if (*p != '\0' || errno != 0) return 1;
// Everything went well
printf("%d", arg);
除此之外,您可以实现自定义检查:测试用户是否传递了参数;测试号码是否在允许的范围内;等等。
比接受的答案更好地解释。至少,像我这样的初学者。 – Dinei 2017-04-22 16:19:56
您可以使用功能int atoi (const char * str);
。如果需要的话int x = atoi(argv[1]);
以下信息:atoi - C++ Reference
/*
Input from command line using atoi, and strtol
*/
#include <stdio.h>//printf, scanf
#include <stdlib.h>//atoi, strtol
//strtol - converts a string to a long int
//atoi - converts string to an int
int main(int argc, char *argv[]){
char *p;//used in strtol
int i;//used in for loop
long int longN = strtol(argv[1],&p, 10);
printf("longN = %ld\n",longN);
//cast (int) to strtol
int N = (int) strtol(argv[1],&p, 10);
printf("N = %d\n",N);
int atoiN;
for(i = 0; i < argc; i++)
{
//set atoiN equal to the users number in the command line
//The C library function int atoi(const char *str) converts the string argument str to an integer (type int).
atoiN = atoi(argv[i]);
}
printf("atoiN = %d\n",atoiN);
//-----------------------------------------------------//
//Get string input from command line
char * charN;
for(i = 0; i < argc; i++)
{
charN = argv[i];
}
printf("charN = %s\n", charN);
}
希望这有助于
您需要包括#include <stdlib.h>
,并以这种方式使用的功能。祝你好运!
我知道cnicutar已经给出了答案,但它有助于理解正在发生的事情。你知道argv [1]究竟是什么吗?我的意思是,你能解释你为什么得到输出吗? – 2012-03-17 08:25:09
在我看来,argv [0]是命令本身,argv [1]是第一个参数I输入。在这种情况下是7. – Michael 2012-03-17 13:27:43
是的,没错,但是它是什么?什么样的?你可以用它做什么?为什么你在上面的例子中获得输出结果?你可以做它的增加或乘法,以及如果用户没有输入数字,但是'./test seven'或什么的? – 2012-03-17 13:57:12