Luka
Luka

Reputation: 207

How do I read multiple times from same line in stdin?

I want to read multiple times from the same line in C, for example, my input would be

1 2 3

and I want to read it like

scanf("%d" , a);
scanf("%d%d" , b, c);

If I do it like this I get a runtime error, in pascal I just use read instead of readln, here it goes to the second row automatically, and in second row there isn't anything, that's why I get a runtime error I guess...

Upvotes: 0

Views: 720

Answers (2)

jfly
jfly

Reputation: 7990

It should be scanf("%d" , &a); scanf("%d%d" , &b, &c);, and scanf() just stops at the newline or other whitespace character in the buffer, it didn't go to the second row automatically.

Upvotes: 1

MrSykkox
MrSykkox

Reputation: 528

Sorry not for posting this as a comment but i aint allowed to do so!

Why wouldn't you just store it in a variable and then you can do your parsing? No need to call scanf twice for handling data from the same line.

Upvotes: 0

Related Questions