global variables in c ++

1

I'm doing a program with functions and a global variable, the idea was to handle a global variable in several functions, I do not know how to do global functions, this is what I try:

#include<cmath>
#include <stdio.h>
#include <iostream>

using namespace std;

int number=3;

void main(){
    cout<<number;
}

but I mark syntax in void main(){ I have no idea why it is assumed that number is a global variable.

What is the problem?

    
asked by DDR 24.07.2018 в 02:24
source

4 answers

3

The problem is that the function main is always of type int and must return a number.

#include<cmath>
#include <stdio.h>
#include <iostream>

using namespace std;

int number=3;

int main(){
    cout << number << '\n';
    return 0;
}

What do you want to achieve by using a global variable? It is not advisable to use global variables and maybe you want to pass it by reference to your functions. For example:

#include <iostream>
using namespace std;

void suma(int &n);

int main(){
  int numero = 0;
  suma(numero);
  cout << numero << '\n';
  suma(numero);
  cout << numero << '\n';
}

void suma(int &n){
 n = n + 10;
}
    
answered by 24.07.2018 / 03:05
source
5

Your understanding of global variables is correct.

Your error.

Your problem is not the global variables. As other users have already pointed out, your main function is incorrect; is incorrect because must return int . The reason for this obligation is because (as it says the language standard C ++ ) otherwise your program will be incorrect, I show it to you directly extracted from the standard (translation and highlighted by me):

  

3.6.1.2 Function% co_of%

     

Implementations should not predefine main . This function should not be overloaded. It must be declared with a return of type main , but otherwise its type is defined by implementation. Implementations should allow:

     
  • A int function returning () and
  •   
  • A int function returning (int, puntero a puntero a char)
  •   

    So your syntax error is because your int function does not follow the standard. I bet even the error the compiler shows you is telling you that this function should return main .

    Regarding global variables.

    Try not to use global variables :

    • Lack of location : The code is easier to understand when its scope is limited. Global variables can be read or modified from any part of the program, this makes it difficult to reason about their use or remember all the points in which they are used.
    • They lack access control or restriction verification : A global variable can be read or written from anywhere in the program, several rules about its use can be forgotten or violated.
    • Concurrency problems : If global variables can be accessed from multiple threads, it is necessary to synchronize them. When working with dynamically linked modules that use global variables, the resulting system may not be secure even when the modules are independent.
    • Namespace pollution : Global variables are in all contexts. You can end up using a global variable when you thought you were using a local or vice versa! (either by ignorance, misspelling the name or forgetting to create the local variable). Also, if you link modules with global variables whose name is the same, if you are lucky, you will have link errors ... if you are not lucky the linker will consider the variables as the same even if it was not your intention.
    • More link problems (in English) : I translated the points that I thought were most relevant to your problem.
    answered by 24.07.2018 в 08:47
    4

    I see two errors here: first, you use void main(){ where it should be int main , why? because you are going to return an integer. The second error I see is that you do not return it, that is, you do not add a return, as in the following code:

    #include<cmath>
    #include <stdio.h>
    #include <iostream>
    
    using namespace std;
    
    int number=3;
    
    int main(){
    
      cout<<number;
       return 0;
    }
    

    I recommend reading this: input_output in c ++ and local and global variables in c ++ int-main-vs-void-main-in-c

        
    answered by 24.07.2018 в 02:40
    1

    Try this syntax , why? because it is a standard in C / C ++, a int main and its respective return 0 .

    #include<cmath>
    #include <stdio.h>
    #include <iostream>
    
    using namespace std;
    
    int number=3;
    
    int main(){
        cout<<number;
    
        return 0;
    }
    
        
    answered by 24.07.2018 в 02:38