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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Static variable in c/c++

Static variable in c/c++

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++question
5 Posts 2 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
    Subrat 4708266
    wrote on last edited by
    #1

    I am in problem with static variable.Please help me. Program: file1.h int x = 10; file2.cpp #include "file1.h" #include "stdio.h" extern int x; void main() { printf("%d",x); } o/p: Error(Unresolved external symbol)... As per my knlowledge is consern I know to access x I have to use extern.But it is giving error.I want to know whts there reseaon? Compiler: vc++ 2005(8.0ver)

    M 1 Reply Last reply
    0
    • S Subrat 4708266

      I am in problem with static variable.Please help me. Program: file1.h int x = 10; file2.cpp #include "file1.h" #include "stdio.h" extern int x; void main() { printf("%d",x); } o/p: Error(Unresolved external symbol)... As per my knlowledge is consern I know to access x I have to use extern.But it is giving error.I want to know whts there reseaon? Compiler: vc++ 2005(8.0ver)

      M Offline
      M Offline
      montiee
      wrote on last edited by
      #2

      Technically "x" isn't an extern the way you've done it. You don't need the extern keyword in file2.cpp. Extern is meant to imply that the variable is defined externally to the files that are referencing it. Since you've defined x in the header and then included it it isn't defined externally. Also you aren't using any static variables whatsoever in the example you've provided. I think you really need to find a basic C primer on the net talking about extern and static variable and read through that since you are missing some fundemental understandings. No point writing code until you clear that up.

      S 1 Reply Last reply
      0
      • M montiee

        Technically "x" isn't an extern the way you've done it. You don't need the extern keyword in file2.cpp. Extern is meant to imply that the variable is defined externally to the files that are referencing it. Since you've defined x in the header and then included it it isn't defined externally. Also you aren't using any static variables whatsoever in the example you've provided. I think you really need to find a basic C primer on the net talking about extern and static variable and read through that since you are missing some fundemental understandings. No point writing code until you clear that up.

        S Offline
        S Offline
        Subrat 4708266
        wrote on last edited by
        #3

        Please modify the code by which I can access x declared in file1.h in file2.cpp. Again give me one fantastic example of usages of keyword extern.Please don't give example like below... extern int x; fun() { printf("%d", x); } int x = 80; fun2(){ printf("%d", x); }

        M 1 Reply Last reply
        0
        • S Subrat 4708266

          Please modify the code by which I can access x declared in file1.h in file2.cpp. Again give me one fantastic example of usages of keyword extern.Please don't give example like below... extern int x; fun() { printf("%d", x); } int x = 80; fun2(){ printf("%d", x); }

          M Offline
          M Offline
          montiee
          wrote on last edited by
          #4

          file1.c ------- int counter = 1; file2.h ------- extern int counter; file2.c --------- #include "file2.h" void countToTen() { while (counter <= 10) { printf("%d\n", counter++); } } Notice that counter is defined in the file.c file hence to access it we need to define it as external to our file2.c file that uses it. Hope that gives you an idea about extern vars. Similar principle applies to extern functions. You really need to read some books on the topic.

          S 1 Reply Last reply
          0
          • M montiee

            file1.c ------- int counter = 1; file2.h ------- extern int counter; file2.c --------- #include "file2.h" void countToTen() { while (counter <= 10) { printf("%d\n", counter++); } } Notice that counter is defined in the file.c file hence to access it we need to define it as external to our file2.c file that uses it. Hope that gives you an idea about extern vars. Similar principle applies to extern functions. You really need to read some books on the topic.

            S Offline
            S Offline
            Subrat 4708266
            wrote on last edited by
            #5

            Thanks a lot. It'd be great if you are sending me some useful links.

            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