Thread: Can some one help me understand the logic of this beginner's program

  1. #1
    Registered User
    Join Date
    Jul 2016
    Location
    Pune, Maharashtra, India
    Posts
    6

    Exclamation Can some one help me understand the logic of this beginner's program

    The code:
    Code:
    #include<stdio.h>
    
    int main()
    {
    
        int j ;
        while ( j <= 10 )
        {
            printf ( "\n%d", j ) ;
            j = j + 1 ;
        }
    
        return 0;
    }
    Output:

    2
    3
    4
    5
    6
    7
    8
    9
    10

    I am using Code Blocks and what I do not understand is that while in the program no particular value has been assigned to integer j, why does the output start with 2, as if the value of j is 2 in the beginning?

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Since j is a local variable it can take any value. Either you have to initialize it with 0 or make it a global variable.

  3. #3
    Registered User
    Join Date
    Jul 2016
    Location
    Pune, Maharashtra, India
    Posts
    6
    Hi Satya,

    Thank you for taking time to reply to my query. So can I say, that without declaring j as a global variable or by not assigning a value to J in the beginning - the outcome of this program will not be reliable, and that different outcome can arise with different compilers?

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Yes.

  5. #5
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    j is an uninitialized local (non-global) variable, and therefore this is really an invalid program and its output is undefined. It could crash your computer, or print 2. Why C allows uninitialized local variables is questionable (for example, Java does not allow an uninitialized local variable before it is used.. however, programmers often resort to initializing to null, which doesn't help much, when you get a NullPointerException). The solution is up to the programmer to always initialize his or her variables to sane default value

  6. #6
    Registered User
    Join Date
    Jul 2016
    Location
    Pune, Maharashtra, India
    Posts
    6
    Quote Originally Posted by MacNilly View Post
    j is an uninitialized local (non-global) variable, and therefore this is really an invalid program and its output is undefined. It could crash your computer, or print 2. Why C allows uninitialized local variables is questionable (for example, Java does not allow an uninitialized local variable before it is used.. however, programmers often resort to initializing to null, which doesn't help much, when you get a NullPointerException). The solution is up to the programmer to always initialize his or her variables to sane default value
    Thanks for the answer Macnilly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-12-2014, 04:40 PM
  2. What gets printed? Trying to understand the logic.
    By Joie Moie in forum C Programming
    Replies: 1
    Last Post: 05-02-2013, 07:30 PM
  3. Coding why wrong/logic cant understand
    By raihan004 in forum C Programming
    Replies: 6
    Last Post: 10-02-2012, 04:16 AM
  4. Recursive function, trying to understand the logic.
    By csharp100 in forum C Programming
    Replies: 7
    Last Post: 11-16-2010, 11:38 AM
  5. Replies: 21
    Last Post: 10-14-2006, 04:38 AM

Tags for this Thread