Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. using extern in C

using extern in C

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
12 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sakhalean
    wrote on last edited by
    #1

    Hi All, I declared a variable int x = 0; in Main.cpp In the remaining .cpp files I am using extern int x; Now I want to change the contents of x to any other value in only Main.cpp but not in the other .cpp files How to do this?

    J L S _ 4 Replies Last reply
    0
    • S Sakhalean

      Hi All, I declared a variable int x = 0; in Main.cpp In the remaining .cpp files I am using extern int x; Now I want to change the contents of x to any other value in only Main.cpp but not in the other .cpp files How to do this?

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      When changing a global variable, all modules accessing it, will get the changed value. This is by design. To have a variable for Main.cpp only, you have to add another one. A note: You are asking for C, but using C++ file extensions.

      S 1 Reply Last reply
      0
      • J Jochen Arndt

        When changing a global variable, all modules accessing it, will get the changed value. This is by design. To have a variable for Main.cpp only, you have to add another one. A note: You are asking for C, but using C++ file extensions.

        S Offline
        S Offline
        Sakhalean
        wrote on last edited by
        #3

        Hi, Iam asking in C only not cpp.

        J 1 Reply Last reply
        0
        • S Sakhalean

          Hi, Iam asking in C only not cpp.

          J Offline
          J Offline
          Jochen Arndt
          wrote on last edited by
          #4

          Then you should rename your files to use .c extensions. If you use a compiler that supports C and C++, it will use C++ when detecting a .cpp extension and C compilation is not specified explicitely. But the answer about the global variable is still the same.

          1 Reply Last reply
          0
          • S Sakhalean

            Hi All, I declared a variable int x = 0; in Main.cpp In the remaining .cpp files I am using extern int x; Now I want to change the contents of x to any other value in only Main.cpp but not in the other .cpp files How to do this?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Sakhalean wrote:

            How to do this?

            You cant, period. You only have one variable called x. Chanhe its value and it changes, regrdless of where it is used.

            ============================== Nothing to say.

            1 Reply Last reply
            0
            • S Sakhalean

              Hi All, I declared a variable int x = 0; in Main.cpp In the remaining .cpp files I am using extern int x; Now I want to change the contents of x to any other value in only Main.cpp but not in the other .cpp files How to do this?

              S Offline
              S Offline
              Stefan_Lang
              wrote on last edited by
              #6

              Your question shows that you don't understand scope. When you declare a variable using extern, it has global scope. Global scope means that all parts of your application will access the same variable when using that name, and there's only one variable and one value. When you declare a variable in your .c file, but not elsewhere, this variable has local scope. Local scope means you can use it in this compilation unit only, and only from the point where you declare it, not before. It also means that when you use the same name in another .c file you get a compiler error, because the variable is not known there. You can declare another variable by the same name in another .c file. If you do that, that variable will be independent of the first one - you will have two different variables storing two different values. If you want a variable that does not affect other parts of your application when you change it, you need a local variable On a sidenote, it is a good practice to avoid variables with global scope. Prefer local variables over global ones wherever you can. Instead of using global variables, pass every value that a function requires as a fucntion argument, even if the only thing the function does with it is pass it on to another function.

              L 1 Reply Last reply
              0
              • S Stefan_Lang

                Your question shows that you don't understand scope. When you declare a variable using extern, it has global scope. Global scope means that all parts of your application will access the same variable when using that name, and there's only one variable and one value. When you declare a variable in your .c file, but not elsewhere, this variable has local scope. Local scope means you can use it in this compilation unit only, and only from the point where you declare it, not before. It also means that when you use the same name in another .c file you get a compiler error, because the variable is not known there. You can declare another variable by the same name in another .c file. If you do that, that variable will be independent of the first one - you will have two different variables storing two different values. If you want a variable that does not affect other parts of your application when you change it, you need a local variable On a sidenote, it is a good practice to avoid variables with global scope. Prefer local variables over global ones wherever you can. Instead of using global variables, pass every value that a function requires as a fucntion argument, even if the only thing the function does with it is pass it on to another function.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Well, to be precise, declaring it outside of any code gives the variable global scope, using 'extern' just allows that symbol to be imported into the other source code file at compile time.

                Stefan_Lang wrote:

                On a sidenote

                Verging on dogma. A solution demands certain things, and with C, you use alot of globals. OK, you can get into a mess, and if that is the case the coder whould wonder about his ability, rather than what features of the language to use. :)

                ============================== Nothing to say.

                S 1 Reply Last reply
                0
                • S Sakhalean

                  Hi All, I declared a variable int x = 0; in Main.cpp In the remaining .cpp files I am using extern int x; Now I want to change the contents of x to any other value in only Main.cpp but not in the other .cpp files How to do this?

                  _ Offline
                  _ Offline
                  _Superman_
                  wrote on last edited by
                  #8

                  I assume what you're trying to do is to create a global variable in Main.cpp and make it read only in other .cpp files? If so, here is what you can do. Create the global variable as static in Main.cpp - static int x = 0; Now create a function in Main.cpp that returns this value - int GetX() { return x; } Now you can call this function from other .cpp files to get the value of x and also x can only be modified from Main.cpp. Here you do not need to use the extern keyword at all. Also to call GetX in other .cpp files, you will need to declare it there - int GetX();

                  «_Superman_»  _I love work. It gives me something to do between weekends.

                  _Microsoft MVP (Visual C++)

                  Polymorphism in C

                  P M 2 Replies Last reply
                  0
                  • _ _Superman_

                    I assume what you're trying to do is to create a global variable in Main.cpp and make it read only in other .cpp files? If so, here is what you can do. Create the global variable as static in Main.cpp - static int x = 0; Now create a function in Main.cpp that returns this value - int GetX() { return x; } Now you can call this function from other .cpp files to get the value of x and also x can only be modified from Main.cpp. Here you do not need to use the extern keyword at all. Also to call GetX in other .cpp files, you will need to declare it there - int GetX();

                    «_Superman_»  _I love work. It gives me something to do between weekends.

                    _Microsoft MVP (Visual C++)

                    Polymorphism in C

                    P Offline
                    P Offline
                    Peter_in_2780
                    wrote on last edited by
                    #9

                    ...or, if you really want to mess with peoples' heads: Leave main.cpp as is. In the other files, declare extern const int x; The compiler then won't let you modify x from these other files, but anyone looking at main.cpp could not tell why. ;P If you do this, you would have a readymade entry for the Coding Horrors forum!

                    Software rusts. Simon Stephenson, ca 1994.

                    1 Reply Last reply
                    0
                    • L Lost User

                      Well, to be precise, declaring it outside of any code gives the variable global scope, using 'extern' just allows that symbol to be imported into the other source code file at compile time.

                      Stefan_Lang wrote:

                      On a sidenote

                      Verging on dogma. A solution demands certain things, and with C, you use alot of globals. OK, you can get into a mess, and if that is the case the coder whould wonder about his ability, rather than what features of the language to use. :)

                      ============================== Nothing to say.

                      S Offline
                      S Offline
                      Stefan_Lang
                      wrote on last edited by
                      #10

                      1. Yes and no. Syntacticaly you are correct. At least if you only have one .c file. But if you have more than one .c file, each file defines a local scope within the program. It's a wider scope than a function block or struct, but not as wide as a variable declared with 'extern' in a header. 2. Maybe. I am aware that many C program(mer)s like to use a lot of globals, and some mechanisms that make it easier to avoid globals in C++ are not available in C. Still, the problems caused by too many global variables is exactly the same in C as it is in C++. It's just that the effort to avoid them is on a different scale. I've also noticed that long-time C programmers turning to C++ have a nasty habit to keep using lots of globals if there's no one around to keep them in check ('them' referring to both the globals and the programmers). Therefore I like to give out this advice to C and C++ programmers alike.

                      L 1 Reply Last reply
                      0
                      • _ _Superman_

                        I assume what you're trying to do is to create a global variable in Main.cpp and make it read only in other .cpp files? If so, here is what you can do. Create the global variable as static in Main.cpp - static int x = 0; Now create a function in Main.cpp that returns this value - int GetX() { return x; } Now you can call this function from other .cpp files to get the value of x and also x can only be modified from Main.cpp. Here you do not need to use the extern keyword at all. Also to call GetX in other .cpp files, you will need to declare it there - int GetX();

                        «_Superman_»  _I love work. It gives me something to do between weekends.

                        _Microsoft MVP (Visual C++)

                        Polymorphism in C

                        M Offline
                        M Offline
                        Mohibur Rashid
                        wrote on last edited by
                        #11

                        I was going to answer that ;P

                        I know I am coward since the day I know that fortune favors the brave

                        1 Reply Last reply
                        0
                        • S Stefan_Lang

                          1. Yes and no. Syntacticaly you are correct. At least if you only have one .c file. But if you have more than one .c file, each file defines a local scope within the program. It's a wider scope than a function block or struct, but not as wide as a variable declared with 'extern' in a header. 2. Maybe. I am aware that many C program(mer)s like to use a lot of globals, and some mechanisms that make it easier to avoid globals in C++ are not available in C. Still, the problems caused by too many global variables is exactly the same in C as it is in C++. It's just that the effort to avoid them is on a different scale. I've also noticed that long-time C programmers turning to C++ have a nasty habit to keep using lots of globals if there's no one around to keep them in check ('them' referring to both the globals and the programmers). Therefore I like to give out this advice to C and C++ programmers alike.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          This is what gets me about C++ dogma. I have seen worse C++ code than C code. It is capale of being more obtuse, messy, confusing and dangerous. Bad code is bad code, and dogma doesnt solve this, that why I write good code and leave the dogma at the church. :)

                          ============================== Nothing to say.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups