-4

I create a C++ project test that contains three files as follows: hello.hpp:

#ifndef HELLO_H
#define HELLO_H
int cHelloSay();
#endif 

hello.cpp:

#include "hello.hpp"
#include<iostream>
int sayHello(){
    std::cout << "123";
}
int i=sayHello();

and main.cpp:

#include "hello.hpp"
int main(int argc, char** argv) {
    return 0;
}

then I compile this project and it outputs:123. So I am confused why the line int i=sayHello(); is executed though main() does not call it ?

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
JustinGong
  • 399
  • 1
  • 4
  • 17
  • in C++ you can call functions when initializing globals. it's called before `main` – Jean-François Fabre Nov 25 '17 at 11:48
  • because this line `int i=sayHello();` - called inside `initterm` – RbMm Nov 25 '17 at 11:49
  • 1
    note that the c tag isn't relevant here. the code cannot work in C because you cannot call functions when initializing globals in C. and it's no C++11 feature either, so plain C++ – Jean-François Fabre Nov 25 '17 at 11:49
  • Be careful accessing `std::cout` outside of the `main()` scope. That could lead to trouble. – user0042 Nov 25 '17 at 11:51
  • 1
    Possible duplicate of [May I initialize a global variable with the result of a function call?](https://stackoverflow.com/questions/6337426/may-i-initialize-a-global-variable-with-the-result-of-a-function-call) – Jean-François Fabre Nov 25 '17 at 11:52
  • @Jean-François Fabre why is var `i` global var? – JustinGong Nov 25 '17 at 12:01
  • @user0042 [`std::ios_base::Init`](http://en.cppreference.com/w/cpp/io/ios_base/Init) makes sures that it works :) – Quentin Nov 25 '17 at 12:02
  • 1
    because it's in global scope. – Jean-François Fabre Nov 25 '17 at 12:02
  • @RbMm do not understand initterm – JustinGong Nov 25 '17 at 12:07
  • @Quentin I remember having troubles once. But I'm old, and maybe that was on an embedded system ;-) ... – user0042 Nov 25 '17 at 12:09
  • @Jean-François Fabre what I mean is different of [it]https://stackoverflow.com/questions/6337426/may-i-initialize-a-global-variable-with-the-result-of-a-function-call – JustinGong Nov 25 '17 at 12:09
  • @Jean-François Fabre thank you very much. Now I have understood – JustinGong Nov 25 '17 at 12:35
  • @AndrewGong - when you write `int i=sayHello();` compiler create internal function, which call `sayHello();` and put result to `i`. and place pointer to this function in (".CRT$XCA", ".CRT$XCZ") (c++) or (".CRT$XIA", ".CRT$XIZ") (c) section. the `[w]mainCRTStartup` call special function `initterm` (you can found it src code in crt) which walk by this segment and call functions by pointer. read for example [How to execute some code before entering the main](https://stackoverflow.com/questions/728939/how-to-execute-some-code-before-entering-the-main-routine-in-vc) – RbMm Nov 25 '17 at 13:10
  • @RbMm thank you very much. I need to learn more – JustinGong Nov 25 '17 at 13:19
  • @AndrewGong - you also can set breakpoint exactly on `int i=sayHello();` line and view from where and how it called.also search for _initterm in crt src – RbMm Nov 25 '17 at 13:26

2 Answers2

0

Now I know any variable declared outside a function is a global variable though it is in another *.cpp file. So the line int i=sayHello(); will be executed before call main().

JustinGong
  • 399
  • 1
  • 4
  • 17
0

When after the program is being complied the variables are initialized. As part of this it is calling your int i=sayHello(); before int main() is called.

Hopes this helps!

Jake Freeman
  • 1,700
  • 1
  • 8
  • 15