Like Tree1Likes
  • 1 Post By Salem

Global Variables?

This is a discussion on Global Variables? within the C Programming forums, part of the General Programming Boards category; As a general rule, why is it a good idea to avoid the excessive use of global variables. Please answer ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    15

    Global Variables?

    As a general rule, why is it a good idea to avoid the excessive use of global variables. Please answer in detail, better if in points.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,501
    I can say from experience, debugging a lot of global variables is tricky.

    In a project I was involved with, there were two global variables with similar names (two sub-states from the same struct). Because the state needs to be visible to all parts of the code (so one section didn't go into a state that shouldn't run when the other is in some state), use of local variables was not a preferred option.

    A state from one variable was accidentally given to the other. Because the modification was made in another place of the program and because the names looked similar, it was very hard to find.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    I think this sums it up pretty well: Global Variables Are Bad.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    > Please answer in detail, better if in points.
    So you can copy/paste directly into your homework answer?
    rags_to_riches likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    32
    Quote Originally Posted by Hasnat Abul View Post
    As a general rule, why is it a good idea to avoid the excessive use of global variables. Please answer in detail, better if in points.
    You can find it in K&R

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    In case to avoid the probability of what salem said i will say only this:
    Imagine a large program which uses global variables.It is extremely possible that manu variables are going to conflict with others, in case of same names...So you have to resolve which one is shadowing the others etc.. This does not only make it difficult from you, but extremely unreadable for the others.

    For more,see what the above post says

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static global & global variables
    By aqeel in forum C Programming
    Replies: 1
    Last Post: 09-25-2009, 01:32 PM
  2. Global Variables
    By samuelmoneill in forum C Programming
    Replies: 4
    Last Post: 04-15-2009, 07:27 AM
  3. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 04:25 AM
  4. Global Variables
    By Sn0mAN in forum C Programming
    Replies: 3
    Last Post: 08-08-2007, 01:54 PM
  5. C global variables
    By Dohojar in forum C Programming
    Replies: 18
    Last Post: 02-25-2002, 12:00 PM

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