ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
You are correct, I put i = 1 and got an infinite loop. May I know from where did you learn this? I never learned that you could substitute true with any number but zero and false with 0 in a C program.
for(i=1; i<=5; i++)
EDIT: don't put ";" after a for statement, as everything that follows after that for doesn't get included in the for statement. It also works how you mentioned if you remove that ";", but it would print out 2 3 4 5 6. So I would reccomend initialising i with 0.
Last edited by Valkyrie_of_valhalla; 06-09-2007 at 03:06 AM.
The for loop works by initialising the counter, checking for the validity of the second statement, executing the body of the loop, and then executing whatever is there after the second semi colon. Since the only body of the for loop in the original program (second one) was the semi colon, the value of the counter never got incremented and thus it broke into an infinite loop, continuously executing the statement after the second semi colon - the printf statement which gave the unincremented value of i i.e 1 continuously.
Thank you all for your help.
One more question: Why can't I declare the for loop variable in the for loop itself i.e.
for(int i=1; i<5; i++)
for(i=1; i<5; i++)
I get this error with gcc:
test.c: In function ‘main’:
test.c:6: error: ‘for’ loop initial declaration used outside C99 mode
Yes, it's called 'e' as in the 'e' of natural logarithms if you sum the terms till infinity.
Could you explain jschiwal? How's the error minimised if I add the series backwards?
The final result here is correct uptil 3 decimal places.
Each term is smaller than the last one. Your series is so short that you may not notice much of a problem, but for some series, the lost of significant digits from adding to 1.0 instead of a bunch of terms like 1.2234567e-13 being totaled can effect the final result.
For the smallest terms in a longer series, you will be adding 0 to the total.
By the way, you might want to look at the manpage of "bc" for the library function they use for e(x).
Try this on for size!
echo 'scale=1024; print e(1) ' | bc -l