ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The second statement in the first for loop is a conditional statement - it's just false since i = 0. If i was initialised with any other number, it would be true.
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.
#include <stdio.h>
int main()
{
int i;
for(i=1; i<=5; i++)
printf("\n %d",i);
return 0;
}
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.
Code:
for(int i=1; i<5; i++)
instead of
Code:
int 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
Distribution: Ubuntu, Debian, Various using VMWare
Posts: 2,088
Rep:
Exactly as the error says - this is not complient with the ANSI C99 standard. There are gcc flags that will turn this off, but it is not really a problem.
Older versions of C, as well as some other languages permit this shortcut.
It is generally considered better programming practice to declare the variables separately from the initializations.
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?
Output:
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.