Global variable conflict.

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default Global variable conflict.

    I have two files like given below:

    /* File 1 */
    #include <stdio.h>
    #include <stdlib.h>

    void tf(void);

    int i;

    int main(void)
    {
    i = 10;
    tf();

    return EXIT_SUCCESS;
    }

    /* End File 1 */

    /* File 2 */
    #include <stdio.h>

    int i = 48;


    void tf(void)
    {
    printf("%d\n", i);
    }


    /* End File 2 */


    The observed behaviour is that the "printf" always prints the value of i as 10.

    Two questions:

    1 (for clc). Is the above code standards (ANSI) compliant?
    2 (for cup). How do I get the linker to "see" the i with value 48 on Solaris?

    Thanks and regards,
    Shinu
    Shinu George Guest

  2. Similar Questions and Discussions

    1. Mod_perl conflict with PHP on HTTP_REFERER variable
      Hi. I'm using Apache 1.3.33 / PHP 5.0.4 / Perl 5.8.7 / Mod_perl 1.29 on linux box. And one of virtualhost using Perl::Run and the others using...
    2. global variable
      I have made a site where I need to be able to include a file containing an array of settings in many pages. This array needs to have global scope...
    3. Global Variable declaration
      For some reason this wont compile, it says that i need to declare the variable before using it on prepareMovie global sm204
    4. Help Please: 'Warning 5001 global variable "iF_timer" already defined in global scope
      Hi there, I have just started to get this error: 'Warning 5001 global variable "iF_timer" already defined in global scope The variable name...
    5. mod_perl and the global variable?
      I have used mod_perl a little bit, I read the CGI porters guide, and it lead me to believe as long as I "use strict" in all my scripts, (declare...
  3. #2

    Default Re: Global variable conflict.

    In 'comp.lang.c', [email]supercool_libran@yahoo.co.uk[/email] (Shinu George) wrote:
    > I have two files like given below:
    >
    > /* File 1 */
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > void tf(void);
    >
    > int i;
    >
    > int main(void)
    > {
    > i = 10;
    > tf();
    >
    > return EXIT_SUCCESS;
    > }
    >
    > /* End File 1 */
    >
    > /* File 2 */
    > #include <stdio.h>
    >
    > int i = 48;
    You can't have two global scope variables with the same name in the same
    project. Your linker should warn you about that.
    > void tf(void)
    > {
    > printf("%d\n", i);
    > }
    >
    >
    > /* End File 2 */
    >
    >
    > The observed behaviour is that the "printf" always prints the value of i
    > as 10.
    Won't link at home.
    > Two questions:
    >
    > 1 (for clc). Is the above code standards (ANSI) compliant?
    No.

    If you insist to have these global scope variables, you must reduce their
    scope to the file with 'static'.

    --
    -ed- [email]emdelYOURBRA@noos.fr[/email] [remove YOURBRA before answering me]
    The C-language FAQ: [url]http://www.eskimo.com/~scs/C-faq/top.html[/url]
    <blank line>
    FAQ de f.c.l.c : [url]http://www.isty-info.uvsq.fr/~rumeau/fclc/[/url]
    Emmanuel Delahaye Guest

  4. #3

    Default Re: Global variable conflict.

    Shinu George wrote:

    [snip]
    > cat file2.h
    #ifndef GUARD_FILE2_H
    #define GUARD_FILE2_H 1
    #include <stdio.h>
    #include "file2.h"

    extern int i;

    void tf(void);

    #endif//GUARD_FILE2_H
    > cat file2.c
    /* File 2 */
    #include "file2.h"

    int i = 48;

    void tf(void) {
    printf("%d\n", i);
    }

    /* End File 2 */
    > cat file1.c
    /* File 1 */
    #include "file2.h"
    #include <stdlib.h>

    int main(int argc, char* argv[]) {
    tf();
    i = 10;
    tf();

    return EXIT_SUCCESS;
    }

    /* End File 1 */
    > gcc -Wall -std=c99 -pedantic -O2 -o file file1.c file2.c
    > ./file
    48
    10

    E. Robert Tisdale Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139