Question

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

I don't know why am I getting it out of order in even lines.

i.e in 103 70 105 150 I need all numbers separated in even line.

Was it helpful?

Solution

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," ")));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top