Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to store integer value in Char?All of my integer types have same

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Thanx in advance
1.I declared a variable of type CHAR.My book said you can also store integers in it,but if I ll assign to char variable 65 ,the output will be A.What to do?I´m learning C++ on VC++ program.Could that be cause of my problems?
2.All of integer types have same range(long,short,int).Why?
3.Is placing U and L realy necesseary?In book it says otherwise the value will be stored as int?How would I know,the range is the same.Similar question for U.

unsigned long x=116UL;

Same for floating point type

float x=1.4F
 
Hi, yes you can use 'char' to count within a range of 256 values. 'short' values have a range of 65,535 and 'long' values range up to over 4 billion or something.

A char takes up one byte of memory, a short two bytes and a long three bytes. You shouldn't have any problems using char to count, for example:

char x = 0;
while (x < 25)
{
// do something
x ++;
}

As long as it is in range. Also, something like:

char x = 4;
char y = 9;
char z = x + y;

Is perfectly legal.

As for adding U and L at the end, I've never bothered.
To differentiate the char value between a character and a number on declaration:

char x = 8; // A number
char y = 'D';// A letter - note the quotes!

If you're having trouble outputting a char 'number' as a number try typecasting it, thus:

/* Do out output stuff here */ << (int)x

To typecast, simply declare the required type in parenthesis before the variable.

Hope this answers your question.
 
Thank you very much.Can you tell me why all of those types have the same range on my computer?Though I don´t get it,what is wrong with my compiler?Made in Taiwan?!
 
I can't imagine why all the types have the same range wihtout checking a sample of the offending code. What compiler is it?
 
I have MVC++ standard edition.It could be something with my computer,since TP5.5 also did some strange things on a few ocassions.
 
I'm not too sure about that. Why don't you show a small snippet of your code here and I'll look at it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top