2, Data type operators and expressions

1, Data and presentation

(1) Data:

(2) Data representation:

1. Constant:

1.1. Constant concept:

1.2. Code means:

#include <stdio.h>

#define PI 3.14 / / no equal sign, no semicolon, essentially constant
 
int main(){
	
	//const int  
	printf("%d",100);
	
	//Line feed 
	printf("\n");
	
	//Real constant
	printf("%lf\n",3.14);//\n or line feed. This method is used more 
	
	//Character constant: enclosed by ''
	printf("%c\n",'c');
	
	//String constant, enclosed by ""
	printf("%s\n","china"); 
	
	//Symbolic constant, defined with #define, see the third line 
	printf("%lf\n",PI); 
	
	return 0; 
}

1.3. Constant precautions:

1.3.1. Specific use of escape characters:
printf("abc\n");//\n line feed
	printf("abc\td");//\t tab, c followed by 4 empty spaces for d 

2. Variables:

2.1. Identifier:

2.2. Variable concept:

2.3. Essence of variables:

The space allocated by the system. The size of the space is determined by the data type

2.4. Characteristics of variables:

1. The value can be changed during operation, and the system allocates space according to the data type
II. Memory space can be accessed by variable name or address

3. Constant variable:

3.1. Concept:

It is essentially a variable, but the value cannot be changed

3.2. Format:

Add const before the variable:

const int a = 1;

(3) Precautions:

2, Base system

(1) Definition:

(2) Decimal to other decimal:

(3) Binary octal hexadecimal conversion:

(4) Hexadecimal representation in C language:

	//Decimal output
	printf("%d\n",25);//Output result: 25 
	
	//Octal output
	printf("%d\n",025);//Output result: 21 
	printf("%o\n",025);//Output result: 25
	
	//Hexadecimal output
	printf("%d\n",0x25);//Output result: 37 
		printf("%x\n",0x25);//Output result: 25

(5) How to store values in the computer:

1. Source code:


matters needing attention:

2. Inverse code:


matters needing attention:

3. Complement:


matters needing attention:

4.C language practice:

//For computers, binary octal hexadecimal input is a complement 	
	char a = 0x85;
	/*
	Complement: 1000 0101
	Original code: 1111 1011 [the complement symbol bit remains unchanged, and the other bits are taken as inverse plus one] 
	*/ 
	
	//Output a in decimal
	printf("%d\n",a); 
	
	//For the user, the decimal input is the original code
	 char b = -15;
	 /*
	 Original code: 1000 1111
	 Inverse code: 1111 0000
	 Complement: 1111 0001 
	 */
	 
	 //Output b in hexadecimal
	 printf("%x\n",b); 
	  

3, Basic operators and expressions

(1) sizeof operator:

1. Definition and concept:

//Note:% d indicates signed output 	% u indicates unsigned output
	 printf("%u\n",sizeof(int)); //4

4, Data type

(1) Basic data type:

1. Integer:

1.1. Integer classification:


Note: one byte = 8 bits, that is, 1b = 8bit, 1KB = 1024b

1.2. Common print formats for integer:


Note that d needs to be added after% L and% ll in the above table, otherwise it cannot be output. The writing method of series programs can be seen

1.3.C language demonstration:

//Integer definition and output [default] 
	 int a = 123;
	 printf("%d\n",a);
	 
	 //Short integer definition and output
	 short int b = 2;
	 printf("%hd\n",b); 
	 
	 //Long integer definition and output
	 long int c = 333;
	 printf("%ld\n",c); 
	 
	 //Double long integer definition and output
	 long long int d = 3333333;
	 printf("%lld\n",d); 

1.4. Difference between signed and unsigned numbers:

Note:
1. The highest bit of a signed number is the sign bit, and the highest bit of an unsigned number is the value bit
2. Signed bits are output with% d, and unsigned bits are output with% u
3. Unsigned bits can only represent positive numbers
4. If the unsigned bit is output with% d, the machine considers it to be a signed number and can output a positive number

1.5. Value range of signed and unsigned numbers:

2. Character type:

2.1. Concept:

2.2.C language demonstration:

char a = 'a';
	//Output using% c 
	printf("%c\n",a);
	//ASCII value of output a
	printf("%d\n",a);
	return 0; 

Character type is essentially a byte integer, and its integer corresponds to ASCII value
Both characters and integers can be used for assignment, which is equivalent
a=97 A=65 difference between uppercase and lowercase 32
Lower case to upper case - 32 upper case to lower case + 32

char a = 'a';
	printf("%c\n",a);//Output a
	printf("%c\n",97);//Output a 
	printf("%c\n",a-32); //Output A
	printf("%c\n",97-32);//Output A 

2.3. Value range of char:

//The value range of char type is - 128 ~ 127
	char c = 128;//Out of range
	printf("%d\n",c);//Output - 128
	//Why is this the result? 

3. Floating point type:

3.1. Concept:

3.2.C language example:

//Define a float type
	float f = 3.14f;
	printf("%f\n",f);// 3.140000
	//Define a double type
	double d = 5.33;
	printf("%lf\n",d);//5.330000

The format of float type is% f, and the number is followed by f
The format of double type is% lf, and there is nothing after the number

4. String constant:

4.1. Concept:

4.2. String output function printf:

4.2.1. General output:
int a = 10;
printf("%d\n",a);
4.2.2. Output string:
//String output
	printf("country =%s\n","china");//country =china
	printf("china");//china

Note:

/*There are decimals 3.12345678, please output 3.12*/
	//%m.n:m is the number of output digits, and n is the number of decimal digits
	double d = 3.12345678;
	printf("%4.2lf\n",d);//Output: 3.12, [note] decimal point also occupies one place 
4.2.3. Input string scanf:

int a;
printf("Please accept an integer:");
scanf("%d",&a);
printf("%d\n",a);
4.2.4. Input / output function of characters putchar (output) getchat (input):
char sex; 
	printf("Please enter gender and answer printout:"); 
	sex = getchar();
	putchar(sex);
	putchar('\n');//Line feed

getchar() and putchar () can only input and output one character. Entering "f" can print, but entering "male" cannot print

5, Operators and expressions:

(1) Operator:

1. Classification of common operators:

2. Arithmetic operator:

3. Self increasing and self decreasing operators:

int a = 3;
int b = 0;
b = a ++;
printf("%d\n%d\n",a,b);//b = 3 a = 4: assign value first and then add

If a=3, b=0; A + +, b= a, find the value of a and B: A is added to 4 and then assigned to B, then a=b=4

4. Comparison operator:

5. Logical operators:

6. Operator priority:

7. Cast operator:

6, Real exercise:


Range of short: - 32768~ 32767, range of unsigned short: 0 ~ 65535
Out of range when running to 32768, [cycle in] = 65535-32767 = 32768=

Keywords: C

Added by phppaper on Thu, 20 Jan 2022 21:13:41 +0200