C language foundation day2

Posted by trippyd on Wed, 23 Feb 2022 12:32:43 +0100

review

Use of vim editor:

vi file name

vi test. c . C language source file

Key i: enter the input mode and insert appears in the lower left corner

esc key: exit the input mode. The insert in the lower left corner is gone

: wq save and exit vi editor

:q! Force exit without saving

:wq! Force save exit

#include <stdio.h> 
//#Preprocessing the processing done before compilation
//include contains the header file in order to declare
//stdio.h standard I / O header file
//< > find header files in the standard library, which are all < >
//The main function is the main function within {} where the program entry logic starts to execute
//printf output the contents of "" \ nextend character line feed carriage return move the cursor to the beginning of the next line
//%D decimal integer% f floating point number if printf output requires integer or decimal, and the value cannot be determined, it should be determined according to the value of the variable, then write% d where the integer should appear in "". When floating point number appears,% F will replace% D and% f according to the value of the variable during actual output
int main()
{	
    printf("hello world\n");	
    return 0;//End the main function, and end the main function is to end the program.
} 

How to define variables

Type name variable name;

int integer

Float float

char character type

Naming convention (identifier):

  1. Numbers, letters, and underscores.
  2. Cannot start with a number. 1A and 2F are all wrong
  3. Strictly case sensitive. num Num is not a variable, and the case is different
  4. Keywords cannot be used. int float char include return and so on are keywords of C language.
  5. Pinyin cannot be used. Naming should accurately express the meaning.

Variable assignment

=Assignment operator

Assign the value on the right (right value) to the value on the left (left value)

int a;

a = 10;

What is initialization:

int a = 10; Assign values when defining variables. Although the difference is not obvious in C language, assignment and initialization are not the same thing.

Formatter

%d %o %x

%f

%c

Input and output functions:

input

scanf has pits. We learn it mainly because we need input when we do exercises.

\n is a scanf taboo.

output

printf

1. Operator

1. Arithmetic operator

±*/%

± * is the same as the mathematical concept we are familiar with.

Division/

The integer is divided by 10 / 6. The part that cannot be divided by 1 is directly discarded and only the integer part is retained.


The result is 1

Dividing decimals into decimals is the same as the mathematical concept we are familiar with.


The result is 1.6667

Divide an integer by a decimal to get a decimal.

The result of 100 / 13 is 7, and the decimal part is directly rounded off.

Modulus division% remainder. 100% 13 gets 9. It is generally used to judge the division. a%b result 0 indicates that a is divided by b.

%The left and right operands of must be integers.

Division and modulo division require that the right operand cannot be 0. The operation will crash.


The denominator is accidentally 0.


The compilation was correct, but the operation crashed.

2. Relational operators

The result of a relational operator is a true or false value

True non-zero integers are true - 1 100 are true. In relational operators, if the result is true, C language will use 1 to represent true by default.

False integer 0

< < = (no space in the middle) > = (no space in the middle) = = (no space in the middle)= (no space in the middle)

==Judge that two numbers are equal, the equal result is 1, and the unequal result is 0

!= Judge that two numbers are not equal. The unequal result is 1 and the equal result is 0

Note the distinction between = = (only for judgment) and = (for assignment)

Example:

x = 7; Assignment, assign 7 to variable x

x == 5; Compare and judge whether the value of X is 5

int main()
{	
    int a = 10;	//Define variable a, initialize 10
    int b = 3;	//Define variable b, initialize 3
    printf("%d\n", a<b);//Output 0 if a > b? The result is 1	
    return 0;
}

3. Logical operators

&&Logic and number keys 7+shift

||Logic or shift + ENTER

! Logical non

Logic and truth table:

Expression 1 & & expression 2 Result

-------------------------------

0 0 0

1 0 0

0 1 0

1 1 1

Example: a = 2, B = 9; (a>0) && (b<0)

int main()
{	
    int a = 2;	
    int b = 9;	
    printf("%d\n", (a>0)&&(b<0));	//0 if (a > 0) & & (b > 0) 1
    return 0;
}

Logic or truth table:

Result of expression 1 𞓜 expression 2

-------------------------------

0 0 0

1 0 1

0 1 1

1 1 1

Example: x = 2, y = 1; (x>0) || (y<0)

#include <stdio.h>

int main()
{
	int x = 2;
	int y = 1;
	printf("%d\n", (x>0)||(y<0));//1   (x<0)||(y<0) 0
	return 0;
}

Logical non truth table:

a !a

------------------

1 0

0 1

reflection

int main()
{	
    int a = 10;	
    a = !a;//Execution completed a 0
    a = !a;//Execution completed a 1
    printf("%d\n", a);	
    return 0;
}

4. Priority

1 + 2 * 3 calculates multiplication first and then addition

Arithmetic operation > relational operation > logical operation

1+2>3 || 2+3<4

3>3 || 5<4

0 || 0

0

Because logical operators take precedence over relational operators, the () of the code in all the above examples of logical and and logical or can be omitted.

practice:

Enter three decimals, sum and average, and print them respectively

#include <stdio.h>

int main()
{
	float a, b, c;
	scanf("%f%f%f", &a, &b, &c);
	float sum = a+b+c;//Sum and store the result in the variable sum
	float avg = sum/3;//Calculate the average value and store the results in avg
	printf("sum = %f avg = %f\n", sum, avg);
	return 0;
}

2. if statement

The normal program is executed from top to bottom and has a sequential structure.

The if statement belongs to the branch structure and selectively executes a certain section of logic.

1. Standard format:

if( condition ) //Execute statement 1 if the condition is true and execute statement 2 if the condition is false
{
	Statement 1. 
}
else //else does not necessarily exist. According to the actual logic, it can be omitted.
{
	Statement 2.
}

int main()
{
	if(0)//If the condition is false, hehe cannot be output
	{
		printf("hehe\n");
	}
	return 0;
}

Example:

Please enter a number to judge whether it is even.

#include <stdio.h>

int main()
{
	int num;
	scanf("%d", &num);
	if(num%2 == 0)//An integer can be divided by 2, indicating that it is even
	{
		printf("yes\n");
	}
	else
	{
		printf("no\n");
	}
	return 0;
}

practice:

Enter an integer to determine whether it is even. If it is even, determine whether half of the number is even.

#include <stdio.h>

int main()
{
	int num;
	scanf("%d", &num);
	if(num%2 == 0)
	{
		printf("yes\n");
		//The priority is the same on the left
		if(num/2%2 == 0)
		{
			printf("half yes\n");
		}
		else//Each else must have a matching if
		{
			printf("half no\n");
		}
	}
	else
	{
		printf("no\n");
	}
	return 0;
}

2. if nesting

if( Condition 1 ) //Condition 1: true judgment condition 2
{	 
	if( Condition 2 ) //Condition 1 true condition 2 true execution statement 2
	{
		Statement 2.  
	}
	Statement 3.//Condition 1 true execution statement 3 has nothing to do with condition 2
}

3.if parallel

if( Condition 1 )//Condition 1 true execution statement 1
{
	Statement 1.  
}
if( Condition 2 )//Condition 2 true execution statement 2 condition 2 is not affected by condition 1 
{
	Statement 2.  
}

3. If - else multi branch selection structure

if( Condition 1 )//Condition 1 true execution statement 1
{
	Statement 1.
}
else if( Condition 2 ) //If condition 1 is false, condition 2 is judged. If condition 2 is true, execute statement 2
{
	Statement 2.
}
else //Condition 1 and condition 2 are false before executing statement 3 in else. Else can be omitted
{
	Statement 3. 
}

The latter condition is affected by the former condition

Example:

#include <stdio.h>

int main()
{
	int week;
	scanf("%d", &week);
	if( week > 7 || week < 1 )//Ensure that the input number is between 1 and 7, otherwise the program ends
	{
		printf("error\n");
		return 0;//End the main function and end the program
	}
    //Only when week is between 1 and 7 can the following code be executed
	if( week == 1 )//If week is 1, the following conditions will not be judged
	{
		printf("Monday\n");
	}
	else if( week == 2 )
	{
		printf("Tuesday\n");
	}
	else if( week == 3 )
	{
		printf("Wednesday\n");
	}
	else
	{
		printf("other days\n");
	}
    return 0;
}
#include <stdio.h>

int main()
{
	int week;
	scanf("%d", &week);
	if( week > 7 || week < 1 )//Ensure that the input number is between 1 and 7, otherwise the program ends
	{
		printf("error\n");
		//return 0;// End the main function and end the program
	}
    //Only when week is between 1 and 7 can the following code be executed
	else if( week == 1 )//If week is 1, the following conditions will not be judged
	{
		printf("Monday\n");
	}
	else if( week == 2 )
	{
		printf("Tuesday\n");
	}
	else if( week == 3 )
	{
		printf("Wednesday\n");
	}
	else
	{
		printf("other days\n");
	}
    return 0;
}

practice:

Please enter a score and use a 5-point scale to evaluate the score.

The score is between 0 and 100

A 90~100

B 80~89

C 70~79

D 60~69

E 0~59

The expression of Pit 1 < a < 10 concatenated by relation operator must be true!!

#include <stdio.h>

int main()
{
	int a;
	scanf("%d", &a);
	printf("%d\n", 1<a<10);//Same priority, from left to right
    //First execute 1 < A, no matter what the value of a is, the result of the expression is 0 or 1, and then compare 1 or 0 with 10, which is less than 10
    //Modify 1 < A & & A < 10
	return 0;
}
#include <stdio.h>

int main()
{
	int score;
	scanf("%d", &score);
    //When representing an interval, it is necessary to use the interval opened before than after. [1, 10) can get 1, but can't get 10
	if(score<0 || score>100)
	{
		printf("error\n");
	}
	else if(score>=90)//The previous condition must be false when the execution is here
	{
		printf("A\n");
	}
	else if(score>=80)
	{
		printf("B\n");
	}
	else if(score>=70)
	{
		printf("C\n");
	}
	else if(score>=60)
	{
		printf("D\n");
	}
	else if(score>=0)
	{
		printf("E\n");
	}
	return 0;
}

4. Base system

1. Binary table:

Decimal: every decimal one. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Binary: every two into one. 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 10000

There is no binary representation of constants in C. Because no one likes binary constants.

Octal: every eight into one. 00 01 02 03 04 05 06 07 010 011 012 013 014 015 016 017 020

Hexadecimal: every hexadecimal enters one. 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf 0x10

Prefix:

Octal, prefix 0 072 010

Hex, prefix 0x 0x82

Decimal format:% d

Octal format:% o

Hex format:% x

int main()
{
    int a = 100;
    printf("%d %o %x\n", a, a, a);//What is the output?
    //Different hexadecimals are just different display forms of the same data. It mainly depends on what kind of convenience our developers see.
    return 0;
};

2. Binary to other

Two to ten: the number on each bit of the binary system is multiplied by its corresponding weight, and then the corresponding results are added. The result is a decimal value.

0110 1011 = 107

Two to eight: every three digits of binary are correspondingly converted into one digit of octal.

001 101 011 = 0153

Two to sixteen: every four numbers in binary are correspondingly converted into one digit in hexadecimal.

0110 1011 = 0x6B

practice:

Convert the following binary numbers into other binary values.

​ 010000111 010101000 00011100

Decimal 135 168 28

Octal 0207 0250 034

Hex 0x87 0xA8 0x1C

3. Other binary conversion

Ten to two: take the decimal number as the remainder of two, and then take the quotient of this time as the dividend to continue to take the remainder of two until the quotient is 0. Read out the remainder from the back to the front, that is, the converted binary number

Eight to two: octal per digit, corresponding to the three digits converted to binary.

Hexadecimal to two: each digit of hexadecimal, corresponding to the four digits converted to binary.

Short division:

135 / 2 = 67 ...... 1

67 / 2 = 33 ...... 1

33 / 2 = 16 ...... 1

16 / 2 = 8 ...... 0

8 / 2 = 4 ...... 0

4 / 2 = 2 ...... 0

2 / 2 = 1 ...... 0

1 / 2 = 0 ...... 1

1000 0111

practice:

Convert the following numbers to binary

0xb6 1011 0110

0231 010 011 001

234 11101010

234/2 = 117...0

117/2 = 58...1

58/2= 29...0

29/2 = 14...1

14/2 = 7...0

7/2 = 3...1

3/2 = 1...1

1/2 = 0...1

Binary summary:

Each hexadecimal does not affect the storage mode of integers in memory, which are stored in binary. The significance of the existence of each binary is to facilitate us to read data.

5. Character constant

1. Character constant:

A character enclosed in single quotes.

matters needing attention:

  1. Must be enclosed in single quotes. Cannot be another symbol. "A" ` a ` the backquote 'a' is a character constant because there is a single quote

  2. It can only be a single character, not multiple characters‘ A 'ab' / / syntax error

  3. If more than one character is required, escape characters are allowed‘ \n’ ‘\0’ \b’

2. ascii code

(ASCII (American Standard Code for Information Interchange)

Each character has a number, which is the ascii code.

Characters in ascii code table are called characters.

User manual table lookup: man ascii press q to exit

'\0' — 0

' ' — 32

'0' — 48 '9' — 57

'A' — 65 'Z' — 90

'a' — 97 'z' — 122

Characters are stored as integers in memory.

3. Example:

Relationship between character type and integer type

#include <stdio.h> 

int main()
{
	char a = 'A';
	printf("%c  %d\n",a ,a);// A  65
	return 0;
}

In C language, characters are integers. The meaning of characters is to make humans look more comfortable. For computers, characters are integers.

Each character is stored in memory with the corresponding encoding in the ascii table.

#include <stdio.h>

int main()
{
	char c = 'b';
	char c2 = 'o';
	char c3 = 'b';
	printf("%c%c%c\n", c, c2, c3);//Output bob in character form
	printf("%d%d%d\n", c, c2, c3);//Output 9811198 as an integer
	return 0;
}

Converts numeric characters to corresponding integers

int main()
{
	char a = '9';//Character 9 cannot be treated as an integer 9
    //In some cases, we need to convert numeric characters into corresponding integers for operation.
	int num = a-'0';//The corresponding integer can be obtained by subtracting the character 0 from the numeric character
	int num2 = a-48;//Subtracting 48 is also OK, because 48 and character 0 are the same for a computer.
	printf("%d %d\n", num, num2);// 9 9
	return 0;
}

practice:

Enter a character to determine whether it is a capital letter.

#include <stdio.h>

int main()
{
	char input;
	scanf("%c", &input);
	if(input>='A' && input<='Z')
	{	
		printf("yes\n");
	}
	else
	{
		printf("no\n");
	}
	return 0;
}

Assignment 1:

Complete the following functions:

1 displays: "please input a number:"

2 user input

3 judge whether the number entered by the user is > 90

Assignment 2:

Judge whether a number meets the conditions:

a) is a multiple of 7

b) not a multiple of 3

If both are satisfied, the number is output

Assignment 3:

Input a certain time and output its next second: (written test programming question)

Effect example: input: 20 59 59

Output: 21:0:0

Stored.

#include <stdio.h>

int main()
{
	char c = 'b';
	char c2 = 'o';
	char c3 = 'b';
	printf("%c%c%c\n", c, c2, c3);//Output bob in character form
	printf("%d%d%d\n", c, c2, c3);//Output 9811198 as an integer
	return 0;
}

Converts numeric characters to corresponding integers

int main()
{
	char a = '9';//Character 9 cannot be treated as an integer 9
    //In some cases, we need to convert numeric characters into corresponding integers for operation.
	int num = a-'0';//The corresponding integer can be obtained by subtracting the character 0 from the numeric character
	int num2 = a-48;//Subtracting 48 is also OK, because 48 and character 0 are the same for a computer.
	printf("%d %d\n", num, num2);// 9 9
	return 0;
}

practice:

Enter a character to determine whether it is a capital letter.

#include <stdio.h>

int main()
{
	char input;
	scanf("%c", &input);
	if(input>='A' && input<='Z')
	{	
		printf("yes\n");
	}
	else
	{
		printf("no\n");
	}
	return 0;
}

Assignment 1:

Complete the following functions:

1 displays: "please input a number:"

2 user input

3 judge whether the number entered by the user is > 90

Assignment 2:

Judge whether a number meets the conditions:

a) is a multiple of 7

b) not a multiple of 3

If both are satisfied, the number is output

Assignment 3:

Input a certain time and output its next second: (written test programming question)

Effect example: input: 20 59 59

Output: 21:0:0

It is required that if the user enters the wrong time, exit at 24 o'clock and 61 seconds

Topics: C Back-end