• C

declaration

how do i declare a global variable?

Thanks
LVL 1
danil082697Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TalenceCommented:
Instead of declaring it within a function body (i.e. locally),
you declare it uhm... globally. Example:

int my_global_variable;

void main()
{
   my_global_variable=1;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RONSLOWCommented:
Sorry, That is not (quite) correct.

You DECLARE it as
  extern int my_global_variable;
in all routines that use it (usually by putting it in a .h file)

You define it as
  int my_global_variable;
in ONE source file only.

Please reject and accept this (more) correct answer

0
RONSLOWCommented:
also you can initialize the variable when you defined (not declare) it.

eg.

int my_global_variable = 1;

For structs, this is important, because you can more easily set up initial values like this.  If you do it later you need separate assignment statements for each struct member.

0
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

TalenceCommented:
Yes, I know you can initialize variables like this, but I
wanted to give a (small) example of using this variable in
the main function... I did mistake _declaring_ variables
with _defining_ them for some strange reason, so my answer is
not correct; sorry, should have read the question more carefully.

Tal.
0
RONSLOWCommented:
That info was more for the benefit of danil.

Here is a fuller example.

Declare the global in myglobals.h
Define the global in main.c
Use the global in other.c

myglobals.h:
  extern int my_global_variable;

main.c:

  #include "myglobals.h"

  int my_global_variable = 1; /* initial value of 1 */
  ...

other.c:

  #include "myglobals.h"
  void F() {
    int x = my_global_variable;
  }

0
TalenceCommented:
> That info was more for the benefit of danil.

So?
0
RONSLOWCommented:
You had said "Yes, I know you can initialize variables like this".

I said "That info was more for the benefit of danil" to explain that my comments were not there to imply that you DIDN'T know how to do it, but rather to give danil further info on how to declare and define global variables.

That is the "So?" of it.  Just trying to be polite.

0
TalenceCommented:
Ah, I see.. I had completely misunderstood your motivations behind your "That info... etc" line, my apologies! Anyway..
I think your examples are pretty clear and should completely
(not to mention correctly) answer Danil's question. Have fun
with the points (albeit not many) :-)

Tal.
0
RONSLOWCommented:
Thanks .. although you could still end up getting the points anyway.  But then, what's a few points between Experts :-)

0
RONSLOWCommented:
Told you you'd get the points instead of me ... sometimes I wonder why I put the effort in to help these people :-(

0
TalenceCommented:
Hehe, poor you :-)

I'd gladly transfer the 50 points to you, though.

Tal.
0
RONSLOWCommented:
Nah -- you keep'em.

I'll just sit here an feel sorry for myself for a while (sob)

:-)

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.