Search code examples
c++visual-studiovariablesinitialization

C++, Using iostream library to output an uninitialized variable \ Visual Basic


How can I make Visual Studio (2019) print an uninitialized variable?

#include <iostream>

int main()
{
  int x;
  std::cout << x;
  return 0;
}

I understand this is relatively useless and uninitialized variables should not normally be used, but I would imagine there should be a way to make it print the value in the random mem location.

Error Code: C4700 - uninitialized local variable 'x' used


Solution

  • There is a method to do it.

    int main()
    {
    
        int num[1];
        for (int i = 0; i < 1; i++)
        {
            std::cout << num[i];
        }
    }
    

    In my opinion, outputting uninitialized variables does not make much sense. Maybe you want to get the address.

    int x;
    std::cout << &x;