Like Tree5Likes
  • 1 Post By manasij7479
  • 1 Post By quzah
  • 1 Post By Salem
  • 1 Post By grumpy
  • 1 Post By AndrewHunter

Scope of varibales in C?

This is a discussion on Scope of varibales in C? within the C Programming forums, part of the General Programming Boards category; Hello All, I came to face some issues regarding the scope of variables. Eg: String literal has a scope which ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Last edited by manasij7479; 09-07-2011 at 05:05 AM.
    Salem likes this.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Salem likes this.
    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
    32,831
    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
    sumit180288 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 manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    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.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    Scope and lifetime are completely different concepts. True, lifetime is often associated with scope, but that is not always true.
    sumit180288 likes this.
    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, Sunshine, 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,866
    The definitive answer can be found in or around section 6.2 of the standard. Here we have a link to that.
    sumit180288 likes this.
    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-04-2010, 12:38 AM
  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, 07: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, 01:11 AM

Tags for this Thread


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