Second learning of C language

Posted by obscurr on Fri, 21 Jan 2022 18:19:46 +0100

bool data type

true and false are not defined in C language

C language uses 0 to represent false and non-0 (more than 1) to represent true

Several common data types represent ranges

int:10^9

long:10^9

long long:10^18

float: it accurately represents 6 digits after the decimal point, and errors may occur after 6 digits

double:15 bits

Note:% f default%. 6f

Some notes on floating point numbers

1) Compare two floating point numbers

If both floating-point numbers are directly assigned by Changshu, there will be no error in the comparison

#include <stdio.h>
int main(){
	float a=2.7+2.6,b=5.3; 
	if(a==b){
		printf("equal"); 
	}else{
		printf("Unequal"); 
	}
	return 0;
}

2) Division of floating point numbers

Divide two integers, and the result is the result of division

If there are floating-point numbers involved in the operation, the result is accurate

#include <stdio.h>
int main(){
	int a=5,b=2; 
	float c=a*1.0/b;
	float d=a/b;
	printf("%f %f",c,d); 
	return 0;
}

3) Automatic type conversion

In the computer, the operands must have the same size (eg:char and int) and storage mode (integer: original code, inverse code and complement) (decimal: IEEE754 standard), which cannot be calculated.

In C language, different types appear in the expression for operation, and the lower type will be converted to the higher type (that is, the type with less bytes will be converted to the type with more bytes)

When there is no double type in the operand, it is converted to integer

#include <stdio.h>
int main(){
	int a=5,b; 
	char c='a';
	short d=1;
	b=a+c+d;
	printf("%d",b); 
	return 0;
}

If there is double type, it will be changed to double type

#include <stdio.h>
int main(){
	int a=5; 
	char c='A';
	double d=1.0;
	d=a+c+d;
	printf("%d",sizeof(d)); 
	return 0;
}

If the left and right types are inconsistent during assignment, the type on the right will be changed to the type on the left

Increase accuracy:

double a;

a=5.0;

Reduced accuracy:

int a;

a=1.567890123;

Program error:

If the right value exceeds the left value, the result may be meaningless

Forced type conversion

#include <stdio.h>
int main(){
	float a;
	a=1.567890123;
	printf("%d",(int)a);
	return 0;
}

Expression evaluation

Concept: a combination of a series of operators and operators used to calculate a value

Operator: + - */

Operators: values participating in operations

operator

Self increasing and self decreasing operator

a++:a=a+1

a--:a=a-1

Binary operator

+= -= *=

a+=1:a=a+1

a-=1: a=a-1

a*=1:a=a*1

Relational operator

> < =

priority

C language operator priority

judge

if

If the conditions are established, the procedure shall be implemented; if not, the next article shall be implemented

Format:

If (judgment){

Condition holds: executed statement

}

else: otherwise

#include <stdio.h>
int main(){
	if(1>2){
		printf("0");
	}else{
		printf("1");
	}
	return 0;
}

Logical operator priority

From left to right

#include <stdio.h>
int main(){
	if(6>5>4){
		printf("yes");
	}
	return 0;
}

>< priority greater than=

#include <stdio.h>
int main(){
	if(5>3==6>4){
		printf("yes");
	}
	return 0;
}

if nesting

#include <stdio.h>
int main(){
	if(1==1 && 2==2){
		printf("yes\n");
	}
	if(1==1){
		if(2==2){
			printf("yes");
		}
	}
	return 0;
}

&&: and

switch case

loop

for loop

Format: for (judgment){

Circulatory body

}

For example, the sum of 1 ~ 100

#include <stdio.h>
int main()
{
	int sum=0;
	int i;
	for(i=0;i<=100;i++){
		sum+=i;
	}
	printf("%d",sum);
	return 0;
}

i=0; If I < = 100, execute the loop body, and then I + +, if I < = 100, execute the loop body Until I < = 100 is not established, execute the next procedure.

while Loop

Format:

While (condition){

Circulatory body

}

If the maximum common divisor is found

#include <stdio.h>
int main()
{
	int a,b;
	scanf("%d %d",&a,&b);
	int t;
	while(b!=0){
		t=a%b;
		a=b;
		b=t;
	}
	printf("%d",a);
	return 0;
}

do while loop

Format:

do {

Circulatory body

}While (condition)

Execute a cycle first, and then judge whether the condition is true. If it is true, continue the cycle, otherwise enter the next program.

Topics: C