Day4 first knowledge of C language 12.15

Posted by ReD_BeReT on Wed, 15 Dec 2021 22:32:10 +0100

Make a summary of this Tuesday's class notes.

Catalogue

Ternary operator (also called conditional operator)

comma expression

Supplement:

unary operator

Relational and logical operators

True and false

Keywords (cannot be created by yourself)

typedef keyword:

Register register keyword:

static keyword (modify variables and functions)

sum operation

Macro definition

Compound expression

!=EOF

! Exclamation marks can also be used to flip the truth of an equation.

Ternary operator (also called conditional operator)

Format is

exp1?exp2: exp3

#include<stdio.h>
int main()
{
    int a,b,c;
    scanf("%d %d %d"),&a,&b,&c);
    printf("%d",(a>b?a:b)>c?(a>b?a:b):c);
    return 0;
}

Where a > b? a: B indicates the size of a compared with B. If a > b, the whole segment outputs a to a. Otherwise, output B to B.

The following is a > c? a: c, that is, the ratio of a to c, outputs the larger one to% d.

comma expression

int a =3;
int b =20;
int c =0;
int d =(a-=3,b+=a,c=a-b,b=a-4);
//int d = comma expression in parentheses.
//a-=3 gives a=0, b+=a, that is, 20 plus 0 or 10, c becomes - 20, and b gives - 4.
//Therefore, the value of d is - 4.

Supplement:

int arr[10] = {0};
arr [5] = 9;
//Where [] is the subscript reference operator, and the operands are the array name and subscript.

int m = get max (3,5);
//Where () is the function call operator, and the operands are the function name and 3,5.

unary operator

!
-
+
sizeof (Is an operator in an operator, not a function.)
~
++
--
Cast type

The format of cast type conversion is:

int main()
{
    int a = 3.14;  //Here it is changed to int a = (int) 3.14; You can cast 3.14 to type int.
    printf("%d",a);

    return 0;
}

+ + and -- have the following concepts:
 

int a = 10;
int b = ++a;
int a = 10;
int b = a++;

//++A is the front + +, which is used after + +, and both a and b are printed as 11.
//A + + is the post + +, which is used first and then + +, and printed out a=11 and b=10.

Relational and logical operators

//Relational operators:
>
>=
<
<+
!=
==
//Logical operators:
&&
||
int a = 3;
int b = 4'
if(a=3)&&(b=4)//Use if for both.

if(a=3)||(b=4)//If either is true, you can use if.

True and false

There are true and false in C language. 0 stands for false and 1 for true. There are also true and false equations.

#include<stdio.h>
int main()
{
    int a=5,b=3,c=8;
    int d=(a>b),e=(b>c);     //a> The B expression is true, and the b > C expression is false
          printf("%d %d",d,e);  //So what is the result of the program
    return 0;
}

The printed d value is 1 and the e value is 0.

int a = 0;
if(a==5)   //An equal sign must be used to judge the value of if. One equal sign judges true and false, and two are to judge whether they are equal or not.
{
    printf("hehe\n");

}

Note: when if(a=0), the assignment expression of a=0 is false (non-zero is true), the subsequent else statement will not be executed.

If if(a=1), subsequent else statements can be executed.

Keywords (cannot be created by yourself)

typedef keyword:

unsignned int num = 10;
typedef unsignned int uint
unit num2 = 10;

//Now the meaning of the first sentence and the third sentence are exactly the same. typefed is equivalent to an alias for unsigned int.

Register register keyword:

Application scope of register keyword:
1. Located locally,
2. Will not be written,
3. Frequently read,
4. It will not be used on a large scale.

The variable modified by register cannot get the address (because it has been placed in the register area).

#include <stdio.h>
#include <windows.h> 
int main() 
{ 
	register int a = 0; 
	printf("&a = %p\n", &a);
	//Compiler error: error 1 error C2103: '&' on register variable 
	//Note that not all compilers report errors here. At present, our vs2013 reports errors.  
	system("pause"); 
	return 0; 
}
int num = 10;           //4 bytes.
register int num2 = 20;  //It is recommended to put 20 in the register.

register keyword stores data in registers for faster computing speed.

static keyword (modify variables and functions)

Can modify global variables, local variables, functions.

void test ()
{
    int a = 5;
    a++
    printf("%d",a);
}
int main()
{
    int i = 0;
    while(i<10)
    {    test();
         i++;

    }

    return 0;
}
//At this time, the output is 6,6,6,6,6,6,6,6,6,6
//But if it becomes static int a =5;
//You can print it as 6,7,8,9,10,11,12,13,14,15

Because the static modified part is stored in the static area.

Memory has stack area, heap area and static area. The original a = 5 is stored in the stack area. However, the static modification of a variable actually changes the storage location of the variable and puts it into the static area, resulting in the action of the variable and the life cycle is not over.

The static area generally stores static variables and global variables. Stack areas are stored local variables and function parameters. Heap area is used for dynamic memory development.

sum operation

The format is as follows:

#include<stdio.h>

int sum(int x,int y,int z)
{
    int c;
    c = x+y+z;
    return c;
}                   //Function declaration.


int main()
{
    int a;
    int b;
    int c;
    
    scanf("%d" "%d" "%d",&a,&b,&c);  //When entering decimals in scanf, change int to float, and% d of scanf to% f,
    printf("%d",sum(a,b,c));         //Change% d of printf to%. 2f.
   
    return 0;
}

Or, when a, b and c have specific values.

int main()
{
    int a = 10;
    int b = 20;
    int c = 30;
    int sum = 0; //initialization.

    printf("%d",sum);
    
    return 0;
}

Macro definition

#include<stdio.h>
int Max(int x, int y)
{
    if(x>y)
        return x;
    else
        return y;
}

#define Max(x,y) (x>y?x:y)
int main()
{
    int a = 10;
    int b = 20;
    int max = Max(a,b);
    printf("max=%d\n",Max);

    return 0;
}

I copied it first. I just met such a solution and didn't understand it deeply.

Compound expression

#include<stdio.h>
int main()
{
    if(4<2<3)    
        printf("ha");
    if(4>2>3)  
        printf("ha");
    if(4<3>2)   
        printf("ha");

    return 0;
}

First, the first 4 < 2 is false and 0 is output. 0 < 3 is true, output 1.

Similarly, the second and third output 0 can be obtained.

The associativity of < and > is calculated from left to right, that is, from left to right= The combination of = is from right to left, but the priority of = is lower than < >.

#include<stdio.h>
int main() 
{
	int a, b;
	    if (a = b = 4)      
		    printf("%d %d ",a,b);
	    if (a = 5 > 3)    
		    printf("%d", a);
	return 0;
}

The first printf output is 4,4. The second printf output is 1. Because 5 > 3, the value of the equation is true, that is, 1.

!=EOF

#include<stdio.h>
int main()
{
    int a=0;
    while(scanf("%d",&a)!=EOF);//You can enter multiple lines.
    if(a>=140)
    {
        printf("Genius\n");
    }
    return 0;
}

//Because the scanf function returns to EOF after reading failure, it needs to be written= EOF to exclude.
//The full name of EOF is end of file, which is the end flag of the file.

! Exclamation marks can also be used to flip the truth of an equation.

int x = 5;
!x Is false.
int y = 0;
!y Is true.

Topics: C