[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 369
  • Last Modified:

declaration

how do i declare a global variable?

Thanks
0
danil082697
Asked:
danil082697
  • 7
  • 5
1 Solution
 
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
 
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
Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

 
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

Featured Post

Get Cisco Certified in IT Security

There’s a high demand for IT security experts and network administrators who can safeguard the data that individuals, corporations, and governments rely on every day. Pursue your B.S. in Network Operations and Security and gain the credentials you need for this high-growth field.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now