ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

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

user-image
تم إضافة السؤال من قبل Zaid Rabab'a , Software Development Team Leader , Al-Safa Co. Ltd.
تاريخ النشر: 2013/07/11
Tarek Issaoui
من قبل Tarek Issaoui

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 no variables in a seperate header file that is included by #include at the front of each source file.

abdelgader abdelgader
من قبل abdelgader abdelgader , software engineer , STC


1- in C# you can create class and declare your variable as static and call them any where in the project(collection of classes) public class a { public static int h=10; } to call this any where in the same package just type className.variableName -> a.h;
2- or you can declare variable as static out side the main method and by default the main method is static so you can call static variable inside static method but in this case you can call the variable inside the same class body only public class hh { static int x; public static void main(String arg[]) { x=10; } }

Osama Gamal
من قبل Osama Gamal , Software Consultant , Ministry of Interior

you may use all declarations in an external file and use it

Mahmoud Manaa
من قبل Mahmoud Manaa , Network Engineer , Zain - Kuwait

I don't have a good knowledge in C++ Syntax So..

In C# you can Create A new Class and use it for all Project level variables and you may want to put a connection string and File locations Special Folder Locations a certain Registry Key etc…

e.g...

1-Create a new class by pressing (Shift+alt+C) or Right click on Project Name From Solution Explorer >Add>Class

2.Give your class a name ,Click on add

3.in the class create static variables like :

 

    public class BikeDetails

      {

        internal static string Model_Name;

        internal static double Engine_Bore;

        internal static bool Fuel_Injection;

 

      }

 

4.you can reach your variables by calling the class like :

 

    private void Save_BikeDetails_Click(object sender, EventArgs e)

      {

         BikeDetails.Model_Name="Honda CBR600rr -2006";

         BikeDetails.Engine_Bore =598;

         BikeDetails.Fuel_Injection = true;

 

      }

Ahmed Elsaid
من قبل Ahmed Elsaid , Senior Software Engineer , SER Group

In c++

1 - Global variable

you can declare the global variable on .cpp file and extern it on any another file 

example

in file1.cpp

int var =0;

and

in file2.cpp

extern int var ;

so you can use var in file1.cpp and in file2.cpp

or

you can declare the var as static variable on class or struct

example on struct

in filestruct.h

struct x{

static int var;

};

in filestruct.cpp

int x::var =0;

and use var

x::var =5;.....

 

example on class

in fileclass.h

class x{

public:

static int var;

};

in fileclass.cpp

int x::var =0;

and use var

          x::var =5;.....

 

Muhammed Ajmal Seeraveetil
من قبل Muhammed Ajmal Seeraveetil , Software Engineer , Wipro

Please follow the instruction while declaring a global variable 

  • Create a folder structure for your project (Eg : module/inc/pgm.h , module/src/pgm.c)
  • Define the scope of your global variable (ie, within a module or exposed to other module )
  • Define the prototype of your function in header files  

Roshan Thapa
من قبل Roshan Thapa , Database and Communication Manager , Finaccess Private Limited

Declare them in a separate static class in a separate file.

Ahmed Zahran
من قبل Ahmed Zahran , Senior Software Developer , Oracle

I agree with Tarek's solution, it is the standard solution you should use everyday especially in C.
in the .c file: int i =10; in the .h file: extern i; in other files: #include "****.h" TAKE CARE, beginners usually place their global variables declarations in a header file (.h), Which is completely wrong!! If you do not use the extern keyword in the header file, you will end up having different values of the same name, each in its file scope.
the reason is that #include just makes a "copy and past" operation during the preprocessing, something beginners always forget.

المزيد من الأسئلة المماثلة

هل تحتاج لمساعدة في كتابة سيرة ذاتية تحتوي على الكلمات الدلالية التي يبحث عنها أصحاب العمل؟