C language Trap (1)---a trap for assigning a value of char to int

Source: Internet
Author: User

As a novice C (although the first language of learning is C, but the actual development of C project is the most recent thing), the use of C process encountered in various types of problems, doubts, knowledge loopholes to make up is undoubtedly very necessary, so decided to each encounter knowledge of the vulnerability to write to the blog.

Today, in the process of writing code, a function is reconstructed, the function is to print a piece of memory content into a 16-binary representation of the string; The sad input is a char pointer: char* buffer; when calling format ("%02x", * Buffer), there is a problem.

For example: 0xb0 output has become: "FFFFFFB0", and finally found that the curse of char;

Whether char is unsigned char or signed char is platform-dependent , and in my platform, char is signed by default, what is the difference between (signed) Char and unsigned char?

Char notation can represent -128~127, unsigned char has no sign bit, can represent 0~255, and is essentially a 8-bit number.

But if we want to represent byte (c itself does not have a byte type), we should use unsigned char, which is why?

Because when an int is assigned with char, the system considers the highest level to be the sign bit, and the int may be 16 or 32 bits, then the top bit is extended (note that the assignment to unsigned int also expands)

And if it is unsigned char, then it will not expand.

This is the biggest difference between the two.

In the same vein, other types can be deduced, such as short and unsigned short. Wait a minute

Let's use examples to illustrate the problem:

#include"StdAfx.h"
#include <stdio.h>

void Test (unsignedChar v)
{
char C = v;
Unsignedchar UC = V;
Unsignedint a = c, b = UC;
int i = c, j = UC;
printf"----------------\ n ");
printf (%%c:%c,%c\n printf (%%x:%x,%x\n printf ( "%%u:%u,%u\n printf (%%d:%d,%d\n }

int Main (int argc, char* argv[])
{
Test (0xb0);
Test (0x68);
return 0;
}

Operation Result:

So, if it is the case of byte, it is recommended to use unsigned char, of course, if you want to use char, plus & 0xFF can solve the problem.

C language Trap (1)---a trap for assigning a value of char to int

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.