C: Simple for loop is not working

  • Thread starter mbrmbrg
  • Start date
  • #1
mbrmbrg
493
2
C: Simple "for" loop is not working

Homework Statement



I'm taking an online course (yay lynda!) in C/C++, and I am trying to replicate a course example that prints "Hello, World!" five times, using a simple "for" loop.

Homework Equations



for(int i = 1; i <= 5; ++i){...}

The Attempt at a Solution



// working.c by Bill Weinman <http://bw.org/> [Broken]
#include <stdio.h>

int main( int argc, char ** argv ) {
for(int i = 1; i <= 5; ++i) {
int x = printf("Hello, World!\n") * 5;
printf("printf returned %d\n", x);
printf("%d", i);
return 0;
}
}



My program runs, but only with 1 iteration of the loop. For reading ease, I've attached a screenshot taken in Eclipse.

4. Other Potentially Relevant Information

I previously have had trouble with a dummy program that required user input. It had been a long day, so I kind of closed my eyes and pretended it was going to be fine, but I never solved that problem, either.

5. Further Work

Uninstall and reinstall everything?? I'd really rather not, but it's all I've got right now.

6. Thank you!
 

Attachments

  • stalled loop.JPG
    stalled loop.JPG
    56.4 KB · Views: 348
Last edited by a moderator:

Answers and Replies

  • #2
Borek
Mentor
29,167
3,836


Code:
#include <stdio.h>

int main( int argc, char ** argv ) {
	for(int i = 1; i <= 5; ++i) {
		int x = printf("Hello, World!\n") * 5;
		printf("printf returned %d\n", x);
		printf("%d", i);
		return 0;
	}
}

Use [noparse]
Code:
[/noparse] tags to show your code.

Think about position of the return statement. What it does?
 
  • #3
mbrmbrg
493
2


Use [noparse]
Code:
[/noparse] tags to show your code.

Thanks, that's much cleaner!

Think about position of the return statement. What it does?

The return 0 statement sets the value of the main function (?) to zero. Does setting the whole main function=0 somehow mess with the value of my counter?
 
  • #4
Borek
Mentor
29,167
3,836


The return 0 statement sets the value of the main function (?) to zero. Does setting the whole main function=0 somehow mess with the value of my counter?

What does the "return" alone do?

As its name implies.
 
  • #5
36,655
8,655


The return 0 statement sets the value of the main function (?) to zero. Does setting the whole main function=0 somehow mess with the value of my counter?
A return statement in main returns control to whoever called main, which is usually the operating system.
 
  • #6
mbrmbrg
493
2


Return brings you back to what comes next. (I know that's convoluted, but it's the only way I can think of to express that return generally moves you along, but as its name implies, return can also bring you back to the beginning.)

But why would the program encounter the return line at all before the loop has run all of its iterations?
 
  • #7
Borek
Mentor
29,167
3,836


But why would the program encounter the return line at all before the loop has run all of its iterations?

Compare:

Code:
for (i = 0;i != 5;i++)
{
   printf("%s","Hello World\n");
   return;
}

and

Code:
for (i = 0;i != 5;i++)
{
   printf("%s","Hello World\n");
}
return;

Do you see the difference?
 
  • #8
mbrmbrg
493
2


Oh. My. Gosh. I put the line that ends my program INSIDE my for loop! No wonder it ends after 1 iteration!

Thank you!

(My apologies for exclamation point abuse; I get overly excited by understanding stuff.)
 
  • #9
Borek
Mentor
29,167
3,836


We all did such things at some point, welcome to the club :wink:
 
  • #10
mbrmbrg
493
2


:smile:
 

Suggested for: C: Simple for loop is not working

Replies
21
Views
1K
Replies
10
Views
626
  • Last Post
Replies
9
Views
497
  • Last Post
Replies
18
Views
560
  • Last Post
Replies
5
Views
499
  • Last Post
Replies
1
Views
488
Replies
6
Views
649
  • Last Post
Replies
17
Views
644
Replies
2
Views
581
  • Last Post
Replies
10
Views
553
Top