[英]Why am I getting random numbers when I try to increase the ascii value of a char?
int n = argv[i][j];
n = n + (int)argv[1];
/* I'm pretty sure the above part that is wrong. What I was hoping that this
would do is take the number the person put in and increase n by that number but
it isnt working*/
printf("%d\n", n);
What I want is to make it so when someone types ./123 12 hi
I want it to increase the ascii characters of h
and i
by 12 or whatever number they put in. When I test my code out with 我想要做的是当有人键入./123 12 hi
我希望它使h
和i
的ASCII字符增加12或他们输入的任何数字。当我用
./123 1 hi
I get an output of 我得到的输出
-1081510229
-1081510228
instead of 代替
105
106
which is i
and j
, the next letters of h
and i
是i
和j
,是h
和i
的下一个字母
Libraries I'm using 我正在使用的图书馆
stdio.h
studio50.h
string.h
argv[1]
is a string (ie, a null-terminated char array), casting it to int
doesn't give the result you expected argv[1]
是一个字符串(即,以null终止的char数组),将其强制转换为int
不会得到您期望的结果
You should use atoi
to do the job. 您应该使用atoi
来完成这项工作。 Or better, use strtol
to get better stability. 或者更好,使用strtol
获得更好的稳定性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.