DEV Community

Cover image for Local Variables
Saloni Goyal
Saloni Goyal

Posted on • Edited on

1

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!

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay