User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Feb 2007
Posts: 11
Reputation: Mac.Z is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Mac.Z Mac.Z is offline Offline
Newbie Poster

Question Qs about C++ variable types

  #1  
Feb 7th, 2007
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++:
  1. cout<< "int: " <<sizeof(int) <<"\n";
  2. 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:
  1. unsigned int c;
  2. cout <<"Enter c: ";
  3. cin >>c;
  4. 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:
  1. double c;
  2. cout <<"Enter C: ";
  3. cin>>c;
  4. 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
Last edited by Mac.Z : Feb 7th, 2007 at 3:21 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 4,716
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 312
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Qs about C++ variable types

  #2  
Feb 7th, 2007
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.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Feb 2007
Posts: 11
Reputation: Mac.Z is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Mac.Z Mac.Z is offline Offline
Newbie Poster

Re: Qs about C++ variable types

  #3  
Feb 7th, 2007
Sorry iamthewee, had problems with the forum posting the entire thread..
hope you read the rest and thanks for answering part of the Qs..
Reply With Quote  
Join Date: Aug 2005
Posts: 4,716
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 312
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Qs about C++ variable types

  #4  
Feb 7th, 2007
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.
Last edited by iamthwee : Feb 7th, 2007 at 3:58 pm.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Nov 2005
Location: Canada
Posts: 236
Reputation: dwks will become famous soon enough dwks will become famous soon enough 
Rep Power: 4
Solved Threads: 20
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Qs about C++ variable types

  #5  
Feb 7th, 2007
Originally Posted by Mac.Z View Post
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++:
  1. cout<< "int: " <<sizeof(int) <<"\n";
  2. 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:
  1. unsigned int c;
  2. cout <<"Enter c: ";
  3. cin >>c;
  4. 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:
  1. double c;
  2. cout <<"Enter C: ";
  3. cin>>c;
  4. 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.
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
Reply With Quote  
Join Date: Feb 2007
Posts: 11
Reputation: Mac.Z is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Mac.Z Mac.Z is offline Offline
Newbie Poster

Re: Qs about C++ variable types

  #6  
Feb 7th, 2007
Thanks guys for your responses

Originally Posted by dwks View Post



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========
  1. char a,b,c;
  2.  
  3. cout <<"Enter a: ";
  4. cin >>a;
  5. cout <<"a= " <<a <<endl;
  6. cout <<"Enter b: ";
  7. cin >>b;
  8. cout <<"b= " <<b <<endl;
  9. cout <<"Enter c: ";
  10. cin >>c;
  11. 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======
  1. cout <<"Enter d: ";
  2. cin >>d;
  3. 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========
  1. unsigned long double a;
  2. cout <<"Enter a: ";
  3. cin>>a;
  4. cout <<a <<endl;
======end code2======
Input:1000000
Output:1e+006

=======code3========
  1. unsigned double c;
  2. cout << "Enter c: ";
  3. cin >> c;
  4. 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 ..
Last edited by Mac.Z : Feb 7th, 2007 at 7:21 pm.
Reply With Quote  
Join Date: Feb 2007
Posts: 11
Reputation: Mac.Z is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Mac.Z Mac.Z is offline Offline
Newbie Poster

Re: Qs about C++ variable types

  #7  
Feb 7th, 2007
Originally Posted by Mac.Z View Post
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======
  1. unsigned int a,b;
  2. cout << "Enter a: ";
  3. cin >> a;
  4. cout << a << endl;
  5. cout << "Enter b: ";
  6. cin >> b;
  7. cout << b << endl;
======end code4===
Input: 10000000000
output:
4294967295
Enter b: 3435973836


so again how 10000000000 was exactly processed by the program?
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,475
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 16
Solved Threads: 271
Moderator
Staff Writer
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: Qs about C++ variable types

  #8  
Feb 7th, 2007
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.
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 3:39 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC