NaN happens in case of Inf/Inf, 0/0, Inf-Inf etc. One way to avoid this is to explicitly write a check, an if - else check maybe, wherever a similar situation could possibly arrive. Or you can use an assert statement, where you can stop execution if the condition asserted is false. In Matlab, it is achieved by:
dbstop if condition
Ok, I assume you are quite "OK" in C, which means you know to implement at least a basic sorting technique, lets say bubble sort. First of all, I appreciate you to have this desire of getting your C Programming skills to the next level. Let me tell you, it is quite rare in this simplified world of Python and JS. I also assume you have a good understanding of Linux.
Going by the literal meaning, a sieve separates out wanted elements from a sets of unwanted elements. Finding all prime numbers up to a certain limit has great applications in a lot of programming problems. The following is an efficient sieve for finding all prime numbers less than a given number n.