Hello,
I'm getting started with C++ and have some questions about the variable types:

1)The size of a variable type, is it defined by the compiler or computer or both? and why it differ from one to another?

2)Isn't short int supposed to hold smaller data than int can hold and the same with int and long it, then why sometimes the sizez of data that both of them can hold(short int & int or int & long it) are equal ??
on my pc with Microsoft Visal C++:

cout<< "int: " <<sizeof(int) <<"\n";
    cout<< "short int: " <<sizeof(short int) <<"\n";

both can hold 2 bytes only, then what's the point of having short int? the same applies to double & long double..both can hold the same size of data too.

3)For me, "unsigned long int" can hold 4 bytes, it's mentioned that maximum value it can hold is "4,294,967,295" what if I wanted to input a bigger number?what should I use?

4)For following code:

unsigned int c;
cout <<"Enter c: ";
cin >>c;
cout <<c <<endl;

what does the program do when I enter I negative value ? I tried -1 and the output was:4294967295..how "-1" was processed? and why it didn't get refused by the program ?!!!


5)For the following code:

double c;
cout <<"Enter C: ";
cin>>c;
cout <<c <<endl;

What does the program do when I enter a value bigger than it can hold ? I tried 1000000( one million) and the output was:"1e+006", how that value was processed ?

any help would be much appreciated :)

thanks :)

Don't worry about using short or long data types.

Just use double for numbers such as 23.34323

and int for numbers such as 432.


If you're really worried about really long numbers 23434.34342342898394898983984939 you need to be thinking about using a big number library.

Sorry iamthewee, had problems with the forum posting the entire thread..
hope you read the rest and thanks for answering part of the Qs..

Hi,


Using statements like unsigned long int and expecting consistency across different platforms is probably asking for trouble.

That's just the problem, really long integers and floating numbers depends largely on the hardware.


The only real way to guarantee a standard numbering system across all computers, is to use a third party number library. But that adds an extra bloating to your program.

However, for handling really long numbers it is necessary.

Hello,
I'm getting started with C++ and have some questions about the variable types:

1)The size of a variable type, is it defined by the compiler or computer or both? and why it differ from one to another?

C was designed with efficiency in mind. (And C++ was developed with C in mind.) Variable types vary from machine to machine, so that the compiler can pick the most efficient type for a given computer. The specifications only include lower limits, and variables can be any size as long as they can store at least the minimum range.

2)Isn't short int supposed to hold smaller data than int can hold and the same with int and long it, then why sometimes the sizez of data that both of them can hold(short int & int or int & long it) are equal ??
on my pc with Microsoft Visal C++:

cout<< "int: " <<sizeof(int) <<"\n";
    cout<< "short int: " <<sizeof(short int) <<"\n";

both can hold 2 bytes only, then what's the point of having short int? the same applies to double & long double..both can hold the same size of data too.

The standard only states that a short must hold at least -32767 to +32767. An int is the same way. If one of the two is going to be larger, it will be the int. Many implementations make int the same as a long.

3)For me, "unsigned long int" can hold 4 bytes, it's mentioned that maximum value it can hold is "4,294,967,295" what if I wanted to input a bigger number?what should I use?

In C, some compilers support a long long type. In C++, you'd have to go with a floating point type such as float or double.

4)For following code:

unsigned int c;
cout <<"Enter c: ";
cin >>c;
cout <<c <<endl;

what does the program do when I enter I negative value ? I tried -1 and the output was:4294967295..how "-1" was processed? and why it didn't get refused by the program ?!!!

The way most implementations represent negative numbers means that overflow (the maximum value +1) and underflow (the minimum value, in this case 0, -1) simply wrap. This is standard for underflow for unsigned types, I think, but is otherwise undefined. You created underflow, and the value wrapped to the highest possible value.

5)For the following code:

double c;
cout <<"Enter C: ";
cin>>c;
cout <<c <<endl;

What does the program do when I enter a value bigger than it can hold ? I tried 1000000( one million) and the output was:"1e+006", how that value was processed ?

It would wrap as many times as nessesary. For example, if it can hold values from 0 to 10, and you enter 43, you would end up with 3. Think of it as the value you assigned to it modulo the maximum value (for unsigned types).

any help would be much appreciated :)

thanks :)

I hope that helped.

For more information, read a book or search around.

Thanks guys for your responses :)


The way most implementations represent negative numbers means that overflow (the maximum value +1) and underflow (the minimum value, in this case 0, -1) simply wrap. This is standard for underflow for unsigned types, I think, but is otherwise undefined. You created underflow, and the value wrapped to the highest possible value.


It would wrap as many times as nessesary. For example, if it can hold values from 0 to 10, and you enter 43, you would end up with 3. Think of it as the value you assigned to it modulo the maximum value (for unsigned types).

This is REALLY INTERESTING, and explained many things but it lead to more MANY thoughts too..

for me it explains the output of the following:
=======code1========

char a,b,c;

    cout <<"Enter a: ";
    cin >>a;
    cout <<"a= " <<a <<endl;
    cout <<"Enter b: ";
    cin >>b;
    cout <<"b= " <<b <<endl; 
    cout <<"Enter c: ";
    cin >>c;
    cout <<"c= " <<c <<endl;

======end code1=======
input: 123
output:
a= 1
Enter b: b= 2
Enter c: c= 3

it kept wrapping as many times as neccessary as you have mentioned..but is that considered to be a good thing? for me I think it's like a bug or something !!

let's say we had int d and followed code1 with:
=====code1.a======

cout <<"Enter d: ";
    cin >>d;
    cout <<"d= " <<d <<endl;

======end code1.a===
and the input for code1 one was 1234..the program will give variable d automatically the value: 4 !! well, I don't want that to happen ! I want the user to enter every value by himself..and if the inputted char is not one single character..then do nothing or give an error..

because we was talking about char, it gave the next variable the rest of the inputted value[ took the first char only]
but this is not the case with float or int ..I tried to input a value bigger than the maximum one for a specific variable then followed it by another variable but couldn't figure out how the wrapping has been done.

yet another question:
============
as I might have mentioned above, long double & double can hold the same size of data [in the paltform I'm working on] what just made me go crazy is:

=======code2========

unsigned long double a;
cout <<"Enter a: ";
cin>>a;
cout <<a <<endl;

======end code2======
Input:1000000
Output:1e+006

=======code3========

unsigned double c;
cout << "Enter c: ";
cin >> c;
cout << c << endl;

======end code3=======
Input:1000000 (as same as previous input)
output:1000000


then despite that fact that long double & double can hold the same size of data, they are not the same in a way !! can any one explain ?

and sorry for my English ..

but this is not the case with float or int ..I tried to input a value bigger than the maximum one for a specific variable then followed it by another variable but couldn't figure out how the wrapping has been done.

to clarify what I meant,
maximum value for unsigned int: 4294967295

=====code4======

unsigned int a,b;
      cout << "Enter a: ";
      cin >> a;
      cout << a << endl;
      cout << "Enter b: ";
      cin >> b;
      cout << b << endl;

======end code4===
Input: 10000000000
output:
4294967295
Enter b: 3435973836

so again how 10000000000 was exactly processed by the program?

Regarding your question about cin input: You're getting input problems with your input stream. If you only grab 1 charecter from the stream, what happens if the user enters more than 1? That's right, they just stay there waiting. This is the major drawback of using cin >> for input.

A much better way is to use getline(), which grabs ALL the charecters from the stream, not just the amount you need. You can then use this to check if the user entered the correct amount, print out an error message if necessary, and proceed when the input is correct. getline also eliminates the problem of the newline left in the input stream.

This article has been dead for over six months. Start a new discussion instead.