简体   繁体   English

strtok()函数在C中无法正常工作

[英]strtok() function not working properly in C

I have applied strtok() in a loop in C language using this code: 我使用以下代码在C语言循环中应用了strtok()

printf("%s",line);
printf("%d %d %d\n",atoi(strtok(line," ")),atoi(strtok(NULL," ")),atoi(strtok(NULL," ")) );

The output is: 输出为:

103 70 105 150

103 0 0

115 17 127 21

115 127 17

10 108 105 97

10 105 8

13 122 43 8

13 43 122

50 187 35 71

50 35 187

Odd line represents line and even lines after using strtok() . 奇数行表示使用strtok()之后的行和偶数行。

I don't know why I am not getting each number separated 我不知道为什么我不把每个数字分开

ie in 103 70 105 150 I need all numbers separated in even line. 即在103 70 105 150我需要将所有数字都以偶数行分隔。

In your case, the order of evaluation of parameters of printf is reverse than you think. 在您的情况下,printf参数的评估顺序与您想象的相反。 In fact, the order of evaluation of parameters is not strictly defined in C, so you shall rearrange your code to something like: 实际上,参数的求值顺序不是在C语言中严格定义的,因此您应将代码重新排列为:

printf("%d ",atoi(strtok(line," ")));
printf("%d ",atoi(strtok(NULL," ")));
printf("%d\n",atoi(strtok(NULL," ")));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM