0

I just started venturing into C coming from PHP. I'm having trouble with the printf() function when its called from within another function:

#include <stdio.h>

void printloop (int valx) {
    int x;
    for (x = valx; x < 5; x++) {
        printf("Value of x is: %d\n", x);
    }
}

int main() {
    printloop(5);
    return 0;
}

The program will compile and run but there is no output on screen.

7
  • 6
    It is quite natural because 5 < 5 is false. What is your problem? Commented Jun 9, 2016 at 12:49
  • There must be output be output on the screen. Commented Jun 9, 2016 at 12:49
  • Damn!!!! Silly me... Am completely out of my mind!!! Commented Jun 9, 2016 at 12:51
  • 1
    printloop(5) would make more sense logically. It's not the function call that is wrong it is the logic of the function. He's starting the index at the ending condition, instead of at the starting condition. Commented Jun 9, 2016 at 12:55
  • 1
    Voting to close this as simple typo/too localized, as I doubt this question will be of future interest to anyone, including the OP. Commented Jun 9, 2016 at 13:35

5 Answers 5

6

When you invoke the printloop function with 5 the for loop essentially becomes

for (x = 5; x < 5; x++) {
    //...
}

And x < 5 will never be true.


I guess what you meant was something like

for (x = 0; x < valx; x++) {
    //...
}
Sign up to request clarification or add additional context in comments.

Comments

5

What's wrong here is that your logic is saying 5 < 5, which is false. Your for loop is not executing because when your printloop function is invoked at printloop(5); it is passing the integer value of 5.

void printloop (int valx) {
int x;
for (x = valx; x < 5; x++) {
    printf("Value of x is: %d\n", x);
}

Your printloop function is receiving a value of 5, setting x inside of your function to x = 5.

When it is time for your for loop to execute you will have

void printloop (int valx) {
    int x = 5;
    for (5 = valx; 5 < 5; 5++) {
        printf("Value of x is: %d\n", x);
    }
}

the for loop will see 5 < 5, which is false, and thus the loop will not execute.

I think what you want to say is

#include <stdio.h>

void printloop (int valx) {
    int x;
    for (x = 0; x < valx; x++) {
        printf("Value of x is: %d\n", x);
    }
}

int main() {
    printloop(5);
    return 0;
}

Which will output:

Value of x is: 0
Value of x is: 1
Value of x is: 2
Value of x is: 3
Value of x is: 4

I hope this makes sense, and keep up the good work!

2 Comments

Yes it does. What I have asked is actually terrible!!!! Its such a basic mistake that I feel ashamed!! Am out of my mind..
This is the real output with for (x = 0; x <= valx; x++) {?? Maybe <?
1

In your main, your are calling printloop and giving it the value 5. This means that you're making valx = 5 and when you say x = valx inside of your loop, you're setting x equal to 5 as well. When your loop gets to its conditional, it will never run because because x is equal to 5, not less than it.

Comments

0
#include <stdio.h>

void printloop (int valx) {
int x;
    for (x = valx; x < 5; x++) {<--- **Your problem is here ( x = 5; x < 5; x++)
    printf("Value of x is: %d\n", x);
    }
}

int main() {
    printloop(5);
 return 0;
}

You are telling the loop while it is less than 5 (which it is not because of the value you gave to valx) thus it doesn't print anything because it doesn't go into the loop

Comments

0

It's because you passed 5 to the function printloop, and the for loop inside printloop will only run if the given argument is less than 5. You could either change that value or pass an integer smaller than 5.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.