char,unsigned char,uint8_t,uint16_t

Posted by VBAssassin on Tue, 10 Mar 2020 04:40:27 +0100

Article directory

char,unsigned char,uint8_t,uint16_t

Summary

  • Basics

In C/C + +, char is used to define character type variables, accounting for only one byte (8 bits).

In C language such as int, long, short and so on, if you do not specify signed or unsigned, the default is signed, but char is not specified as signed or unsigned in the standard. The compiler can be compiled as signed or unsigned.

But I feel as if most of them are signed without specifying signed or unsigned.

If it is signed, its value range is - 128 ~ + 127 (- 27 ~ 27-1), the symbol occupies 1 bit, others occupy 7 bits.

unsigned char means unsigned, with the range of 0 ~ 255 (0 ~ 2 ^ 8-1)

  • Range of values

signed char: -2^7 ~ 2^7-1 unsigned char : 0 ~ 2^8-1

Pow (2,8), a function to calculate the power

  • Writing method
char *a = "abcd";//No warning

signed char *b = "abcd";//Compiler warning "initializing 'signed char *' with an expression of type 'char [5]' converts between points to integer types with different sign"

unsigned char *c = "abcd"; //Compiler warning "initializing 'unsigned char *' with an expression of type 'char [5]' converts between points to integer types with different sign"

The compilation warning is because * b and * c use signed and unsigned respectively to modify the data of type char as integer, and then assign the string of char, so the warning is given

  • Character array
char string[] = "abcd";
NSLog(@"%s",string);
  • The method of judging the compiler's default char symbol

The method of judging the compiler's default char symbol( This method comes from Baidu Encyclopedia)

char c = -1;
if(c < 200){  
    printf("signed\n");
}
else{
    printf("unsigned\n");
}

My compiler alarms. In line 2, "Comparison of constant 200 with expression of type 'char' is always true", Xcode's Clang compiler is signed by default, so always true.

  • cont char
char string[] = "abc";
const char *run = string;

Printing uses% s.

  • unsigned char
unsigned char uc = 100;
NSLog(@"uc %c",uc);  // -> d
NSString *str = [NSString stringWithFormat:@"%c",uc];
NSLog(@"str %@",str); // -> d

The definition in the first line is equivalent to

unsigned char uc = 'd';

Printing uses% c.

Turn NSString

char -> NSString

//Define char type characters
char *a = "abcd";
//Print character a
NSLog(@"char %s",a);
//Convert char type to NSString type
NSString *str = [NSString stringWithFormat:@"%s",a];
NSLog(@"string : %@",str);

char[] -> NSString

char string[] = "abcd";
NSLog(@"%s",string);
NSString *str = [NSString stringWithFormat:@"%s",string];
NSLog(@"string : %@",str);
  • How to use NSString

[NSString stringWithUTF8String:(nonnull const char *)]

perhaps

[NSString stringWithCString:(nonnull const char *) encoding:(NSStringEncoding)]

The input parameters are of const char * type, and the return values are of NSString type.

char string[] = "abcd";
NSString *str = [NSString stringWithUTF8String:string];
NSLog(@"string : %@",str);
char *a = "abcd";
NSString *str = [NSString stringWithUTF8String:a];
NSLog(@"string : %@",str);

uint8_t,uint16_t

  • uint8_t

The definition in iOS is as follows:

#ifndef _UINT8_T
#define _UINT8_T
typedef unsigned char uint8_t;
#endif /* _UINT8_T */

typedef unsigned char, which defines uint8 Φ t as an unsigned int integer.

In essence, it is an integer with a value range of 0-225.

  • uint16_t

Definition in iOS:

#ifndef _UINT16_T
#define _UINT16_T
typedef unsigned short uint16_t;
#endif /* _UINT16_T */

Of type unsigned short.

On the range of value

1 byte = 8 bits (stored in binary), understanding the relationship between bytes and bits helps to understand the storage of data in the computer.

The 32-bit operating system corresponds to 4 bytes and the 64 bit corresponds to 8 bytes. That is to say, on the 32-bit operating system, the computer allocates 4 bytes of memory to store a data.

Because the number of bytes in memory is limited, the data stored is limited, so different types of data are stored in a range on the computer.

char

Unsigned value range: binary is 00000000 ~ 11111111, with 8-bit binary data storage

The value range of char is 0 ~ 255 (0 ~ 2 ^ 8-1)

The range of signed value: - 2 ^ 7 ~ (2 ^ 7) - 1 is - 128 ~ 127, and the sign occupies one place.

int

  • Signed 32-bit int

Signed 32-bit int integers range from - 2147483648 (- 2 ^ 31) to 2147483647 (2 ^ 31-1)

int max = pow(2, 31)-1; // -> 2147483647
int min = pow(-2, 31);  // -> -2147483648
NSLog(@"%d~%d",min,max);

My computer is 32-bit, and when the calculation is out of range, the value returned is the minimum.

int max = pow(2, 100); // -> -2147483648
int min = pow(-2, 100);  // -> -2147483648
NSLog(@"%d~%d",min,max);

pow() must have been processed in this function, so the program does not flash back directly.

Recommended reading

  • Unsigned 32-bit int

0 ~ 2^32-1

Calculation method of value range

sizeof()

int result_int = sizeof(int);
NSLog(@"%d",result_int);// -> 4

int result_f = sizeof(float);
NSLog(@"%d",result_f);// -> 4

int result_char = sizeof(char);
NSLog(@"%d",result_char);// -> 1

int result_uint8_t = sizeof(uint8_t);
NSLog(@"%d",result_uint8_t);// -> 1

int result_uint16_t = sizeof(uint16_t);
NSLog(@"%d",result_uint16_t);// -> 2

int result_uint32_t = sizeof(uint32_t);
NSLog(@"%d",result_uint32_t);// -> 4

int result_uint64_t = sizeof(uint64_t);
NSLog(@"%d",result_uint64_t);// -> 8

1 byte = 8 bits

Some system macros

iOS provides some macros that can directly get the value range of some data types

/* C90/99 5.2.4.2.1 */
#define SCHAR_MAX __SCHAR_MAX__
#define SHRT_MAX  __SHRT_MAX__
#define INT_MAX   __INT_MAX__
#define LONG_MAX  __LONG_MAX__

#define SCHAR_MIN (-__SCHAR_MAX__-1)
#define SHRT_MIN  (-__SHRT_MAX__ -1)
#define INT_MIN   (-__INT_MAX__  -1)
#define LONG_MIN  (-__LONG_MAX__ -1L)

#define UCHAR_MAX (__SCHAR_MAX__*2  +1)
#define USHRT_MAX (__SHRT_MAX__ *2  +1)
#define UINT_MAX  (__INT_MAX__  *2U +1U)
#define ULONG_MAX (__LONG_MAX__ *2UL+1UL)

#ifndef MB_LEN_MAX
#define MB_LEN_MAX 1
#endif

#define CHAR_BIT  __CHAR_BIT__

#ifdef __CHAR_UNSIGNED__  /* -funsigned-char */
#define CHAR_MIN 0
#define CHAR_MAX UCHAR_MAX
#else
#define CHAR_MIN SCHAR_MIN
#define CHAR_MAX __SCHAR_MAX__
#endif

/* C99 5.2.4.2.1: Added long long.
   C++11 18.3.3.2: same contents as the Standard C Library header <limits.h>.
 */
#if __STDC_VERSION__ >= 199901L || __cplusplus >= 201103L

#undef  LLONG_MIN
#undef  LLONG_MAX
#undef  ULLONG_MAX

#define LLONG_MAX  __LONG_LONG_MAX__
#define LLONG_MIN  (-__LONG_LONG_MAX__-1LL)
#define ULLONG_MAX (__LONG_LONG_MAX__*2ULL+1ULL)
#endif

Some macros about int

/* 7.18.2.1 Limits of exact-width integer types */
#define INT8_MAX         127          
#define INT16_MAX        32767         
#Define int 32_max 2147483647 / / the maximum value of signed 32-bit int data
#define INT64_MAX        9223372036854775807LL 

#define INT8_MIN          -128           
#define INT16_MIN         -32768         
   /*
      Note:  the literal "most negative int" cannot be written in C --
      the rules in the standard (section 6.4.4.1 in C99) will give it
      an unsigned type, so INT32_MIN (and the most negative member of
      any larger signed type) must be written via a constant expression.
   */
#Define int 32 min (- int32 MAX-1) / / the minimum value of signed 32-bit int data
#define INT64_MIN        (-INT64_MAX-1)    

#define UINT8_MAX         255
#define UINT16_MAX        65535
#Define uint32? Max 4294967295u / / the maximum value of unsigned 32-bit int data
#define UINT64_MAX        18446744073709551615ULL

/* 7.18.2.2 Limits of minimum-width integer types */
#define INT_LEAST8_MIN    INT8_MIN
#define INT_LEAST16_MIN   INT16_MIN
#define INT_LEAST32_MIN   INT32_MIN
#define INT_LEAST64_MIN   INT64_MIN

#define INT_LEAST8_MAX    INT8_MAX
#define INT_LEAST16_MAX   INT16_MAX
#define INT_LEAST32_MAX   INT32_MAX
#define INT_LEAST64_MAX   INT64_MAX

#define UINT_LEAST8_MAX   UINT8_MAX
#define UINT_LEAST16_MAX  UINT16_MAX
#define UINT_LEAST32_MAX  UINT32_MAX
#define UINT_LEAST64_MAX  UINT64_MAX

/* 7.18.2.3 Limits of fastest minimum-width integer types */
#define INT_FAST8_MIN     INT8_MIN
#define INT_FAST16_MIN    INT16_MIN
#define INT_FAST32_MIN    INT32_MIN
#define INT_FAST64_MIN    INT64_MIN

#define INT_FAST8_MAX     INT8_MAX
#define INT_FAST16_MAX    INT16_MAX
#define INT_FAST32_MAX    INT32_MAX
#define INT_FAST64_MAX    INT64_MAX

#define UINT_FAST8_MAX    UINT8_MAX
#define UINT_FAST16_MAX   UINT16_MAX
#define UINT_FAST32_MAX   UINT32_MAX
#define UINT_FAST64_MAX   UINT64_MAX

/* 7.18.2.4 Limits of integer types capable of holding object pointers */

#if __WORDSIZE == 64
#define INTPTR_MAX        9223372036854775807L
#else
#define INTPTR_MAX        2147483647L
#endif
#define INTPTR_MIN        (-INTPTR_MAX-1)

#if __WORDSIZE == 64
#define UINTPTR_MAX       18446744073709551615UL
#else
#define UINTPTR_MAX       4294967295UL
#endif

/* 7.18.2.5 Limits of greatest-width integer types */
#define INTMAX_MIN        INT64_MIN
#define INTMAX_MAX        INT64_MAX

#define UINTMAX_MAX       UINT64_MAX

/* 7.18.3 "Other" */
#if __WORDSIZE == 64
#define PTRDIFF_MIN	  INT64_MIN
#define PTRDIFF_MAX	  INT64_MAX
#else
#define PTRDIFF_MIN       INT32_MIN
#define PTRDIFF_MAX       INT32_MAX
#endif
//Signed int data value range (32-bit operating system)
NSLog(@"%d~%d",INT_MIN,INT_MAX);                 // -> -2147483648~2147483647
NSLog(@"%d~%d",INT_LEAST32_MIN,INT_LEAST32_MAX); // -> -2147483648~2147483647


//Unsigned 32-bit int maximum, i.e. 2 ^ 32-1
NSLog(@"%u",UINT32_MAX);                        // -> 4294967295
NSLog(@"%f",pow(2, 32)-1);                      // -> 4294967295.000000

Recommended reading

C language data type value range

144 original articles published, praised, visited 70000+
Private letter follow

Topics: iOS C xcode encoding