
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How are C++ Local and Global variables initialized by default?
The following is the same for both local and global variables. Basically, whenever you declare a variable, the compiler will call its default constructor unless you specify otherwise.
The language level types (e.g. pointers, 'int', 'float', 'bool', etc) "default constructor" does absolutely nothing, it just leaves the memory as it is when it is declared. This means that they can be pretty much anything because you usually can't be sure what was in that memory previously or even where the memory came from.
If you create a class that doesn't have a constructor, the compiler will create one for you which simply calls the constructor of each of its members/variables. If you have a constructor with arguments, and no constructor without arguments, then compiler will throw an error that a matching constructor was not found.
- Related Questions & Answers
- Why are global and static variables initialized to their default values in C/C++?
- What are local variables and global variables in C++?
- Global and Local Variables in C#
- Global and Local Variables in Python?
- Global and Local Variables in Java
- Global vs Local variables in Python
- Global and Local Inversions in C++
- What are global variables in C++?
- What are local variables in C++?
- What is the difference between global and local variables in Python?
- What is the difference between global and local Variables in JavaScript?
- Why are global variables bad in C/C++?
- What are class variables, instance variables and local variables in Java?
- What are the local and global scope rules in C language?
- Do local variables in Java have default values?