| | |
Data Types
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Tipically a char is 8-bit long, and most significant bit(MSB) represent the sign value.
If MSB is 0, it means positive and if MSB is 1, it means negative.
So only last 7 bits (out of 8 bits) are used to store the actule char value. And total number of values will be 2^7 = 128 (0 to 127) or (-128 to 0).
But when we declare a char as unsigned then there is no importance of the sign bit (i.e MSB), So now total value stored in char will be 2^8 = 256 (0 to 255), and this will cover all the char values.
One of important use of unsigned char is in embedded system, to store the value of some 8-bit register.
If MSB is 0, it means positive and if MSB is 1, it means negative.
So only last 7 bits (out of 8 bits) are used to store the actule char value. And total number of values will be 2^7 = 128 (0 to 127) or (-128 to 0).
But when we declare a char as unsigned then there is no importance of the sign bit (i.e MSB), So now total value stored in char will be 2^8 = 256 (0 to 255), and this will cover all the char values.
One of important use of unsigned char is in embedded system, to store the value of some 8-bit register.
>why do programmers benifit from having such a variety?
They can choose the data type that best fits the problem and optimize speed or storage costs.
>Also if anyone could help me out with what an unsigned character is used for?
Off the top of my head: If you don't want negative numbers, if you want to extend the range of signed char but don't want to jump up to short int, if you need the wrap-around qualities of an unsigned type, if you want to store an object in an array of bytes (unsigned char is the only type guaranteed not to have padding bits).
They can choose the data type that best fits the problem and optimize speed or storage costs.
>Also if anyone could help me out with what an unsigned character is used for?
Off the top of my head: If you don't want negative numbers, if you want to extend the range of signed char but don't want to jump up to short int, if you need the wrap-around qualities of an unsigned type, if you want to store an object in an array of bytes (unsigned char is the only type guaranteed not to have padding bits).
I'm here to prove you wrong.
![]() |
Similar Threads
- Data types (C)
- Is there a way(function) to distingusih between different data types? (C++)
- Pls help with C code. Evaluating data types. (C)
- C++ Data Types (C++)
- IIS connection will not write data (ASP)
- Data Abstraction (Computer Science)
Other Threads in the C Forum
- Previous Thread: Validate range from text file
- Next Thread: problem with size_t filesize
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop initialization input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux list lists locate looping lowest matrix meter microsoft number oddnumber opendocumentformat openwebfoundation overwrite owf pdf pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






