After calling C++ function from main function it turns into loop

Problem description:
I have declared a global variable in my c++ program. I have initialized this global variable in my main function. After that I have called another function named f(). In output it turns into a loop and constantly printing value.

Expected behavior:

It should print value just 2 times.

Actual behavior:

It has turned into a loop

Steps to reproduce:

Declare a global variable in c++. Create a function and call from main function.

Bug appears at this link:

[https://replit.com/@shakib04/coding-ninjas-ds-and-algo-with-cpp#chapter_2/lesson-5/2.5.2_global_variables.cpp]

Browser/OS/Device:
Chrome Browser/Windows 10

try:

#include <iostream>

int a = 10;

void f() {
    std::cout << a++ << std::endl;
}

int main(){
      f();
      std::cout << a << std::endl;
}
2 Likes