How do I differentiate between variable types in C++ when naming them?

0
0
Asked By CuriousCoder34 On

I'm new to C++ and trying to understand variable naming better. If I declare two variables with the same name but different types in the same scope, like this:

```cpp
int a = 1;
char a = 'b';
```

I know I'd face an issue when trying to print their values, for example, using `std::cout << a;`. Is there a way to specify which variable type I'm referring to, or is this just not allowed?

1 Answer

Answered By SyntaxSorcerer99 On

You can't declare two variables with the same name in the same scope. If you try, the compiler will throw an error when you define `char a` after `int a`. All variable names must be unique in their respective scope.

VariableVisionary42 -

So, variables can only have one type in a specific scope, right?

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.