
 |
![]() |
|
|
|
|
 |
Using unsigned/typedef Keywords
Adding the word unsigned in front of the type syntax has a specific meaning within C++. The unsigned prefix prevents you from assigning any negative numbers. An integer can accept numbers: 0,1,-1,2,-2,3,-3, etc. But an unsigned int would accept numbers: 0,1,2,3,4,5,etc. If you want to ensure that you only assign positive numbers to a variable make sure it is an unsigned type. Here would be an example of declaring an unsigned float variable.
That’s it! If you ever wanted to find out the limits of any type just include the limits library. Here is an example:
 | |  | | |
void main()
{
cout <<"int Minimum: " << INT_MIN << endl;
cout <<"int Maximum: " << INT_MAX << endl;
cout <<"short int Minimum: " << SHRT_MIN << endl;
cout <<"short int Maximum: " << SHRT_MAX << endl;
cout <<"long int Minimum: " << LONG_MIN << endl;
cout <<"long int Maximum: " << LONG_MAX << endl;
cout <<"char Minimum: " << CHAR_MIN << endl;
cout <<"char Maximum: " << CHAR_MAX << endl;
} |
|  |
|
This will print all the minimum and maximum values of each type. These aren’t all the types, I’ll make a table of all the types right now.
{FIG}
There is a nice short cut for writing the type if the type is (pretty big)to long. For instance if you didn’t want to type out this:
You could use the keyword typedef. Typedef is short for type definition, you could use it like this:
 | |  | | |
typedef unsigned float UFLOAT; |
|  |
|
Now UFLOAT would be like writing out unsigned float. So instead you could write
Here is an example using typedef:
 | |  | | |
#include <iostream.h>
typedef unsigned short int USHORT;
void main()
{
USHORT NumOne;
USHORT NumTwo;
USHORT Result;
cout <<"Enter two non-negitive and small numbers: " << endl;
cin >> NumOne >> NumTwo;
Result = NumOne + NumTwo;
cout <<NumOne << "+" << NumTwo << "= " << Result << endl;
} |
|  |
|
This application prompts the user to enter two numbers that aren’t huge numbers nor are they negative. If the user enters a negative number they will get an unexpected result. But as you can see I used the typedef and it worked just as if I had typed in unsigned short int.Please Rate this Code:
Comments for: Using unsigned/typedef Keywords
Alex [alex.bunting@ntlworld.com] Posted: 3 times. says:
but what is the point in using this, and could yo uplease explain typedef more fully? |
 |
 |
Alex [alex.bunting@ntlworld.com] Posted: 3 times. says:
but what is the point in using this, and could yo uplease explain typedef more fully? says:
what about \"unsigned char\"? |
 |
 |
MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
Sorry for the long wait.
Typedef
I dont like this name
Typedef in C/C++ is used to define types. Say you dont like the type bool and you would prefer Boolean, this is what you would do:
The syntax is:
 | |  | | |
typedef <exisiting-type> <prefered-name>; |
|  |
|
Remember that if you typedef something, it automatically becomes "Existing Type" in your code! So you can have a whole bunch of typedefs involving other typedefs, and so on. For example:
 | |  | | |
typedef int _d;
typedef short _i;
typedef unsigned _i _d _uid;
|
|  |
|
Notice that the third line has _i and _d in the "existing-type" section. That is valid as long as you typedef it before calling that line.
Is there room for functions?
As I have explained above, typedefs can also be used with functions and classes. If you have a function like:
 | |  | | |
int foo(char*, int*, int); |
|  |
|
And you have another function which takes a function pointer as one of its parameters:
 | |  | | |
void doit(int (*f)(char*,int*,int), unsigned char);
|
|  |
|
You do not want to keep writing that long and confusing function pointer code. Instead you would make a typedef for it:
 | |  | | |
typedef int (*fPtr)(char*, int*, int);
|
|  |
|
And you would use the variable name fPtr to call that function like this:
 | |  | | |
// ... somewhere in the function
int x = 10;
fPtr(\"some string\", &x, --x);
|
|  |
|
So you see typedefs are very useful. Infact, if you pay attention, the entire WINAPI is made up of typedefs and macros mostly. Find windows.h and look at it, everything is a typedef (except for function declarations and macros).
 | |  | | |
typedef unsigned char BYTE;
typedef BYTE (*LPBYTE);
|
|  |
|
From the above, a byte is an unsigned char, and a lpbyte (lp always means "pointer to a") is a pointer to a byte.
I hope this helps clarify everything!
Regards,
MoMad |
 |
 |
MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
Unsigned just means that you want both the PLUS and MINUS ranges.
Technically, all l-value types have signed/unsigned options. Signed just means that you have BOTH the PLUS and MINUS. And unsigned means that you have the POSITIVE ONLY! Why would anyone use this?? Because it will make your max twice as large.
Example:
x and y are NOT the same. You will see that if you try that example above :)
Regards,
MoMad |
 |
 |
|
 |
|
 |
|
|
|
|
|
|
|