times = atoi(argv[1])) < 1
:字符串转化为数字,本就大于0,小于一只能是0 或 负数,肯定错
字符串仅以整数开头,atio()函数
也能处理,它只把开头的整数转换为字符
整体的意思(atoi()函数具有局限性):
命令行参数3被储存为字符串3\0。atoi()函数把该字符串转换为整数值3,然后该值被赋给times。该值确定了执行for循环的次数
代码
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i, times;
if (argc < 2 || (times = atoi(argv[1])) < 1)
{
/*
argc < 2 :行号小于2,说明我们没有输入字符串,只有一个文件名
times = atoi(argv[1])) < 1 :字符串转化为数字,本就大于0,小于一只能是0 或 负数,肯定错
那么就输出文件名喽
*/
printf("Usage: %s positive-number\n", argv[0]);
}
else
{
for (i = 0; i < times; i++)
{
puts("Helo,good looking");
}
}
return 0;
}