Thread: headrs

  1. #1
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127

    headrs

    hi all

    Code:
    #ifndef _HEADER_FILE_H
    #define _HEADER_FILE_H
    extern int x;
    #endif
    Code:
    header_file.c
    include "header_file.c"
    int x;
    main.c
    Code:
    #include<stdio.h>
    #include "header_file.h>
    
    int main()
    {
         x=55;
             printf("&#37;d\n",x);
    return 0;
    }
    now what is the question ?
    the question in the variable x ;
    it's declared in the header_file
    the defined in the header_file.c
    then used in the main.c
    but what if i redefined it in main.c also like that
    main.c
    Code:
    #include<stdio.h>
    #include "header_file.h>
    
    int main()
    {
         int x=55;
             printf("%d\n",x);
    return 0;
    }
    i tried and it work
    so my question is ?
    what has happen to the variable x in the header_file.c when i redefined the variable x in main .c again ?
    and
    i cannot allocate space like that

    Code:
    /*header*/
    #ifndef _SS_H
    #define _SS_H
    extern int comp;
    extern  char* all;
    #endif
    Code:
    /*header source*/
    #include "ss.h"
    #include<stdlib.h>
    
    int comp=14;
    char *all=malloc(100*sizeof(*all));
    error :ss.c:5: error: initializer element is not constant

    so my question is why i cannot allocate space like that?
    thanks
    Last edited by St0rM-MaN; 07-06-2007 at 04:13 AM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Variables local to whatever block you're dealing with, take precedence over variables declared in previous blocks. The basic concept of this is involved with what is called "scope".

  3. #3
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Variables local to whatever block you're dealing with
    that's what i got from the scope
    except the variable declared out side any function or prefixed with external in that case when you use it you have to define it
    thanks mac
    and my topic title should be headers not headrs

Popular pages Recent additions subscribe to a feed