Cantonese Embedded Study Notes (5)

1. Introduction to Linux

0.1 What is Linux?

Linux is an open source and free operating system, and its stability, security, and ability to handle high concurrency
have been recognized by the industry.

At present, most enterprise-level applications and even cluster project deployments run on the Linux operating system.

Many software companies will choose Linux in consideration of development costs, and it is the most recognized operating system for Chinese software companies

0.2 What are the versions released by Linux?

Ubuntu (Ubantu), Red Hat (Red Hat), CentOS

1. Software introduction

  • VMware — A virtual machine is a virtual computer

  • Ubuntu — the operating system is developed based on the Linux framework

  • notepad++ – editing software

2. Set up a shared directory

3. Some basic commands under Linux

  • cd jump directory

  • cd + / directory name with path/directory name or folder/ For example: cd /mnt/hgfs/

  • cd ... jump to the previous path

  • ls lists all files in the current directory

  • Create a file touch + file name for example: touch hello.c

  • Delete the file rm + filename eg: rm hello.c

  • Create a directory mkdir + directory name For example: mkdir + 01_04

  • Delete directory rm + directory name -r For example: rm 01_04 -r

    practise:

    Try to implement the above instructions

4. Edit code under Linux

use gcc editor

gcc + ***.c generates an executable file by default, this file is called a.out

How to run our executable

./a.out

./The meaning of the current directory


The C language will be reviewed every day to deepen the proficiency of the code

5. Consolidation of C language foundation

C language data type

(1), basic type

That is, the C language has already defined it for us, and we can use it directly

Integer data:

C language is used to store integers

  • char //character type
  • short //short integer
  • int // shaping
  • long / long integer
  • long long // long long shaping

The range of data represented is different

The number of bytes is different, making them occupy different spaces!

char 1111 1111
int 1111 1111 1111 1111 1111 1111 1111 1111

long has a larger range

sizeof(int) -> Find how many bytes the int type occupies


Character type:

used to store characters

char : occupies a byte 8bit


Floating point type: used to store decimals
  • Single precision floating point:

float occupies 4 bytes

  • Double precision floating point:

double occupies 8 bytes

long double occupies 12 bytes//long double-precision floating-point type


	C语言中的数据按是否可以修改分为两种:

variable (writable)

data type + variable name

int a ;
char b;

int a = 10;
char b = 20;//definition and initialization

access a variable;

int a = 100;

printf("%d", a);
//Get the value in the memory unit corresponding to a, (read), and print
int b;
scanf("%d", &a);//Get the address of a
/ /Write the data input by the terminal into the address of variable b;

constant (not writable)

(1) Integer constants
1 2 3 4 256
(2) Character constants Character
constants are usually enclosed in single quotes '' to obtain multiple character sequences

‘a’ , ‘b’ , ‘3’ , ‘%’ , ‘\n’ , ‘\113’

In the computer, when saving a character, does it save the shape of the character?

no!
ASCII code

man ascii

Characters : Divided into two types
Ordinary characters: Characters 'a', 'b', '1'
that can display shapes

Escape character : has no shape, but has special meaning

'\n' newline
'\r' carriage return
'\t' tab (tab)
'\0' end of string

Exercise :
5.1
Exchange the values ​​of two integer variables and output them


C language operator

1. Arithmetic operators

++ – unary operator

      • / binary operator

% binary operator

Such as:
4%3

/:
The second operand, cannot be 0

Example: 4/0 : Illegal

%:
The second operand cannot be 0,
such as 4%0: Illegal

++:
auto increment
–:
auto decrement
a = 9;
printf("%d\n", a++);

2. Relational operators

The operator
"relationship" used to judge the relationship between two things refers to the magnitude of the value

< > <= >= != ==

Values ​​of relational operators:

1 > 2

The relationship is established: the value is 1
The relationship is not established: the value is 0

3. Logical operators

! Logical non-unary operator
&& Logical and binary operator
|| Logical or binary operator

practise:

Input a year from the terminal
to judge whether it is a leap year
Leap year (every 4 years, no profit every 100 years, re-run every 400 years)
Ordinary leap year
Century leap year
answer

Branch structure:

1.
if (expression)
{ statement block; }

If the expression is true, execute the statement block, if the expression is 0, do not execute, directly execute the following statement

int a = 2;
if(a)
{ statement 1; } Expression: Any legal expression in C language is fine. As long as it is a legal expression, it will have a value. The cpu will make a judgment based on whether the value is 0 or not.





If it is non-zero, the statement block will be executed, otherwise it will not be executed

Programming suggestions:

Regardless of whether there is a statement after if,
if (expression)
{

}
The above curly braces { should be aligned with i

if(expression) //Determine the jurisdiction of if
Statement 1;
Statement 2;

2.
if (expression)
{ statement block; } else { statement block; }





3.
if (expression)
{ statement block; } else if (expression 1) { statement block; } else if (expression 3) { statement block; }









if(expression)
{ statement block 1; } if(expression) { statement block 2; }





4. Nested
if (expression)
{ if (expression 1) {

}
else
{ ; } statement block 1; } else {





}

Each else corresponds to the first if
hierarchy above it in the same column

practice :

Input a letter from the keyboard,
if it is a lowercase letter, convert it to an uppercase letter,
if it is an uppercase letter, convert it to a lowercase letter,
prompt "ASCII"
and print it out

#include<stdio.h>
int main()
{
    
    
	char ch;
    printf("请输入一个字母");
	scanf("%c",&ch);
	if(ch >= 65 && ch <= 90){
    
    
		ch += 32;
	} else {
    
    
		ch -=32;
	}
	printf("%c",ch);
}


Loop structure:

0, the introduction of the problem
Prime number (prime number)
can only be divisible by 1 or itself

35

if(0 == 35 % 2)
{ printf(not a prime number) }

if(0 == 35 % 3)
{ printf(not a prime number) }

if(0 == 35 % 4)
{ printf(not a prime number) }

·····························

-"cycle

1、while 2、for 3、do…while

1. While loop
Syntax:
while (expression)
{ statement block; } expression is true, execute the statement block


Every time through the loop, the expression will be judged once

while(1)
{
;
}

expression1 is true

so it's an endless loop

while()
{ The following is a compound statement; if() {


}

for()
{
    
    
   for()
     {
    
    

    }
 }


practise:

1. Use a while loop to find the sum of all multiples of 3 within 200

3 6 9 12 ······

/*求200以内所有3的倍数 之和*/
#include<stdio.h>
int main()
{
    
    
	int i = 1 , sum = 0;
	while(i < 200)
	{
    
    
		if(0 == i % 3)
		{
    
    
			sum += i;
		}
		i++;
	}
	printf("sum = %d\n" , sum);
	return  0;
}

2. for loop

Syntax:
for(expression1; expression2; expression3)
{ statement block; }

··············
for( ; ; )
{
;
}

Legal
is an endless loop

for( ; 1 ; )
{ ; } In the syntax of the for loop, you can leave all three expressions empty, which is an endless loop. Any expression that is left empty also conforms to the grammar;



When the for loop is executed;
the first pass execution:
run expression 1, judge whether expression 2 is 0, execute statement block, execute expression 3

How to execute later:

Judging whether expression 2 is 0, executing statement block, executing expression 3
judging whether expression 2 is 0, executing statement block, executing expression 3
judging whether expression 2 is 0, executing statement block, executing expression 3
·· ···········································

Two key statements:
break and continue

(1) Break Function: When the statement block of the loop is executed to break, it will jump out of the loop
for(int i = 1 ; i <= 10 ; i++)
{ if(i == 5) { break; } printf(" %d " , i); } printf("\n");






(2)
The function of continue: When the loop statement block is executed to continue, the current loop is ended, the expression 3 is executed, and the expression 2 is judged. In general, the
end of this loop is to execute the next loop
for(int i = 1 ; i <= 10 ; i++)
{ if(i == 5) { continue; } printf("%d " , i); } printf("\n");






Summary:
Introduction to Linux Environment, Introduction to Ubuntu System,
C Language:
Data Types, Operators, Branch Structure, Loop Structure

Homework:
1. Find the number of daffodils within 1000. "Daffodil number": it is a three-digit number, and the sum of its ones cube, tens cube, and hundreds cube is equal to
itself

2. Find
prime numbers within 100 Prime numbers:

3. Find the sum of consecutive positive integers of a positive integer
A positive integer may have the sum of multiple consecutive positive integers

For example: input a 15
output as follows:
15 = 1 + 2 + 3 + 4 + 5
15 = 4 + 5 + 6
15 = 7 + 8

Optional questions:

Use bubble sorting to realize the first array with a capacity of 10 and a data type of int for sorting (ascending order)
can use more advanced algorithms, such as: quick sorting, heap sorting

Guess you like

Origin blog.csdn.net/qq_40843903/article/details/112252108