Solved

declaration

Posted on 1998-04-21
12
365 Views
Last Modified: 2010-04-15
how do i declare a global variable?

Thanks
0
Comment
Question by:danil082697
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 1

Accepted Solution

by:
Talence earned 20 total points
ID: 1250879
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1250880
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1250881
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:Talence
ID: 1250882
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1250883
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
 
LVL 1

Expert Comment

by:Talence
ID: 1250884
> That info was more for the benefit of danil.

So?
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1250885
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
 
LVL 1

Expert Comment

by:Talence
ID: 1250886
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1250887
Thanks .. although you could still end up getting the points anyway.  But then, what's a few points between Experts :-)

0
 
LVL 10

Expert Comment

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

0
 
LVL 1

Expert Comment

by:Talence
ID: 1250889
Hehe, poor you :-)

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

Tal.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1250890
Nah -- you keep'em.

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

:-)

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question