简体   繁体   中英

strtok() function not working properly in C

I have applied strtok() in a loop in C language using this code:

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() .

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.

In your case, the order of evaluation of parameters of printf is reverse than you think. In fact, the order of evaluation of parameters is not strictly defined in C, so you shall rearrange your code to something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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