strtol未按预期运行,c
发生了什么事。。。strtol似乎出现溢出,但没有设置errno您很可能没有包含所需的strtol未按预期运行,c,c,string,int,strtol,C,String,Int,Strtol,发生了什么事。。。strtol似乎出现溢出,但没有设置errno您很可能没有包含所需的和/或标题 在包含以下内容后,您的代码工作正常(GCC处于64位模式): long max = 9223372036854775807 input = 12345678901 output = -539222987 direct call = 3755744309 $cat t.c #包括 #包括 #包括 #包括 内部主(空) { 长产量; 字符输入[]=“12345678901”; errno=0; 输出=
和/或
标题
在包含以下内容后,您的代码工作正常(GCC处于64位模式):
long max = 9223372036854775807
input = 12345678901
output = -539222987
direct call = 3755744309
$cat t.c
#包括
#包括
#包括
#包括
内部主(空)
{
长产量;
字符输入[]=“12345678901”;
errno=0;
输出=strtol(输入,空,10);
printf(“最大长度=%ld\n”,最大长度);
printf(“输入=%s\n”,输入);
printf(“输出=%ld\n”,输出);
printf(“直接调用=%ld\n”,strtol(输入,NULL,10));
如果(errno | | output>=INT|u MAX | | output您很可能没有包含所需的
和/或
标题
在包含以下内容后,您的代码工作正常(GCC处于64位模式):
long max = 9223372036854775807
input = 12345678901
output = -539222987
direct call = 3755744309
$cat t.c
#包括
#包括
#包括
#包括
内部主(空)
{
长产量;
字符输入[]=“12345678901”;
errno=0;
输出=strtol(输入,空,10);
printf(“最大长度=%ld\n”,最大长度);
printf(“输入=%s\n”,输入);
printf(“输出=%ld\n”,输出);
printf(“直接调用=%ld\n”,strtol(输入,NULL,10));
如果(errno | | output>=INT|MAX | | output谢谢,这就解决了它!我有stdio,但没有stdlib,我不知道这里需要它。谢谢你指出了errno问题。@user1019856,你的编译器很可能会告诉你,如果你编译代码时在,-Wall
上有所有警告。谢谢,这就解决了!我有stdio,但没有st!)dlib,不知道这里需要它。谢谢你指出错误。没有问题。@user1019856,你的编译器很可能会告诉你,如果你编译代码时,所有警告都在,-Wall
左右。
$ cat t.c
#include<limits.h>
#include<errno.h>
#include<stdlib.h>
#include<stdio.h>
int main (void)
{
long output;
char input[] = "12345678901";
errno = 0;
output = strtol(input,NULL,10);
printf("long max = %ld\n",LONG_MAX);
printf("input = %s\n",input);
printf("output = %ld\n",output);
printf("direct call = %ld\n",strtol(input,NULL,10));
if(errno || output >= INT_MAX || output <= INT_MIN) {
printf("Input was out of range of int, INT_MIN = %d, INT_MAX = %d\n",INT_MIN,INT_MAX);
printf("Please input an integer within the allowed range:\n");
}
return 0;
}
$ gcc -Wall -Wextra -pedantic t.c
$ ./a.out
long max = 9223372036854775807
input = 12345678901
output = 12345678901
direct call = 12345678901
Input was out of range of int, INT_MIN = -2147483648, INT_MAX = 2147483647
Please input an integer within the allowed range: