Help with understanding ( int, char, long, short, signed, unsigned etc.... )

My question is simple: When should I use a long, int, char, unsigned/signed variables??

When I declare a variable "unsigned;" what did I do it???
Why would I delcare an integer "long" or "short" ( unsigned or signed)??

Any examples of when things like "unsigned", "long", "short" etc... should, and shouldn't be used???

Can anybody explain the usage of long, int, char, unsigned/signed with some simple example?

I got "google" about this problem...
But can't really get any answer got well explanation among long, int, char, unsigned/signed variables.

Help will be appreciated.

Each integer datatype (includes char) has a range of values that it can work with.
Variables. Data Types. - C++ Documentation

Read the 'fundamental datatypes' section. It explains the range of each type.
Only you know the kind of data your program will use:
character strings == array of char
letter of the alphabet == char
integer numbers == int (long is often the same as int on 32 bit machines)

signed number dpend on whether you will be doing arithmetic operations and
what those are. Basically you should consider using signed variables simple yo avoid confusion: signed variables use a different printf() format specifier from unsigned variables. Keeping every integer as signed (int, long) makes housekeeping easier.

Read /usr/include/limits.h to see what the ranges are on your machine.

1 Like

Thanks a lot, jim.
I just read through the link that you share.
Do you mind to explain the usage with some simple real example?
Is it the Size* of int, char, long, short depend on my system?
Can I just based on /usr/include/limits.h to identify whether I should use int, char, long, short, signed, unsigned, etc?
Really thanks for your advice.
Simple real case example should be enough.
I just wanna to know the main difference if I'm using wrong variable.
Thanks again.

Yes and no. It is dependent on your platform (system) and also on your programming (data) model i.e. whether it is IPL16, IPL32, LP64, etc.

See 64-bit - Wikipedia for more information.

1 Like

All numeric values are signed unless you declare them as unsigned.

'char' is the smallest size the processor can individually address without restoring to binary operation tricks. On some old CRAY machines it was 32 bits! But you can depend on it being 8 bits on nearly any modern architecture unless you're programming embedded DSP's or toasters.

unsigned gets used when negative values don't make sense. Raw binary data for example probably isn't meant to be signed, so if you want to do sensible computation or comparison on it, you need it unsigned:

char c=192; // >= 128 becomes negative
unsigned char d=192;

if(c >= 192) printf("c >= 192\n"); // won't print
if(d >= 192) printf("d >= 192\n"); // will print

'int' suffices for common needs like loops and so forth. It's sized to work fairly optimally with your native architecture, so its size can vary -- in the DOS days it was usually 16-bit. It's supposed to be larger than char (otherwise you couldn't tell when getc, which returns negative values on error, is returning a char >=128.)

'short' is fairly dependably 16-bit. You might see it used for port numbers. You might have seen the htons() call, which means 'host to network short', which converts 16-bit numbers from native byte orders into network byte orders.

You might see long used for things like file offsets. It was 32-bits even in the DOS days, and stayed 32 bits when things moved to 32 bit. 32 bits turned out not to be enough and they had to resort to either adding system calls with extended ranges like llseek, or changing the system calls to use 64-bit types even on 32-bit systems. Which you can't do without rebuilding everything but what can y'do.

So what sizes you use mostly depend on what you have to talk to. If you want a for loop from 1 to 1000, use int. If you're reading in arrays of ASCII character data, use char. If you're writing a function to sort an array, use long for its size so it won't run out of range until your address width does. If you want to feed the number into a seek call as a file offset you should use its own type, off_t, which should be sensible for your architecture.

And if you want to read or save your data structures to/from file, you should be very specific about what sizes you use so they won't change when you recompile. #include <stdint.h> and declare things like int8_t, uint32_t, etc.

1 Like

Hi Corona688,

Really thanks for your explanation in detail.
I have more idea how to distinguish int, char, long, short, signed, unsigned etc now.
Thanks again and a lot.

many thanks, fpmurphy.
I understand the difference between all the basic variables now :slight_smile:

---------- Post updated at 03:03 AM ---------- Previous update was at 03:02 AM ----------

Thanks, jim mcnamara.
You're right.
Thanks for your advice and sharing.
I understand it now :slight_smile: