DEV Community

Cover image for Local Variables
Saloni Goyal
Saloni Goyal

Posted on • Updated on

Local Variables

Variables in C++ have a type.

  • C++ is a type safe language, i.e., it enforces your decisions around types.
  • string, number, date, Employee, etc.
  • some types, like int, are built into the language
  • some are user-defined - some "users" are actually library writers

Declaration and Initialization

  • Variables must be declared (type + name) before they are used.

int limit;

where int is the type and limit is the variable name.

  • built in types are not initialized
  • user-defined types might be

Best Practice: declare and initialise at once.

int limit = 100;

If you make it a habit to always initialise variables with declaration, you can rely on the compiler to figure out the appropriate type to keep the value in.

auto x = 7; // x is an int

  • C++ is a strongly-typed language.

Please leave out comments with anything you don't understand or would like for me to improve upon.

Thanks for reading!

Top comments (0)