Of course it's possible! :)
First of all, please note you're using signed int
for your variables, and max int on a 32 bit machine is 2,147,483,647 which is approx. 10^8 times smaller than the max number you're using, which is 999,999,999,999,999,999.
I'll recommend to change the max num to INT_MAX
(You'll need to include "limits.h")
Edit:
In addition, as said in the comments to my answer, consider changing to unsigned int
. you need only positive values, and the max number will be twice higher.
2nd Edit:
That being said, when final
will reach the closest it can to the limit in the condition, the next time its promoted, it'll exceed INT_MAX
and will result in an overflow. That means here, that the condition will never be met.
Better to just change the condition to the times you want the loop to run. Please note though, that any fibonacci number larger than the max numder that can be stored in your variable type, will result in an overflow.
Secondly, final
isn't initialized. Write final = 0
to avoid errors.
I recommend turning on all the warnings in your compiler. It could catch many errors before they compile :)
Also, I see no reason not to initialize the variables when you declare them. The value is already known.
Now for the speed of the program:
I'm not sure to which extent you're willing to change the code, but the simplest change without changing the original flow, is to make less calls to printf()
.
Since printf()
is a function that will wait for a system resource to become available, this is probably the most time consuming part in your code.
Maybe consider storing the output in a string, and lets say every 100 numbers print the string to the screen.
Try maybe to create a string, with a size of
(10 (num of chars in an int) + 1 (new line char) )* 100 (arbitrary, based on when you'll want to flush the data to the screen)
Consider using sprintf()
to write to a string in the inner loop, and strcat()
to append a string to another string.
Then, every 100 times, use printf()
to write to the screen.