-2

I am a beginner is Computer Science and I recently started learning the language C. I was studying the for loop and in the book it was written that even if we replace the initialization;testing;incrementation statement of a for loop by any valid statement the compiler will not show any syntax error. So now I run the following program:

#include<stdio.h>

int main()
{
    int i;
    int j;
    for(i<4;j=5;j=0)
        printf("%d",i);
    return 0;
}

I have got the following output.

OUTPUT: 1616161616161616161616161616161616161616.........indefinitely

I understood why this is an indefinite loop but i am unable to understand why my PC is printing this specific output? Is there any way to understand in these above kind of programs what the system will provide us as output?

pmg
  • 106,608
  • 13
  • 126
  • 198
  • 1
    You haven't initialized `i`. What did you expect it to show? Also this will not even compile since there is no declaration of `j` in your code. – alex01011 May 23 '21 at 15:52
  • @alex01011 I really forgot to write the declaration of j here please check it now after the edit – It's probable May 23 '21 at 15:57
  • Point still stands, you haven't initialized it – lulle2007200 May 23 '21 at 15:59
  • Possible duplicate, [What will be the value of uninitialized variable?](https://stackoverflow.com/questions/11233602/what-will-be-the-value-of-uninitialized-variable). – alex01011 May 23 '21 at 16:00
  • 1
    I try to explain it in simple words. You use variables that have no values. In that case the values are undefined and have random values. Variables are labels that point to a specific location in memory. In your case, the unitialized variables point somewhere random in your memory and display the random value of that location. –  May 23 '21 at 16:19

3 Answers3

1

This is a case of undefined behaviour.

You have declared i so it has a memory address but haven't set the value so its value is just whatever was already in that memory address (in this case 16)

I'd guess if you ran the code multiple times (maybe with restarts between) the outputs would change.

Some more information on uninitialized variables and undefined behaviour: https://www.learncpp.com/cpp-tutorial/uninitialized-variables-and-undefined-behavior/

charlie scott
  • 136
  • 1
  • 9
0

Looks like i was never initialized, and happens to contain 16, which the for loop is printing continuously. Note that printf does not add a new line automatically. Probably want to read the manual on how to use for.

To better understand what is going on, you can add additional debugging output to see what other variables are set to (don't forget the \n newline!) but really problem is the for loop doesn't seem right. Unless there's something going on with j that you aren't showing us, it really doesn't belong there at all.

izzy
  • 769
  • 2
  • 12
  • 22
0

You are defining the variable i but never initializing it, instead, you are defining the value for j but never using it.

On a for loop, first you initialize the control variable, if you haven't already done it. Then you specify the condition you use to know if you should run another iteration or not. And last but not least, change the value of the control variable so you won't have infinite loops.

An example:

#include <stdio.h>

int main()
{
    // 1. we declare i as an integer starting from 1
    // 2. we will keep iterating as long as i is smaller than 10
    // 3. at the end of each iteration, we increment it's value by 1
    for (int i = 1; i < 10; i++) {
        printf("%d\n", i);
    }

    return 0;
}
Danillo Souza
  • 81
  • 1
  • 8