Hello guys. I'm currently learning alone about c++. One thing I've learned is that the most common basic types available to me are char, int, and double. A variable of type char stores a single character, variables of type int store integers (numbers without decimal places), and variables of type double store numbers with decimal places.
I have 2 codes below where the first code with thisisanumber working properly with int, while the second code doesn't work with int. I thought int is integer and example on interger is 5. My question is why doesn't second code below doesn't work?
1 2 3 4 5 6 7 8
int main ()
{
int thisisanumber;
cout << "Please enter a number: ";
cin >> thisisanumber;
cout << "You entered: " << thisisanumber << "\n";
}
1 2 3 4 5 6 7 8
int main ()
{
int 1;
cout << "Please enter a number: ";
cin >> 1;
cout << "You entered: " << 1 << "\n";
}
I believe it's because in c++ you can't assign variable names as just numbers, you can however, have a combination of numbers and letters, such as number1.
In case you're wonderting why it's bad style, it's because some identifier names are reserved for use by the compiler and C++ standard library (and similar.)
It's not totally clear cut, but when you see a double undescore it's almost always something to do with the compiler, whereas a single underscore is probably the standard library (etc.)
Someone has gone to the trouble of posting the relevant rules here. As they are a bit involved, most people just avoid leading underscores altogether to make life simpler for everyone.