variable availability

This is a discussion on variable availability within the C Programming forums, part of the General Programming Boards category; In the following program, why is integer variable i also available to prlt(int i)? Code: main() /* The main() function ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    10

    variable availability

    In the following program, why is integer variable i also available to prlt(int i)?

    Code:
    main()                    /* The main() function definition */
    {
      int i=5;                    /* Define an integer variable */
      prIt(i);                             /* Calls the prIt()
                                function and passes it i */
      return 0;                  /* Return to the operating system */
    }
    
    
    prIt(int i)            /* The prIt() function definition */
    {
       printf("%d \n", i);     /* Calls the printf() function */
       return 0;                             /* Returns to main() */
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,790
    Notice that the prIt function has a parameter named i. This parameter is not the same variable as the local variable i in the main function, but the value of i in the main function was passed as an argument to prIt.

    By the way, you should declare functions with a return type, and here both functions should have an int return type. Also, since you use printf(), you should #include <stdio.h>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    You are helpful, thank you!

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    This may be off the topic, but what is the difference between static local variables and global variables?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,790
    One obvious difference is the scope of the variables.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    I see. "Static simply means that the local variable's value is still there if the program calls the function again."
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 12:57 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 03:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21