What's the best way to declare and define global variables

What's the best way to declare and define global variables and functions?

Questions by lakshmi5783

Showing Answers 1 - 12 of 12 Answers

rimi.mnnit

  • May 30th, 2008
 

If a program is in several source files,the best arrangement is to place each definition 
in some relevant source(.c) file, with an external declaration of function n variables in
 a seperate header file that is included by #include at the front of each source file..

rks_147

  • Jun 20th, 2008
 

Best Way to represent a variable as global is , Just define the variable at the initial or just below the headers files


                   For Example:-
                                           #include<stdio.h>
                                           #include<conio.h>
                                           #define PIE 3.1415
                                          
                                           void main()
                                            {
                                                   body of program      
                                                                                                
                                            }
                                            getch()

where PIE is define globally.

kbjarnason

  • Jul 1st, 2010
 

The best way to declare and define global variables is to fix your code so you're not using them.

The second best way is to determine whether you mean "file scope" - "global" but only within the given source unit - or actual global, visible to everything.

If it's file scope you're after, define the variable as a static, outside the confines of any function block.

If it's truly a global, declare it in a header as an external, then define it once in a single source module.  Include the header in any source module which requires access to the variable.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions