First knowledge of c language 02

Posted by wmac on Wed, 20 Oct 2021 06:33:56 +0200

catalogue

6 selection statement

7 circular statement

8 function

9 array

10 operator

arithmetic operator

Shift operators

Shift operators

Bitwise operators

Assignment operator

unary operator

Logical operator

Relational operator

Logical operator

Conditional Operator

comma expression

Subscript references, function calls, and structure members

11 common keywords

6 selection statement

 

if
switch

#include <stdio.h>

int main()

{

       int coding = 0;

       printf("Will you type the code? (option 1) or 0):>");

       scanf("%d", &coding);

       if (coding == 1)

       {

               printf("Hold on, you'll have a good time offer\n");

       }

       else

       {

               printf("Give up and go home to sell sweet potatoes\n");

       }

       return 0;

}

7 circular statement

Some things must be done day after day
Lifelong learning
Valid code 2w line! Note that it is a valid code
while
for
do while

int main()

{

       printf("Add bit\n");

       int line = 0;

       while (line <= 20000)

       {

               line++;

               printf("I'll keep trying to type the code\n");

               //printf("%d\n", line);

       }

       if (line > 20000)

               printf("Congratulations on finding a good job offer\n");

       else

       {

               printf("The heat is not enough\n");

       }

       return 0;

}

8 function

Main function - main function
What is a function
f(x)=cx+b
f(x,y) =x+y

In c language, some independent functions are encapsulated into a function

int main()

{

       int num1 = 0;

       int num2 = 0;

       int sum = 0;

       printf("Enter two operands:>");

       scanf("%d %d", &num1, &num2);

       sum = num1 + num2;

       printf("sum = %d\n", sum);

       return 0;

}

int Add(int x, int y) 

{

       int z = x + y;

       return z;

}

int main()

{

       int num1 = 0;

       int num2 = 0;

       int sum = 0;

       printf("Enter two operands:>");

       scanf("%d %d", &num1, &num2);

       //How to write the format string in scanf, you must input it

       sum = Add(num1, num2);

       printf("sum = %d\n", sum);

       return 0;

}

Think of a function as a factory
Raw materials -- > factory (processing) - > Products
Input parameter - function operation - return result
num1,num2--Add--sum
Return type, function name, formal parameter of function, function body
If the function is defined after main, it needs to be declared before main

9 array

A collection of elements of the same type
int a=1;
int b=2;
int c=3;
...
Deposit 1-1000?

int main()

{

       int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };//Define an integer array with up to 10 elements

       //Save character - character array

       char ch1[3] = { 'a','b','c' };

       char ch2[6] = "hello";

       //There is another '\ 0' after hello

       //Best initialization

       int arr1[10];

       printf("%d\n", arr1[0]);

       //If the vs2019 array is not initialized, no error will be reported

       

       //Incomplete initialization

       //int arr[10] ={ 0 };

       return 0;

}

The array subscript starts from 0, which is specified in the c standard
Positioning array elements by subscript

int main()

{

       int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };

       int i = 0;

       while (i<=9)

       {

               printf("%d ", arr[i]);

               i++;

       }

       return 0;

}

10 operator

arithmetic operator

      • / %
        %Operators can only act on integers
        %Modulo (remainder) operator
        7% 2 quotient 3 surplus 1
int main()

{

       //Int n = 7.0% 2; / / illegal

       //%Can only act on integers

       return 0;

}

7.0/2
/As long as there are decimals at both ends, floating-point division is performed

Shift operators

int a = 3;
Binary bit of 3
00000000000000000000000000000011
32 bit

Shift operators

>> <<

Shift left moves the binary bit to the left, yes × 2 Effect of

Bitwise operators

Bits here also refer to binary bits

&

Bitwise AND
If the corresponding binary bit is 1 at the same time, it is 1. As long as there is 0, it is 0

^

Bitwise XOR
The corresponding binary bits are the same as 0, and the difference is 1

int main()

{

       int a = 3;

       int b = 5;

       int c = a ^ b;

       //c:   00000000000000000000000000000110

       printf("%d\n", c);

       return 0;

}

|

Bitwise OR
If the corresponding binary bit has 1, it is 1, and all 0 is 0

int main()

{

       int a = 3;

       int b = 5;

       int c = a & b;

       //3:

       //00000000000000000000000000000011

       //00000000000000000000000000000101

       //00000000000000000000000000000001

       //At the same time, 1 is 1. As long as there is 0, it is 0

       printf("%d\n", c);

       return 0;

}

Assignment operator

= += -= *= /= &= ^=  |=    >>=   <<=

+=Compound assignor
Compound assignment characters are more convenient to write
Be lazy and write a directly++

unary operator

+Is a binocular operator with 2 operands a+b
A unary operator has only one operand

!

Logical reverse operation

int main()

{

       int c = 10;

       printf("%d\n", !c);  //0

       return 0;

}

In c language, 0 means false and non-0 means true
Operators are operators. The English translation results are different

+

-

&

Fetch address symbol

sizeof

sizeof is an operator, not a function!!!
Used to calculate variable size
int a=10; an a variable is created in memory
sizeof(a) results in 4
Calculates the amount of memory occupied by variables or types created by variables -- in bytes

int main()

{

       int a = 10;

       printf("%d\n", sizeof(a));//4

       printf("%d\n", sizeof(int));//4

       //Calculates the amount of memory occupied by variables or types created by variables -- in bytes

       printf("%d\n", sizeof(10));//4

       //The result of 3 + 4 is 7 and the type is int

       return 0;

}

~

Reverse by binary digit
Negate the complement existing in memory

int main()

{

       int a = -1;

       //-1 is a negative integer

       //The binary of negative integer has original code, inverse code and complement code

       //The binary complement is stored in memory

       //1000000000000001 - original code

       //11111111111111111111111111111111111110 - inverse code

       //11111111111111111111111111111111111111 - complement

       //000000000000000000000000000000000000000000000000000 - negate

       int b = ~a;

       printf("%d\n", b);

       return 0;

}

++/--

++a pre + + first + + then use

//int main()

//{

//     int a = 10;

//     int b = ++a;

//     printf("%d\n", b);

//     printf("%d\n", a);

//     

//     return 0;

//}

int main()

{

       int a = 10;

       int b = a++;

       //Post + + use first and then++

       printf("%d\n", b);

       printf("%d\n", a);



       return 0;

}

For custom types, pre + + is more efficient

int main()

{

       int a = 10;

       int b = a--;

       //Use first and then--

       printf("%d\n", b);//10

       printf("%d\n", a);//9



       return 0;

}

*

Dereference operator / indirect access operator

(type)

Cast type

int main()

{

       int n = 3.14;

       printf("%d\n", n);



       return 0;

}

warning conversion from double to int may lose data
Just use cast

int main()

{

       int n =(int) 3.14;

       printf("%d\n", n);



       return 0;

}

Logical operator

int main()

{

       int a = 3;

       int b = 5;

       if ((a == 3) || (b == 3))

       {

               printf("hehe\n");

       }

       return 0;

}

Relational operator

>
>=
<
<=
!=
==

Logical operator

&&Logic and
||Logical or

Conditional Operator

exp1 ? exp2 : exp3

//int main()
//{
//      //exp1 ? exp2 : exp3
//      //Simplify if statement
//      int a = 0;
//      int b = 0;
//      int max = 0;
//      scanf("%d %d", &a, &b);
//      if (a > b)
//              max = a;
//      else
//              max = b;
//      printf("max is %d\n", max);
//      return 0;
//}


int main()
{
        //exp1 ? exp2 : exp3
        //Simplify if statement
        int a = 0;
        int b = 0;
        int max = 0;
        scanf("%d %d", &a, &b);
        max = a > b ? a : b;
        //ternary operators 
        printf("max is %d\n", max);
        return 0;
}

comma expression

//int main()

//{

//     int a = 3;

//     int b = 5;

//     int c = 10;

//     //The comma expression will be evaluated from left to right, and the whole comma expression result is the last expression result

//

//     int d = (a + 2, b = a - 3, c = b + 4);

//     printf("%d\n", d);//b is 0 + 4 = 4

//     The value of printf ('% d \ n', b); / / * * B has changed 0**

//

//     return 0;

//}

Subscript references, function calls, and structure members

arr[4]

[] subscript reference operator
Two operands, one is arr, the other is 4 array name and subscript

() function call operator
Add(3,5);
When calling a function, the () operand must be the function name Add,3,5
Operands are not fixed and can be more or less

.
->

11 common keywords

Auto auto defines automatic variables, automatically creates spaces, and automatically destroys them
auto int a=0;
All local variables themselves are auto

char
short
int
long
float
double

break - terminate - used in the loop while for do while switch
Continue - continue - used to cycle and exit this cycle
case-swtich
Default default
do
else
for
if
go to
while

const - constant attribute - modifier variable, modifier pointer
extern - declare external symbols

Register - register keyword / recommended command. It is not mandatory. The cpu needs to consider whether to use it


The higher the speed, the smaller the space and the higher the cost
Cache tens of M
In the early days, there was no cache or anything
The processing speed of cpu is almost the same as that of memory. Later, the processing speed of cpu is faster and faster, but the reading and writing speed of memory can not keep up, so cache and register appear
CPU central processing unit
Register eax ebx ecx edx esp ebp
Attention! Register variables cannot take addresses
return, which is used in the function

Signed signed decorated type
Unsigned unsigned unsigned

void none, null function return type, function parameter, decorated pointer
volatile
Types that can be customized in c language:
enum enumeration
struct structure
union Consortium
sizeof
typedef type redefinition

typedef unsigned int u_int;

int main()

{

       unsigned int num1 = 100;

       u_int num2 = 100;

       return 0;

}

Topics: C