•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,907 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,113 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2088 | Replies: 7
![]() |
•
•
Join Date: Feb 2007
Posts: 11
Reputation:
Rep Power: 2
Solved Threads: 0
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++:
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:
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:
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
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++:
c Syntax (Toggle Plain Text)
cout<< "int: " <<sizeof(int) <<"\n"; cout<< "short int: " <<sizeof(short int) <<"\n";
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:
c Syntax (Toggle Plain Text)
unsigned int c; cout <<"Enter c: "; cin >>c; cout <<c <<endl;
5)For the following code:
c Syntax (Toggle Plain Text)
double c; cout <<"Enter C: "; cin>>c; cout <<c <<endl;
any help would be much appreciated

thanks
Last edited by Mac.Z : Feb 7th, 2007 at 3:21 pm.
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
you need to be thinking about using a big number library.
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.34342342898394898983984939you need to be thinking about using a big number library.
Member of: F-ugly code club
Join today don't delay!
Join today don't delay!
Hi,
Using statements like
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
However, for handling really long numbers it is necessary.
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.
Last edited by iamthwee : Feb 7th, 2007 at 3:58 pm.
Member of: F-ugly code club
Join today don't delay!
Join today don't delay!
•
•
•
•
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++:
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.c Syntax (Toggle Plain Text)
cout<< "int: " <<sizeof(int) <<"\n"; cout<< "short int: " <<sizeof(short int) <<"\n";
•
•
•
•
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:
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 ?!!!c Syntax (Toggle Plain Text)
unsigned int c; cout <<"Enter c: "; cin >>c; cout <<c <<endl;
•
•
•
•
5)For the following code:
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 ?c Syntax (Toggle Plain Text)
double c; cout <<"Enter C: "; cin>>c; cout <<c <<endl;
•
•
•
•
any help would be much appreciated
thanks
I hope that helped.
For more information, read a book or search around.
dwk
Seek and ye shall find.
"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.
"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison
"The only real mistake is the one from which we learn nothing."
-- John Powell
Seek and ye shall find.
"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.
"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison
"The only real mistake is the one from which we learn nothing."
-- John Powell
•
•
Join Date: Feb 2007
Posts: 11
Reputation:
Rep Power: 2
Solved Threads: 0
Thanks guys for your responses 
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========
======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======
======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========
======end code2======
Input:1000000
Output:1e+006
=======code3========
======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 ..

•
•
•
•
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========
c Syntax (Toggle Plain Text)
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;
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======
c Syntax (Toggle Plain Text)
cout <<"Enter d: "; cin >>d; cout <<"d= " <<d <<endl;
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========
c Syntax (Toggle Plain Text)
unsigned long double a; cout <<"Enter a: "; cin>>a; cout <<a <<endl;
Input:1000000
Output:1e+006
=======code3========
c Syntax (Toggle Plain Text)
unsigned double c; cout << "Enter c: "; cin >> c; cout << c << endl;
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 ..
Last edited by Mac.Z : Feb 7th, 2007 at 7:21 pm.
•
•
Join Date: Feb 2007
Posts: 11
Reputation:
Rep Power: 2
Solved Threads: 0
•
•
•
•
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.
maximum value for unsigned int: 4294967295
=====code4======
c Syntax (Toggle Plain Text)
unsigned int a,b; cout << "Enter a: "; cin >> a; cout << a << endl; cout << "Enter b: "; cin >> b; cout << b << endl;
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.
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.
tuxation.com - Linux articles, tutorials, and discussions
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- zenos series (C)
- Modern Replacement for Basic (Legacy and Other Languages)
- C++ Syntax (C++)
- c++ compiling problem (C++)
Other Threads in the C++ Forum
- Previous Thread: DES encryption using Crypto API's
- Next Thread: Program Design



Linear Mode