variables when declared inside or outside a function

This is a discussion on variables when declared inside or outside a function within the C Programming forums, part of the General Programming Boards category; I read that if a variable/pointer is declared outside a function it will be assigned a value of zero/NULL by ...

  1. #1
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54

    variables when declared inside or outside a function

    I read that if a variable/pointer is declared outside a function it will be assigned a value of zero/NULL by default. I worte a code to test the same and found it is very much correct:

    Code:
    /* Values of un initialized variables and pointers when declared inside or outside a function */
    #include<stdio.h>
    
    int j;
    int *ptr;
    
    int main(void) {
    
     int k;
     int *ptr2;
    
     printf("j is declared outside main %d\n", j);
     printf("ptr is declared outside main %d\n", ptr);
     printf("k is declared inside main %d\n", k);
     printf("ptr2 is declared inside main %d\n", ptr2);
    
     return 0;
    }
    result i get is :
    Code:
    $ ./prog1 
    j is declared outside main 0
    ptr is declared outside main 0
    k is declared inside main -1208533004
    ptr2 is declared inside main -1208408144
    My question is what is the logic behind this behaviour? why won't variables that are declared inside a function also initialized to zero/NULL automatically?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, global variables are guaranteed to be 0. Variables in functions are "undefined"[1]. Both of these are defined in the C standard specification. All local variables MUST be initialized to something, or they will just contain "rubbish".

    I guess from the values that you are running on a Linux rather than a Windows platform - but I could be wrong.

    [1]This means that it can be ANY value, and usually very meaningless. It is usually the same value for the same application on the same platform with the same call sequence and the same input data - but it's definitely not guaranteed to be any particular value.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    Quote Originally Posted by matsp View Post
    Yes, global variables are guaranteed to be 0. Variables in functions are "undefined"[1]. Both of these are defined in the C standard specification. All local variables MUST be initialized to something, or they will just contain "rubbish".

    I guess from the values that you are running on a Linux rather than a Windows platform - but I could be wrong.

    [1]This means that it can be ANY value, and usually very meaningless. It is usually the same value for the same application on the same platform with the same call sequence and the same input data - but it's definitely not guaranteed to be any particular value.

    --
    Mats
    Thank you for explaining this Mats!

    PS: And yes you guess correctly that I am running this on a Linux platform.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    wow! How did you know he was running linux just from the values?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Abda92 View Post
    wow! How did you know he was running linux just from the values?
    I didn't KNOW, I was guessing. The values are just under 0xB8000000, which is near the stack base address in Linux. Windows stack, is around 0x200000, so quite a different value.

    Of course, I could have just cheated and seen that it said "$ ./someapp" in the "here's where I run the code" - but I actually didn't.

    Of course, it could have been any other system that puts the stack 32M from 3GB.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,012
    Quote Originally Posted by jas_atwal View Post
    Code:
     printf("ptr is declared outside main %d\n", ptr);
     printf("ptr2 is declared inside main %d\n", ptr2);
    This is bad, undefined behavior. Pointer values should be displayed with %p in printf. Alternatively, you can cast the pointers to int in your printf call. The code you have will work as expected as long as sizeof(int) == sizeof(int*), but if not it could lead to problems.

    The issue is that the type of the arguments passed is deduced at runtime by way of the format string. If the format string does not match the (promoted*) type of the arguments passed, then the behavior is undefined. If the expected and passed arguments at least match is size, then the behavior is predictable: the arguments will be reinterpreted -- but not cast -- as the expected type. So for example, printf("%lx",1.0); should print out the floating point representation of 1.0. If the passed arguements do not match in size, then the behavior is hard to guess. It may work, it may crash.

    *small types like char and short are promoted to int, float is promoted to double ext.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    Thank you so much for this great explanation King! Things are much clear now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 06-09-2008, 09:53 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

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