I wouldn't think that fgets would be any faster than scanf for this problem. You're only reading one line at a time with fgets, and in this problem there are at most two numbers per line. For a faster input method you could look at using read or the other functions like it. The reason this will be faster is that it reduces the number of read operations.