Thread: Simple question on Counter Variables

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    28

    Simple question on Counter Variables

    I'm learning C from a fairly modern book so my questions will be beginner level for awhile yet...

    Anyway, let's say I have a program with several different functions, and many of them have loops in them. Let's say I use "i" as my loop counter for each one.

    Is it best to declare i before Main() ? This would make it a global variable wouldn't it, that means all of my functions could use "i" correct?

    Also when would be the best time to initialize "i"? If I want to make sure i = 0 should I do that in Main() first or in my other functions before the loops?

    If I have ++i in a function and then a function I call later on also uses "i" then will it have the same value it ended the loop with in the previous function? or will i = 0 or an unknown again?

    I just need to know if its necessary to have i = 0; in each of my functions that use "i" as a counter variable for loops.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There's no earthly reason for your functions to share counter variables. Just plain don't do it.

  3. #3
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Quote Originally Posted by tabstop View Post
    There's no earthly reason for your functions to share counter variables. Just plain don't do it.
    Exactly, global counters can lead to confusion and logic errors. Just have separate i's for your loops.
    Spidey out!

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    28

    ok, but...

    Would this be preferrable then? if i only have to declare "i" as an integer in main() once, then how is that any different from using it as a global variable? otherwise i would have to declare "i" as an integer in all 3 of my custome functions if i wanted to use them as a counter?

    Also, if the "i" counter in SomeFunction(); is let's say increminted to the value 12 for the loop, the function does whatever it's purpose and then the function ends and main() calls AnotherFunction(); does "i" still = 12 from when I increminted it in the previous function or does i = 0 or some unknown number at this point?

    Code:
    #include <stdio.h>
    
    void SomeFunction( void );
    void AnotherFunction( void );
    void YetAnotherFunction( void );
    
    int main( void ) {
    
    int i;
    i = 0;
    
    SomeFunction();
    AnotherFunction();
    YetAnotherFunction();
    
    return 0;
    
    }
    
    
    void SomeFunction( void ) {
    
    // do i need to do int i; here?
    // do i need to do i = 0; here?
    // some loop using "i" as the counter here
    
    }
    
    
    void AnotherFunction( void ) {
    
    // do i need to do int i; here?
    // do i need to do i = 0; here?
    // some loop using "i" as the counter here
    
    }
    
    
    void YetAnotherFunction( void ) {
    
    // do i need to do int i; here?
    // do i need to do i = 0; here?
    // some loop using "i" as the counter here
    
    }
    Last edited by lucidrave; 07-30-2009 at 02:59 PM. Reason: needed to clarify

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Yes, declare "i" in every function.
    Code:
    void SomeFunction(void) {
        int i;
        for(i = 0; i < something; i++)
            ...
    }
    >> does "i" still = 12 from when I increminted it in the previous function or does i = 0 or a unknown number now?
    "i" equals whatever you set it to in the function. Since "i" is declared in the function, its scope is limited to that function. In other words, once the function ends, that variable "i" no longer exists.
    Last edited by bithub; 07-30-2009 at 02:54 PM.

  6. #6
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Quote Originally Posted by lucidrave View Post
    would this be preferrable then? if i only have to declare "i" as an integer in main() once, then how is it any different from a global variable? otherwise i would have to declare "i" as an integer in all 3 of my functions listed below if it used them as a counter?

    Also, if the "i" counter in SomeFunction(); is let's say increminted to the value 12 before the loop and the function ends, and then main() calls AnotherFunction(); does "i" still = 12 from when I increminted it in the previous function or does i = 0 or a unknown number now?

    Code:
    #include <stdio.h>
    
    void SomeFunction( void );
    void AnotherFunction( void );
    void YetAnotherFunction( void );
    
    int main( void ) {
    
    int i;
    i = 0;
    
    SomeFunction();
    AnotherFunction();
    YetAnotherFunction();
    
    return 0;
    }
    
    void SomeFunction( void ) {
    
    // do i need to do int i; here?
    // do i need to do i = 0; here?
    // some loop using "i" as the counter here
    
    }
    
    
    void AnotherFunction( void ) {
    
    // do i need to do int i; here?
    // do i need to do i = 0; here?
    // some loop using "i" as the counter here
    
    }
    
    
    void YetAnotherFunction( void ) {
    
    // do i need to do int i; here?
    // do i need to do i = 0; here?
    // some loop using "i" as the counter here
    
    }
    Firstly, if you declare i in main it will only be valid in main hence it is not global, if you wanted to use it in the functions you'll have to pass it in. Although that is highly not recommended. Declaring counter variables right before you need them is the most common practice and the most clear one as well.

    Also, primitives are passed by value to functions so if you pass in i, it will still equal 0 when it returns from the function as the function only gets a copy of it.

    Code:
    void SomeFunction( void ) {
    
    int i;
    for(i =0; i < 10; ++i)
    {
    //..do something!
    }
    }
    Spidey out!

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you use C99, you could just make the counter local to the loop:
    for (int i = 0; i < n; i++)
    Due to the "evil" rule is C90 that variables must be declared at the beginning of the block, I can see where the confusion comes in. It is preferable that all loops use different counters, but in C99 you can actually redefine the same name several times because it disappears when the loop is finished. Unfortunately not so for C90.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    28
    Thanks for clearing that up guys, it makes more sense to me now.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes. Each i should be a separate, and local, variable.

    Global variables can be very useful, but they are regularly reviled because they lead to so many bugs and poor coding techniques.

    C doesn't use encapsulation as extensively as C++, but the idea here is to encapsulate each i, into it's own function. If there should be a problem with i (such as over reaching an array boundary), you don't want to have to look at any other i, anywhere else in the program, for the problem.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    C doesn't use encapsulation as extensively as C++, but the idea here is to encapsulate each i, into it's own function. If there should be a problem with i (such as over reaching an array boundary), you don't want to have to look at any other i, anywhere else in the program, for the problem.
    This is not encapsulation, btw.
    Encapsulation is about protecting code from changes. If you change something and everything breaks, you got poor encapsulation.
    You might think of it more like... sandboxing. If i is attacked in function X, function Y is not affected.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question about the C programming tutorial #1
    By elsheepo in forum C Programming
    Replies: 13
    Last Post: 01-19-2009, 08:59 PM
  2. simple question about variables and loops
    By InvariantLoop in forum C Programming
    Replies: 2
    Last Post: 01-26-2005, 09:47 AM
  3. Data Storage Question, and Dynamic variables?
    By Zeusbwr in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2004, 11:01 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. simple question
    By pancho in forum C Programming
    Replies: 3
    Last Post: 04-09-2002, 03:26 PM