I've got this code:
#include <iostream>
int tabela[1];
tabela[0] = 1;
int main(){
std::cout << tabela[0];
std::cin.get();
return 0;
}
and it doesn't want to work. My compiler says " "tabela" doesn't name a type". However if I do this:
#include <iostream>
int tabela[1];
int main(){
tabela[0] = 1;
std::cout << tabela[0];
std::cin.get();
return 0;
}
It works. Can sb explain me why? Thanks in advance.
At the outermost level, a C++ file is a sequence of declarations. tabela[0] = 1;
is not a declaration - it's a statement (in particular an expression-statement). A function body, however, is a sequence of statements, so it's fine to put this line inside the body of main
(or any other function).
Some statements are declarations (called declaration-statements), but in general they're not.
for it to be valid C++, you can only initialize variables in global, you can't assign them there.
edit: comments beat me to it. props
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.