About char, int and double

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.
Last edited on
Because C++ requires variable names to begin with either a letter or an underscore.

See the "Identifiers" section on this page of the tutorial.

Variables. Data Types.
http://www.cplusplus.com/doc/tutorial/variables/

I guess it's to avoid confusion between integral literals and variable names.

char 42[] = "the answer"; // not valid

Andy
Last edited on
1 is a value, it is not a variable. It is called an "integer literal". Basically, it is a constant value that you cannot read another value into.

Coincidentally, a variable's name needs to start with a letter or an underscore:
1
2
3
4
5
int a; // OK
int a123; // OK
int _a; // OK, but bad style
int 123a; // bad
int _123a; // OK, but bad style 


http://en.cppreference.com/w/cpp/language/integer_literal
Another, more local reference: see "Literals" section on this page:

Constants
http://www.cplusplus.com/doc/tutorial/constants/

int _a; // OK, but bad style

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.

What are the rules about using an underscore in a C++ identifier?
http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier

Andy
Last edited on
Thanks everyone for fast reply's ! :D

Now can you give me example of three type of char, int and double ?
@djronix:

here's it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main(){
    char myChar;
    int myInt;
    double myDouble;
    cout << "Enter a char: " << endl;
    cin >> myChar;
    cout << "Enter an int: " << endl;
    cin >> myInt;
    cout << "Enter a double: " << endl;
    cin >> myDouble;

    cout << "You entered: " << endl;
    cout << "char: " << myChar << endl;
    cout << "int: " << myInt << endl;
    cout << "double: " << myDouble << endl;
    cout << endl;

    return 0;
}

Topic archived. No new replies allowed.