将字符串转换为int(但仅当确实是int时) [英] Convert a string to int (But only if really is an int)
本文介绍了将字符串转换为int(但仅当确实是int时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在大学里,我被问到程序是否检测到从命令行参数输入的字符串是否为整数(./Program 3.7
).现在,我想知道如何检测到这一点.因此atoi会检测到例如a
的输入无效,但是例如3.6
的输入应该是无效的,但是atoi会将其转换为整数.
In college I was asked if our program detects if the string enter from command line arguments is a integer which it did not(./Program 3.7
). Now I am wondering how I can detect this. So input as for example a
is invalid which atoi detects, but input like for example 3.6
should be invalid but atoi will convert this to an integer.
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc > 1) {
int number = atoi(argv[1]);
printf("okay\n");
}
}
但是,仅当argv [1]确实是整数时,才应打印偏离路线的行列.希望我的问题清楚.非常感谢.
But offcourse okay should only be printed if argv[1] is really an integer. Hope my question is clear. Many thanks.
推荐答案
查看全文