Results 1 to 4 of 4

Thread: If variable is not initialized in main function

  1. #1
    Join Date
    Jan 2009
    Posts
    30

    If variable is not initialized in main function

    I have read in lots of books that you should initialize every variable declared the program. I don't know what is the theory behind this. Can you let me know what happens when a variable is not initialized in the function.

    Can anyone explain me why this is with suitable example? so that is will be easy to understand for me
    Last edited by Jaisudha; 18-11-2009 at 02:24 PM.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: If variable is not initialized in main function

    See the below program, in which I have not initialized the value of variable 'r'.Please try to run this program and see what's happen


    main()
    {
    int r;
    printf(“%d”,r);
    p= sample()
    }

    sample()
    {
    printf(“Testing program”);
    }
    Output of above code:

    r=256

    Here I got output 256 for 'r', you may get another value because i have not initialized the value for 'r'. So When you will run this code you will get any garbage value.
    Last edited by Zecho; 18-11-2009 at 02:39 PM.

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: If variable is not initialized in main function

    If a variable is not declared in the main function of program, you are not able to use the variable in the program unless it is already defined as a global variable.

    Initialization process is required to assign value to the declared variable.
    if you do not initialize ,it will store a garbage value it.(Garbage value is the any value generated by computer itself)
    Last edited by Reegan; 18-11-2009 at 02:36 PM.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: If variable is not initialized in main function

    Hi ,

    It is very important to initialize each every variable in the program otherwise it will give you unexpected output.
    Please also note that initializing a variable is only done exactly once when the computer loads your program into memory for execution. Which means all initializations are done only before the program starts it's execution.

    I hope this information will definitely resolve your queries regarding variable initialization.
    Last edited by opaper; 18-11-2009 at 02:37 PM.

Similar Threads

  1. Call a function depending on the variable name
    By Captain Carrot in forum Software Development
    Replies: 5
    Last Post: 23-03-2010, 09:27 AM
  2. How to pass PHP variable value to jquery function?
    By Kasper in forum Software Development
    Replies: 5
    Last Post: 03-03-2010, 06:41 PM
  3. Accessing a variable outside a function without arg
    By BoanHed in forum Software Development
    Replies: 3
    Last Post: 04-12-2009, 01:35 PM
  4. Function with a variable number of arguments
    By Chrisch in forum Software Development
    Replies: 3
    Last Post: 20-11-2009, 02:03 PM
  5. How to Call a variable subroutine or function
    By Sayam in forum Software Development
    Replies: 2
    Last Post: 16-04-2009, 09:33 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,566,673.42520 seconds with 16 queries