C language_ Chapter 1. C language data types and statements

Chapter I data types and statements of C language

1.1 my first C language program

#include <stdio.h> 	//	 The header file contains stdio. H, which is the standard input and output header file of C language
int main()	//	Every complete C program has and has only one main function, and the C program is executed from main
{	//	In C language, () [] {} '' '' must appear in pairs
    printf("Hello world\n");
    /*
    	printf It is the standard output of C language, and the header file is stdio.h
    	\n Represents line feed
    	C Each language statement should end with a semicolon
    	C Only half angle characters are allowed for languages other than ''
    	include It means to load the file and copy the following file stdio.h to the current file
		<> From the system directory
		"" Find the path you choose first. If the path you choose cannot be found, find it in the system directory
    */
    return 0;	//	Function return value
}

//C language comments are divided into line comments (/ /) and block comments (/ * * /)
//Line comment will comment the content after / / the line
//Block comments annotate everything between / * and * /

1.2 keywords

1.2.1 keywords related to data types

Keyword concept: used to define variable types and indicate the memory occupied by variables.

  1. char

Character type variable, accounting for 1 byte in memory.

char ch01 = 'a';	//	Assign the ASCII value of character a to the variable ch01 of type char
/*
	C The language requires each variable to have one and only one data type
	= Is the assignment number
	ASCII The code mainly stores English letters, Arabic numerals and some special symbols in binary form in the computer
*/
char ch02 = '1';	//	C language does not allow the same variable name in the same scope, and it is better not to use the same constant name
char ch03 = '1234';	//	Usage error because the character 1234 does not exist

The computer stores data in binary. Each binary number occupies one bit (b) and each byte (b) occupies 8 bits. 1 K = 1024 B,1 M = 1024 K,1 G = 1024 M.

  1. short(short int)

Short integer, accounting for 2 bytes, which can represent the range of - 32768 ~ 32767 (- 215 ~ 215-1).

short sh01 = 11;		//	Assign the number 11 to sh01
short int sh02 = -333;	//	Assign the number - 333 to sh02
short sh02 = 65535;	 	//	It's out of bounds. It's best not to assign values like this
  1. int

Integer, 4 bytes in 32-bit or 64 bit system and 2 bytes in 16 bit system.

int in = 55;	//	Assign the number 55 to in
  1. long(long int)

Long integer, accounting for 4 bytes in 32-bit system (the number of long bytes is related to the compiler).

long lo = 66;
long int li = 666;
  1. long long(long long int)

Super long integer, accounting for 8 bytes in 32-bit system.

  1. float

Single floating point type, accounting for 4 bytes, with precision of 6 ~ 7.

float fl01 = 3.8f;
float fl02 = 3.8;
  1. double

Double floating point type, accounting for 8 bytes, with precision of 15 ~ 16.

double dou = 5.5;
  1. struct

Structure type, omitted temporarily.

struct st
{
    int a;
    char b;
};
  1. union

Type of joint venture (Consortium), omitted temporarily.

union un
{
    int a;
    float b;
};
  1. enum

Enumeration type, omitted.

enum en {A, B, C};
  1. signed

Signed type (character type, integer type and floating point type are added by default).

signed int a = -1;
int b = -1;
  1. unsigned

Unsigned.

unsigned int a = 2;
unsigned int b = -2;	//	Cross the border
  1. void

Null type, cannot define variable, can be used to decorate pointer, function or return value.

void *str = "abc";	//	void modifier pointer*
void fun(void);		//	The first void modifies the return value, and the second modifies the function entry parameter
  1. C language type classification

number according to class type { base book class type { whole type ( s h o r t   i n t   l o n g ) word symbol type ( c h a r ) real type ( f l o a t   d o u b l e ) structure make class type { Pieces lift ( e n u m ) number group ( i n t   a r r y [ 10 ] ) junction structure body ( s t r u c t ) common use body ( u n i o n ) finger needle class type ( v o i d   ∗ ) empty class type ( v o i d ) Data type \ begin{cases} basic type \ begin{cases} integer (short\ int\ long) \ \ character (char) \ \ real (float\ double) \end{cases} \ \ construction type \ begin{cases} enumeration (enum) \ \ array (int\ arry[10]) \ \ structure \ \ union \ end {cases} \ \ pointer type (void \ *) \ \ null type (void) \end{cases} One of the various types of data data types is the more one one of the the each each one of the the each of the each of the each of the each of the each of the each of the each of the each of the each of the each of the each each of the each of the various types of the data types the data types the data types the please please please please please please one of the each each of the various types of the data types the data types the data types the data types the data types the one one of the each each of the various one of the each of the each of the each of the various types of the each one of the each of the each of the each one of the each one of the each one of the each of the each of the each of the one of the each of the each of the each of the one of the each of the one of the each of the each of the each of the each of the each of the one of the each of the one of the each of the each of the one of the each of the one of the one of the one of the each of the one of the one of the one of the the the⎧ integer (short)   int   long) char (char) float (float)   double) construct type ⎩⎪⎪⎨⎪⎪⎪⎧ enumeration (enum) array (int)   Array [10]) struct union pointer type (void   *) empty type (void)

1.2.2 store related keywords

  1. register

Register, which modifies the register variable, is used to tell the compiler to allocate the storage space of the variable to the register as much as possible.

be careful:

  • Defined variables are not necessarily stored in registers;
  • When the CPU fetches data, it is faster to get data from the register than from the memory;
  • Because registers are valuable, register arrays cannot be defined;
  • register can only modify character type and integer type, not floating point type;
  • Only variables in memory have addresses, so you can't take addresses from register addresses.
register char rch;
register float rfl;	//	error
register int rin;
int *p;		//	Define pointer variable p
p = &rin;	//	Error taking address from register
  1. static

Static, can be used to modify variables and functions, omitted.

static int a;
static int fun(void);
  1. const

Constants. Variables modified with const are read-only variables. C language does not allow assignment to constants and read-only variables.

const int a = 11;
a = 15;	//	error
  1. auto

Auto, all variables are auto by default.

  1. extern

External, generally used for the declaration of functions and global variables, indicating that the functions and variables are external, omitted.

extern int a;

1.2.3 keywords related to control statements

if else break continue for while do switch case goto default

For now.

1.2.4 other keywords

  1. sizeof

The size of storage space for variables, arrays, commons, structures, enumerations, etc.

int a;
sizeof(a);
sizeof(int);
  1. typedef

Rename, rename an existing type.

typedef int moscall_int;
int a;
moscill_int b;	//	A and B are all int types
  1. volatile

Easy to change. Tell the CPU to retrieve the volatile variable from the memory again.

volatile int a = 0;

1.2.5 variables

  1. Naming of variables

In C language, when naming variables and functions, they can only contain numbers, letters and underscores, and numbers cannot start, and variable names cannot be keywords.

int abc_123ds;	//	allow
int _abc123ds;	//	allow
int 1_abc23ds;	//	error
  1. Definition of variables
int a;      //	This is a declaration of variables and does not open up space in memory
int b = 10; //	This is the definition of variables, which opens up space in memory
a = 10;		//	This is the assignment of variables
  1. constant

In C language, the quantity that cannot be changed during program operation is called constant.

10 20;			//	integer constants 
'a';			//	character constant 
"Hello";		//	string constant 
#define MAX 100 	//	 Macro, Max is a constant with a value of 100
 Array name function name		//	A constant cannot be an lvalue, that is, the value to the left of the equal sign (=)
'a' = 10;		//	error
  1. Assignment of variables

In C language, variables are allowed to be assigned, and forced type conversion (forced conversion) may be required for different types of values.

int a = 10;	//	Assign a to 10
int b = a;	//	Assign a to b
double c = 30;
a = (int)c;	//	Strong rotation
b = a = 20;	//	correct
  1. Input and output of variables
typeplaceholder
char%c (integer corresponds to ASCII value → character)
unsigned short%hu
signed short%hd
unsigned int%u
signed int%d
unsigned long%lu
signed long%ld
float%f
double%lf
octal number system%o. % #o (output octal number starting with 0)
hexadecimal%x. % #x (output hexadecimal number starting with 0x)

%X% o% d outputs 32 bits by default

int main()
{
    int in;
    char ch = 0xab;
    scanf("%d", &in);	//	Assign an integer to in
    /*
    	scanf It is the standard input of C language, and the header file is stdio.h
    	scanf Enter the values of one or two or more variables from the keyboard, such as scanf ("% D,% d", & A, & B);
    	& Here it means to get the address, & in means to find the address of in in memory
    */
    printf("in = %d\n", in);
    printf("ch = %x\n", ch);	//	ch = ffffffab
    printf("ch = %#X\n", ch);	//	ch = 0XFFFFFFAB
    return 0;
}

1.3 signed and unsigned numbers

  1. The binary highest bit of a signed number is the sign bit, 0 represents a positive number and 1 represents a negative number;
  2. The binary highest bit of an unsigned number is a numeric bit;
  3. Signed number and unsigned number have the same number and different ranges.
char a = 10;
char b = -10;
unsigned int c = 10;
/*
	a The binary of is 0000 1010, and the highest bit is 0
	b The binary of is 1000 1010, and the highest bit is 1
	a The value range of is 0000 ~ 1111, i.e. - 2 ^ 7 ~ 2 ^ 7 - 1
	c The value range of is 0000 ~ 1111 1111, i.e. 0 ~ 2 ^ 8 - 1
*/
typeRange
signed char-27 ~ 27 - 1
unsigned char0 ~ 28 - 1
signed short-215 ~ 215 - 1
signed short0 ~ 216 - 1
signed int-231 ~ 231 - 1
unsigned int0 ~ 232 - 1

1.4 type conversion

The methods of type conversion include automatic conversion and forced type conversion.

1.4.1 automatic conversion (implicit conversion)

The compiling system automatically completes type conversion according to the rules set by the system.

Conversion rules:

The type with less memory bytes (small value range) is converted to the type with more memory bytes (large value range) to ensure that the accuracy is not reduced and the data is not lost.

Conversion mode:

char and short → int → long → long

signed → unsigned

float → double

1.4.2 forced type conversion:

Cast the operation result of the expression to the required data type.

Conversion mode:

(type) (expression)

int a = 10;
long b;
b = (long)a;
double c = (double)(a + b);	//	Include implicit conversion

Type conversion is a temporary conversion and does not change the type and value of the original data.

1.5 operator

An arithmetic expression that connects operands (operands) and conforms to the syntax rules of C language. Operands can be constants, variables, functions, and so on.

1.5.1 operator classification

According to the number of operands, it can be divided into monocular operator, binocular operator and ternary operator.

By function, it can be divided into arithmetic operators, relational operators, logical operators, bit operators and conditional operators.

1.5.2 arithmetic operators

+ - * / % += -= *= /= %= ++ --

int main()
{
	int a = 10;
	int b = 20;

	printf("a + b = %d\n", a + b);	//	30
	printf("a - b = %d\n", a - b);	//	-10
	printf("a * b = %d\n", a * b);	//	200
	printf("a / b = %d\n", a / b);	//	0% d prints integers and the decimal part will be automatically rounded off
	printf("a % b = %d\n", a % b);	//	10% here is the remainder operation
    
    printf("a += b = %d\n", a += b);	//	30 is equivalent to a = a + b
	printf("a -= b = %d\n", a -= b);	//	10
	printf("a *= b = %d\n", a *= b);	//	200
	printf("a /= b = %d\n", a /= b);	//	10
	printf("a %%= b = %d\n", a %= b);	//	10%% means output a%
    
    printf("++a = %d, a++ = %d\n", ++a, a++);	//	12 10 self addition operation, equivalent to a + 1
    printf("a = %d\n", a);		//	12
    printf("--b = %d\n", --b);	//	19 self subtraction

	return 0;
}
/*
	printf("++a = %d, a++ = %d\n", ++a, a++);
	++ Before, add by yourself first, and then do other processing. After + +, do other processing first, and then add by yourself
	printf Functions may be printed from right to left or from left to right,
	So the final result may be 12, 10, 11 or other answers, which is related to the compiler
	Self subtraction and self addition are similar
*/

/*
	Monocular operator: + +--
	Binocular operator: + - * /% + = - = * = / =%=
*/

1.5.3 relational operators

> < >= <= != ==

int main()
{
	int a = 10;
	int b = 20;
    
	printf("%d\n", a > b);	//	0 does not hold. Return 0
	printf("%d\n", a < b);	//	1 Establishment return 1
	printf("%d\n", a >= b);	//	0
	printf("%d\n", a <= b);	//	1
	printf("%d\n", a != b);	//	1 !=  Represents not equal to
	printf("%d\n", a == b);	//	0 = = means equal to, and = means assignment

	return 0;
}

/*
	Binocular operator: > < > = < =! ===
*/

1.5.4 logical operators

&& || !

int main()
{
	int a = 1;
	int b = 0;
    
	printf("%d\n", a > 0 && b == 0); // 1 is a logical and. It returns 1 only when a > 0 and b == 0 are established at the same time, otherwise it returns 0
	printf("%d\n", a > 0 || b++);    // 1 logical or, returns 1 when at least one of a > 0 and b + + is true, otherwise returns 0
	printf("%d\n", !(a > 0));        // 0 is not logical. When a > 0 is true, it returns 0, otherwise it returns 1
	printf("b = %d\n", b);	         // 0
	
    return 0;
}

/*
	&& When the previous expression is false, the following expression will not be executed, otherwise all expressions will be executed
	|| When the previous expression is true, the following expression will not be executed, otherwise all expressions will be executed
*/
/*
	Monocular operator:!
	Binocular operator: &||
*/

1.5.5 bitwise operators

& | ~ ^ >> <<

int main()
{
	int x = 0x34;	//	0x34 means that 34 is a hexadecimal number
	int y = 0x56;
	//Binary representation of 0011 0100 0x34
	//Binary representation of 0101 0110 0x56
    
	//&By bit and, and operation is performed between the same bits. 0 is 0, otherwise it is 1
	//0001 0100
	printf("%#x\n", x & y);	//	0x14%#x print hexadecimal numbers in 0x format
    
    //|By bit or, or operation is performed between the same bits. 1 is 1, otherwise it is 0
	//0111 0110
	printf("%#x\n", x | y);	//	0x76
    
    //^XOR by bit, XOR operation is performed between the same bits, the same is 0, otherwise it is 1
	//0110  0010
	printf("%#x\n", x ^ y);	//	0x62
    
    //~Negate by bit, negate the number on each bit, 1 becomes 0, and 0 becomes 1
    //1100 1011
	printf("%#x\n", ~x); 	//	 0xffffcb% x will print out 32 bits, and the highest bit is 0, which will be omitted
    
    //< < shift left, high overflow, low fill 0
    //1101 0000
    printf("%#x\n", x << 2);	//	0xd0
    
    //>>Shift right
    /*
    	The right shift is divided into logical right shift and arithmetic right shift. The specific right shift depends on the compiler
    	Logical shift right: low overflow, high complement 0, the result is 0000 1010
    	Arithmetic shift right: for signed numbers, the low bit overflows and the high bit complements the sign bit. The result is 1111 1010
    	0101 0011 >> 3 Logical shift right 0000 1010
    				   Arithmetic shift right 0000 1010
    */
    printf("%#x\n", -0x2d >> 3);	//	0xfffffffa
    
	return 0;
}

/*
	Monocular operator:~
	Binocular operator: & ^ > ><<
*/
/*Determine whether the right shift is a logical right shift or an arithmetic right shift*/
#include <stdio.h>
int main()
{
    printf("%d\n", -1 >> 3);	//	If the result is - 1, the arithmetic shifts to the right
    
    return 0;
}

/*Bit operation usage*/
/*1. Set some locations to 0*/
int main()
{
	int x = 0x34;
	//0011 0100
	//1111 0000
	//0011 0000 = 30
	printf("%#x\n", x & 0xf0);
	return 0;
}

/*2. Set some positions 1*/
int main()
{
	int x = 0x34;
	//0011 0100
	//1111 0000
	//1111 0100 = 0xf4
	printf("%#x\n", x | 0xf0);
	return 0;
}

/*3. Invert some bits*/
int main()
{
	int x = 0x34;
	//0011 0100
	//1111 0000
	//1100 0100 = 0xc4
	printf("%#x\n", x ^ 0xf0);
	return 0;
}

/*4. Exchange the values of two numbers without adding intermediate variables*/
int main()
{
	int a = 10;
	int b = 20;
	a = a ^ b;
	b = a ^ b;
	a = a ^ b;
	printf("a = %d, b = %d\n", a, b);
	return 0;
}

/*5. If you multiply a number by 2^n, you only need to shift n bits to the left*/
int main()
{
	int  a = 0x03;
	printf("%d\n", a << 1);
	printf("%d\n", a << 2);
	printf("%d\n", a << 3);
	printf("%d\n", a << 4);
	return 0;
}

/*6. Set some positions 1, or zero*/
int main()
{
	int  a = 0x053;
	//Set the 4th position to 0, and the other bits remain unchanged
	//0x01 << 4 == ~0001 0000 = 1110 1111
	//0x053 & ~(0x01 << 4)
	//printf("%x\n", a & ~(0x01 << 4));
	//0x01 < < 3 | a sets the third position to 1, and other bits remain unchanged
	printf("%x\n", a | (0x01 << 3));
	return 0;
}

/*7. Judge whether a number is odd or even*/
int main()
{
	int a;
	scanf("%d", &a);	//	Standard input function, the header file is stdio
	//0010 1001 0101
	//0000 0000 0001
	if (a & 0x01)	//	If else statement, used for condition judgment, headless file requirements
	{
		printf("Odd number\n");
	}
	else
	{
		printf("even numbers\n");
	}
	return 0;
}

1.5.6 conditional operators

() ? () : ()

int main()
{
	int a = 1;
	int b = 0;
    
	printf("%d\n", a > b ? a++ : b);	//	1
    /*
    	If a > b is true, execute a + +, otherwise execute B
    */
    
	return 0;
}

/*
	Ternary operator: ()? (): ()
*/

1.5.7 comma operator

,

int main()
{
	int a = 1;
	int b = 0;
	int  c = (a, b, a + b, a++, ++b, a + b);
    
	printf("c = %d\n", c);	//	3 the result of the comma operator is the result of the following expression
    
	return 0;
}

/*
	Binocular operator:,
*/

1.5.8 precedence and associative type of operators

1.6 selection of structure

1.6.1 if statement

int main()
{
	int  a = 5;
	if (a % 5 != 0)	//	You cannot add a semicolon after this
	{
		printf("a Cannot be divided by 5\n");	
	}				//  You can't add a semicolon after it
	if (10 == a)	//	When judging values and variables, in order to prevent writing = = as =, you can consider placing constants on the left
	{
		printf("a = 10\n");
	}
	if (-1)			//	Non 0 is true
    { 
		printf("come in-1\n");
	}
	return 0;
}

/*
	if Statement format: if (A) {}
	A It can be an expression, a variable or a constant,
	If A is true (A! = 0), execute the contents in the braces; otherwise, skip the braces and continue to execute downward
	if (2 == a)
		printf("a = 2\n");	This is actually a statement
*/

1.6.2 if else statement

int  main()
{
	int a = 10;
	if (a % 2 == 0)
	{
		printf("even numbers\n");
	}
	else	//	Sum face cannot be added ()
	{
		printf("Odd number\n");
	}
	return 0;
}

/*
	if-else Statement format: if (a) {} else {}
	If A is true, the first brace is executed; otherwise, the second brace is executed
	else It can only be added after if or else if. There can be no other statements between the first {} and else
	The matching principle is: if or else if statements on it, closest to it and in the same scope
*/
/*
	Scope: the valid range of a variable,, or function
	For example, the variable a of the above code is only valid in the main function, so the scope of a is the main function
	The following codes:
	int main()
	{
		if (1)
		{
			int a = 10;
		}
		
		return 0;
	}
	At this time, the scope of a is within the if statement
	The scope of the function is omitted after the function definition or declaration
*/

1.6.3 if else statement

int  main()
{
	int a = 4;
	if (a % 4 == 0)
	{
		printf("0\n");
	}
	else if (a % 4 == 1)	//	Can only be added after if
	{
		printf("1\n");
	}
	else if (a % 4 == 2)
	{
		printf("2\n");

	}
	else	//	You can have or not else
	{
		printf("3\n");
	}

	return 0;
}

1.6.4 switch statement

int main()
{
	int a = 4;
    
	printf("Enter a 0 ~ 4 Number of: ");
	scanf("%d", &a);
    
	switch (a)	//	a can be an integer character or an expression
	{
	case 0:		//	If a = 0, execute from here, otherwise find the next case
		printf("0\n");
		break;	//	Used to exit a switch or loop statement. break: exit the current scope
	case 1:
		printf("1\n");
		break;
	case 2:		//	Lack of break. When a = 2, the program output result is: 2 'line feed' 3
		printf("2\n");
	case 3:
		printf("3\n");
		break;
	default :	//	If the previous case s fail to match, execute default, which is usually placed at the end of the switch
				//	Or if the program executes its last case and does not exit, it will also execute default
		printf("Input error\n");
	}

	return 0;
}

1.7 cycle control

1.7.1 for statement

int  main()
{
	int sum = 0;
	int i;
    
    /*Calculate the value of 1 + 2 +... + 100*/
	for (i = 1; i <= 100; i++)	//	; represents a statement, so i + + and + + i have no effect on the result
	{
		sum += i;
	}
    
	printf("%d\n", sum);
    
	return 0;
}

/*
	for (a; b; c) { }
	When entering the loop for the first time, execute expression a and then expression b. if b is false, jump out of the for loop,
	If b is true, execute the contents of {}. After {} is executed, execute expression c,
	Then pass the execution result of c to b, and then execute b until b is false and exits, otherwise the loop will continue.
	Expression a is executed only the first time it enters the loop, and then it doesn't matter,
	a b c Can be null, that is, there is for (;;), which means that the judgment condition of the for loop is always true,
	a b c Can be irrelevant. The following cases:
	int main()
	{
		int a = 10;
		int b = 9;
		for(int c = 5; a < 20; b++)
		{ }
	}
*/

1.7.2 while statement

int main()
{
	int sum = 0;
	int i = 1;
    
	while (i <= 100)
	{
		sum += i;
		i++;
	}
    
	printf("%d\n", sum);

    return 0;
}

/*
	while (a) { }
	When a is true, execute the contents of {} and then judge a until a is false and exit
*/

1.7.3 do while statement

int main()
{
	int sum = 0;
	int i = 1;
    
	do
	{
		sum += i;
		i++;
	}
    while (i <= 100);
    
	printf("%d\n", sum);
	return 0;
}

/*
	do { } while (A)
	Execute {} first and then judge. If it is true, continue the cycle. If it is false, exit directly
*/

1.7.4 nested statements

Selection structures and loop structures can be nested with each other or with themselves.

int main()
{
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			printf("i = %d  j = %d\n", i, j);
		}
	}
    
    for (int i = 0; i < 2; i++)
	{
		int j = 0;
		if (i < 1)
        {
            j++;
        }
        else
        {
            j += 2;
        }
        
        printf("j = %d\n");
	}

	return 0;
}

1.8 jump statement

1.8.1 return

End the execution of the current function. Except for the function whose return value is void, return can not be written. All other functions need to write the return value.

#Function return value			#return script
int                  return n;	#n is an integer
void			     return;

#Description of return value of main function
return 0: The program exits normally
return Not 0: Program abnormal exit

1.8.2 continue

Jump out of this loop and can only be used within loops or nested loops.

int main()
{
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			if (1 == j)
				continue;	//	When j == 1, the program will jump directly to j + + instead of executing printf
			printf("i = %d j = %d\n", i, j);	//	The print result will be missing j = 1
		}
	}

	return 0;
}

1.8.3 break

Jump out to the scope where the break is located. It can only be used in loops, nested loops, or switch es.

int main()
{
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			if (1 == j)
				break;	//	When j == 1, the program will directly jump to i + +, instead of executing this for loop
			printf("i = %d j = %d\n", i, j);	//	The printing result will be missing j = 1 and j = 2
		}
	}

	return 0;
}

1.8.4 goto statement

Unconditional jump statement. Goto statements are harmful, especially when bouncing back. It is recommended to reduce the use of goto statements.

int main()
{
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			if (j == 1)
				goto LOOP;	//	When j == 1, jump directly to LOOP instead of executing this nested LOOP
			printf("i = %d j = %d\n", i, j);
		}
	}
LOOP:
	printf("kill\n");
	return 0;
}

1.9 supplement

1.9.1 hex

Concept: carry counting system, a artificially defined counting method with carry.

  • Decimal system: every decimal one, consisting of 0, 1, 2... 9;
  • Octal: every eight into one, consisting of 0, 1, 2... 7. In C language, it starts with 0, such as 023;
  • Hexadecimal: every hexadecimal is one, which is composed of 0 1 2... A B C D E F, where a B C D E F represents 10 11 12 13 14 15 respectively. In C language, it starts with 0x, such as 0xab;
  • Binary: every two into one, composed of 0 and 1. Binary cannot be used in C language.
#==========Binary conversion==========#
1. n Binary to decimal
#method
 Start at the top, The number on each digit is multiplied by n ^ (digit - 1), Add again
#Binary to decimal
1011 ---> 1 * 2 ^ 3 + 0 * 2 ^ 2 + 1 * 2 ^ 1 + 1 * 2 ^ 0 = 11

2. Decimal to n Base system
#Methods: short division
#Decimal to binary
11 ---> 11 / 2 = 5...1
         5 / 2 = 2...1
         2 / 2 = 1...0
         1 / 2 = 0...1
 Read from bottom to top, That is, decimal 11 is converted to binary 1011

3. Octal and binary conversion
#method
 Octal to binary: Each octal bit is converted to three binary bits
 Binary to octal: Every three binary bits are converted to one octal bit
#Octal to binary
76 ---> 7 Convert to binary 111, 6 Convert to binary 110, 76 Convert to binary 111110
#Binary to octal
0111 0101 ---> 01 110 101, 01 Convert to octal to 1, 110 Convert to octal 6, 101 Convert to octal to 5, 0111 0101 Convert to octal 165

4. Hexadecimal and binary conversion
#method
 Hex to binary: Each hexadecimal bit is converted to four binary bits
 Binary to hexadecimal: Every four binary bits are converted to one hexadecimal bit

5. Octal and hexadecimal conversion
#method
 Convert to binary or decimal first, Then convert to the corresponding base

1.9.2 original code, inverse code and complement code

  1. Original code

Concept: a number of the most primitive binary code.

  • Positive number: 10, original code: 0000 1010.
  • Negative number: - 10, original code: 1000 1010.

C language cannot directly input or output binary numbers.

Disadvantages of using original code storage:

  • There are two states for 0
    • +0: 0000 0000
    • -0: 1000 0000
  • Error in positive and negative operation
    • 1000 1111(-15) + 0000 1111(+15) = 1001 1110(-30)
  1. Inverse code
  • Original code of positive number = inverse code;
  • Inverse code of negative number = the sign bit of the original code remains unchanged, and other bits are reversed.

Disadvantages of inverse code:

  • There are two states for 0
    • +0: 0000 0000
    • -0: 1111 1111
  1. Complement
  • Positive number: original code = inverse code = complement code;
  • Negative number: complement = inverse code + 1.

What the computer saves is a complement.

1. Complement source code
   Original code: 1001 0111
   Inverse code: 1110 1000
   Complement: 1110 1001
   Seeking method: Division of complement by sign bit, Other bits are reversed, then +1(You can also start first -1, Reverse again)
        
2. When saving data, If a decimal number is assigned, The original code is stored, If the assignment is a hexadecimal or octal number, What is stored is a complement
        
3. 8 Bit complement to 32-bit complement
   The highest bit is 1, Fill 24 1's in front, The highest bit is 0, Fill 24 zeros in front
   8 Bit complement: 1010 1011
   32 Bit complement: 1111 1111 1111 1111 1111 1111 1010 1011
   Original code: Complement inversion(1000 0000 ... 0000 0101 0100), again +1(1000 ... 0101 0101) = -43

4. Define variable out of bounds
int main()
{
    char a = 127 + 2;	//	char value range: - 128 ~ 127
    //Original code = inverse code = complement code: 1000 0001
    //32-bit complement: 11111111111111111111111111 1000 0001
    //32-bit original code: 10000000 0000000 0111 1111
    printf("%d\n", a);	//	-127
    return 0;
}

1.9.3 ASCII code table


Keywords: C

Added by damien@damosworld.com on Wed, 27 Oct 2021 16:12:54 +0300