So guys, it was mainly some error with the C buffer. This was the first time I encountered this, and the solution to this was to write fflush(stdout)
after the printf() statements. Flushing out the buffer works.
Note: The program might still need several runs and might completely execute only once in three attempts.