The difference between Char and signed char, unsigned char

Source: Internet
Author: User
First, start

One of the troubling questions today is the difference between Char and signed char, unsigned char. between two or three people

1.ANSI C provides 3 character types, namely Char, signed char, and unsigned char. Instead of just two types like short and int (int by default is unsigned int).

2. All three of them are 1 bytes.

3.signed char value range is 128 to 127 (signed bit) unsigned char range is 0 to 255

4. In memory a string of binary, its meaning, is this type to explain.

5. The so-called signed char and unsigned char are actually relative "operations" and have been separated from our literal "character", which means a limited range. Third, Char

definition of 1.char

C standard for Char is impementation Defined, is not clearly defined

(1) that it is defined by what. Frankly, a specific compiler explicitly defines a char by using signed char or unsigned char, which means that different compilers do not define what char is.

(2) Why should this be defined. Because these three types of objects in the storage medium in the form of the same (are all a 8bit of 01 strings, but the resolution is different)

(3) Whether it is signed char or unsigned char. It depends on the compiler: the VC compiler, GCC on x86, defines Char as signed char, while ARM-LINUX-GCC defines char as unsigned char

Use of 2.char

The different uses of Char gave it a different literal meaning

(1) When the word used

The original ASCII standard, the definition of the character code value is only 0~127, so how to define the char is just good to install, so in general, we understand it is that it represents a character , that is, to represent an ASCII (which is also the basis of the C language to explain it)

(2) When a single byte integer

In essence, characters (' A ', ' B ', ' C ', etc.) are essentially an integer, except that the value represented by the character is 0~127, and we can use char to represent a less-than-large integer Four, about high -level expansion

Description: We now default to VC this set of char is defined as signed Char

First in memory, Char and unsigned char is no different, is a byte, 8 bit, ordinary assignment, read and write files and network Word throttling are no difference, anyway is a byte, no matter what the highest bit, the final reading results are the same, but how do you understand the highest level.

Well, we think that the highest bit of char now is the sign bit, so char can represent -128~127, unsigned char has no sign bit, so it can represent 0~255.

But the display on the screen may not be the same. v. Examples of Practice:

#include <stdio.h>

void f (unsigned char v)

{

char c = v;

unsigned char UC = v;

unsigned int a = c, b = UC;

int i = c, j = UC;

printf ("----------------\ n");

printf ("%%c:%c,%c\n", C, UC);

printf ("%%x:%x,%x\n", C, UC);

printf ("%%u:%u,%u\n", A, b);

printf ("%%d:%d,%d\n", I, j);

}

int main (int argc, char *argv[])

{

F (0x80);

f (0x7F); 

return 0;

}
Output:

%c:?,? %X:FFFFFF80,%u:4294967168, 128%d: -128, 128----------------%c:,%x:7f, 7F%u:127, 127%d:127, 127----------- -----

So, in programming, we should also pay attention to some problems caused by unsigned

vi. Statement of reference

In the process of the article to learn a lot of other sites of work, now to link

Http://wenku.baidu.com/view/b9c8f705eff9aef8951e0607.html

Http://www.360doc.com/content/11/0501/16/1317564_113560310.shtml

Http://zhidao.baidu.com/question/63820510.html

Http://www.360doc.com/content/11/0407/20/2200926_108011723.shtml


This article links

For this article you can reprint, deduce, or for commercial purposes, but want to retain the source and ensure integrity, do not use incomplete things to mislead others.

Http://cppkey.com

Http://pppboy.blog.163.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.