Solved

declaration

Posted on 1998-04-21
12
362 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 recursion in the C programming language.

932 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

10 Experts available now in Live!

Get 1:1 Help Now