Solved

declaration

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

Thanks
0
Comment
Question by:danil082697
  • 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now