Thread: Scope of varibales in C?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    India
    Posts
    27

    Question Scope of varibales in C?

    Hello All,

    I came to face some issues regarding the scope of variables.

    Eg: String literal has a scope which is lifetime (entire program).

    Where can i find such rules which are not commonly written in every book.
    Plz give me a link or some document stating such all the scope rules clearly.


    Regards

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Last edited by manasij7479; 09-07-2011 at 04:05 AM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hope is the first step on the road to disappointment.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Like so
    Code:
    int global;  // lifetime of program, scope everywhere
    static int local;  // lifetime of program, scope is current source file
    void foo ( ) {
      static int bar;  // lifetime of program, scope is  current function
      int count;  // lifetime of function, scope is  current function
      for ( count = 0 ; count < 10 ; count++ ) {
        int temp; // scope is up to the matching }
      }
    }
    For string literals, like
    char *s = "this is a string";
    the only thing you can be sure of is that "this is a string" has a lifetime of the program. If two source files have the same string constant, then one of them could be removed.

    All other initialisation data could be stored in a variety of unspecified ways.
    For example
    Code:
    char msg[] = "hello";
    May be implemented as
    Code:
    char msg[6];
    strcpy(msg,"hello");
    or
    Code:
    char msg[6]
    msg[0] = 'h';
    msg[1] = 'e';
    // and so on
    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.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Eg: String literal has a scope which is lifetime (entire program).
    Another point you might have misunderstood.
    The following code is perfectly invalid.
    Code:
    #include<stdio.h>
    void foo(void);
    int main(void)
    {
            printf("%s",s);
            return 0;
    }
    
    void foo(void)
    {
            char *s ="xyz";
    }
    You can only use a literal away from its local scope if you manage to get hold of its address.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Scope and lifetime are completely different concepts. True, lifetime is often associated with scope, but that is not always true.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    The definitive answer can be found in or around section 6.2 of the standard. Here we have a link to that.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Location
    India
    Posts
    27
    @AndrewHunter
    Very nice doc shared. Thank you for this...
    Quote Originally Posted by AndrewHunter View Post
    The definitive answer can be found in or around section 6.2 of the standard. Here we have a link to that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static varibales simple qns
    By s.sidak in forum C Programming
    Replies: 3
    Last Post: 11-03-2010, 11:38 PM
  2. global varibales being owerwritten
    By B.S.F. in forum C Programming
    Replies: 4
    Last Post: 12-06-2006, 01:09 PM
  3. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM
  4. Scope
    By Extol in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 02:14 PM
  5. Scope
    By Witch_King in forum C Programming
    Replies: 6
    Last Post: 09-06-2001, 12:11 AM

Tags for this Thread