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


 
Thread Tools Search this Thread
Top Forums Programming Help with understanding ( int, char, long, short, signed, unsigned etc.... )
# 1  
Old 06-21-2011
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.
# 2  
Old 06-21-2011
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.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 06-22-2011
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.
# 4  
Old 06-22-2011
Quote:
Is it the Size* of int, char, long, short depend on my system?
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.
This User Gave Thanks to fpmurphy For This Post:
# 5  
Old 06-22-2011
Quote:
Originally Posted by cpp_beginner
Can I just based on /usr/include/limits.h to identify whether I should use int, char, long, short, signed, unsigned, etc?
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:
Code:
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.

Last edited by Corona688; 06-22-2011 at 12:35 PM..
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 06-23-2011
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.
# 7  
Old 07-06-2011
many thanks, fpmurphy.
I understand the difference between all the basic variables now Smilie

---------- 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 Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

Unsigned to signed, error?...

Hi guys... Macbook Pro, 13", circa August 2012, OSX 10.7.5, default bash terminal. I require the capability to convert +32767 to -32768 into signed hex words... The example piece code below works perfectly except... #/bin/bash # sign.sh # Unsign to sign... while true do # I have used... (2 Replies)
Discussion started by: wisecracker
2 Replies

2. Programming

Signed and unsigned intergers

when a date type is considered signed and unsigned is that simple referring to - for signed and positive numbers for unsigned? Further if that is the case would mutiplying and dividing ect where 2 signed numbers, like (-2)*(-2) = 4 result in a unsigned. (3 Replies)
Discussion started by: Fingerz
3 Replies

3. Programming

Comparing unsigned char bits.

/******************************************************************************/ /* Printing an unsigned character in bits */ #include <stdio.h> void display_bits ( unsigned char ); int main() { unsigned char x; /*... (15 Replies)
Discussion started by: robin_simple
15 Replies

4. Programming

conversion to 'char' from 'int' warning

Hi, I wrote a simple code in C++ converting from UpperToLower case characters. However, my compiler gives me a warning: "warning: conversion to 'char' from 'int' may alter its value". Any tips? I would like to stress, I don't want to load my string into char array. int ToLower(string... (4 Replies)
Discussion started by: kajolo
4 Replies

5. Shell Programming and Scripting

add signed and unsigned numbers- awk help

Hi All, I have written the below to add the numbers in a column. Postive numbers are unsigned and negative numbers are signed in the file. After the below cmd I am getting -0.00 , instead of 0.00. Can someone guide me on what I am missing in the cmd. grep '^L' $FileName| awk -F"|" ' {... (7 Replies)
Discussion started by: gsjdrr
7 Replies

6. Shell Programming and Scripting

using getopt for both short and long options

Hi , I am using getopt for both short and long options as below SHORTOPTS="a:c" LONGOPTS="alpha:,charlie" OPTS=$(getopt -o $SHORTOPTS --longoptions $LONGOPTS -n "$progname" -- "$@") eval set -- "$OPTS" while ; do case $1 in -a|--alpha) echo "-a or --alpha... (1 Reply)
Discussion started by: padmisri
1 Replies

7. Red Hat

cast from const void* to unsigned int loses precision

Hello everey one, here i am attempting to compile a c++ project .it's throughing the following errors. my machine details are as follows: Linux chmclozr0119 2.6.18-53.el5 #1 SMP Wed Oct 10 16:34:19 EDT 2007 x86_64 x86_64 x86_64 GNU/Linux errors: ===== Generating... (0 Replies)
Discussion started by: mannam srinivas
0 Replies

8. UNIX for Dummies Questions & Answers

int open(const char *pathname, int flags, mode_t mode) doubt...

hello everybody! I want to create a file with permissions for read, write, and execute to everybody using C, so I write this code: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(){ int fileDescriptor; fileDescriptor =... (2 Replies)
Discussion started by: csnmgeek
2 Replies

9. Programming

to get the correct value with unsigned int

hi, Please help me with the following code to get the difference in values. struct a{ int b1; int c1; char d1; } main() { unsigned int b=10; unsigned int c; c = b - (unsigned int )sizeof(a); printf("%d",c); } Here c returns some junk value. How can i get the... (2 Replies)
Discussion started by: naan
2 Replies

10. Programming

Unsigned int

How can I store and/or print() a number that is larger than 4 294 967 295 in C? is int64_t or u_int64_t what I need ? if, so how can I printf it to stdout? (2 Replies)
Discussion started by: nimnod
2 Replies
Login or Register to Ask a Question