2. Blurring Light

Written earlier: This is a floating shadow of C.   Can help you quickly browse C's outline~

Catalog

🍔 constant  

🍕 Literal Constant

🍕 const-modified constant variable

🍕# Identifier constant defined by define

🍕 enumeration constant

🍔 Character string

🍕 Pritf-related issues in strings

🍕 strlen-related problems in strings

🍔 Escape Character

🍕 String length including escape characters

🍔 Notes

🍔 Selection statement if

🍔 loop

🍔 function

🍔 array

🍔 Operator  

🍕 Arithmetic Operators  

🍕 Shift operators

🍕 Bitwise operators

  🍕 Assignment Operators

  🍕 unary operator

Case 1: Logical Inverse Operation

Case 2: sizeof

Case 3:  ~ Operator action (for positive numbers)

Case 4:  ~ Operator action (for negative numbers)  

Case 5: ++/--Operator

🍕 Cast Operator

🍕 Relational Operators

🍕 Logical Operator  

🍕 Conditional operator (trinomial operator) exp1? Exp2: EXP3

🍕 comma expression

🍕 Subscript Reference Operator[]  

🍔 constant  

🍕 Literal Constant

/ is a value that can be expressed as a string. It includes integer constants, character constants, and string constants.

Note: There are no literal constants of structs such as array constants, struct constants, etc.

#include <stdio.h>
int main()
{
    int num=4;
    printf("%d\n",num);
    num=8;
    printf("%d\n",num);//num variable, so you can change
    3;
    100;
    3.14;//Literal Constant
    'a';
    "abc";
    return 0;
}

🍕 const-modified constant variable

  L: How can I prove that a const-modified constant is a variable?

🐉: Array to prove:

.arr[n] n should be a constant, otherwise errors will occur in VS, because arr[m] in the following program also errors, indicating that const-modified constant M is a variable!

#include<stdio.h>
int main()
{
    int n=10;
    const int m=10;
    int arr[10]={0};
    int arr[n]={0};//Error, constants should be placed at n
    int arr[m]={0};//error,m is a variable but has a constant attribute, so we say m is a constant variable
    //In C99, variable-length arrays are supported, allowing variable classes to specify the size of the array
    return 0;
}

🍕# Identifier constant defined by define

#include<stdio.h>
#define MAX 10
//Conventional define definitions are typically capitalized
int main()
{
    int arr[MAX]={0};
    printf("%d\n",MAX);
    return 0;
}

🍕 enumeration constant

/ Citation:

//Enumeration=One-to-One Enumeration
//Gender: male, female, confidential
//Three primary colors: red, yellow, blue
//Week: 1,2,3,4,5,6,7
//Enumeration keywords: enum
#include<stdio.h>
enum Sex
{//Enumerate possible values of this type
  male,
  female,
  secret        
};

int main()
{
    enum Sex s= female;
    printf("male=%d\n",male);
    printf("female=%d\n",female);
    printf("secret=%d\n",secret);
    return 0;
}

Case:

1. Variables defined by enumeration constants can be changed

2. Enumeration constants themselves cannot be changed

#include <stdio.h>
enum Color
{
    RED,
    YELLOW,
    BLUE
};
int main()
{
 enum Color color =BLUE;
 printf("color=%d\n",color);
 color=YELLOW;//Variables defined by enumeration constants can be changed
 BLUE=6;//The error enumeration constant itself cannot be changed
 printf("color=%d\n",color);
 return 0;   
}

✨✨✨ I am a dividing line ✨✨✨

🍔 Character string

🐉: Let me give you an overview of the first slightly interesting thing - string

. This string of characters caused by double Quotes is called a String Literal or simply a string.

The end flag of the string is an escape character of \0.

\0 is the end flag when calculating the length of a string and is not counted as the string content.

#include <stdio.h>
int main()
{
    "abcdefg"//Character string
    'a';//character
    "a";//Character string
    "hello";
    "";//Empty String
    char arr1[]="abc";//array
    char arr2[]={'a','b','c'};
    printf("%s\n",arr1);//%s Print String
    printf("%s\n",arr2);
    return 0 ;
}

L: Why is it so hot when printing arr2?

🐉: Because the end flag of the string is'\0'.

L: What does this have to do with the end sign?

🐉: See the following analysis:

🍕 Pritf-related issues in strings

 

🍕 strlen-related problems in strings

string length--A function that calculates the length of a string

🐉: When strlen calculates the length of a string\0 is the end flag and does not count as the string content

🐉: For arr1 here, since arr1 is a string and it comes with a hidden'\0', the strlen number stops at c

🐉: For arr2, strlen counts backwards and looks for'\0'until it finds'\0' in the random value, stopping

L: So 218 here is a random value. Maybe it will change after the next recompile?

🐉: Yes!

L: So how can we improve arr2? Let him calculate normally

🐉: Improvements are as follows

#include <stdio.h>
int main()
{
    char arr1[]="abc";
    char arr2[]={'a','b','c','\0'};// Here'\0'can also be replaced by 0 because the value of \0 is 0
    //stelen only requires length before'\0'
    printf("%d\n",strlen(arr1));
    printf("%d\n",strlen(arr2));
    return 0 ;
}

. As we can see from the above two tests, adding'\0'and 0 at the end of the arr2 array will calculate the character length normally

This is because the value of'\0'is 0

 

 

🍔 Escape Character

The escape character, as its name implies, changes meaning

Case 1: What if you want to type a single quotation mark, a double quotation mark, two consecutive question marks?

Case 2: \ddd and love hatred between decimal and ASCII

\ddd( octal)->corresponding 10-digit number->ASCII character corresponding to decimal number

 · Case 3: \xdd and love hatred between decimal and ASCII

\xdd( hexadecimal)->corresponding to decimal number->corresponding ASCII characters for decimal number

🍕 String length including escape characters

  🐉: The answer is 14. Is that correct?

L: A piece of cake - guess what? I'll analyze it for you:

✨✨✨ I am a dividing line ✨✨✨

🍔 Notes

✨✨✨ I am a dividing line ✨✨✨

 

🍔 Selection statement if

Case 1.

Case 2.

. ⚠️ == is equal to / is an assignment within the if statement ()

int main()
{
    int a=0;
    a=20;//assignment
    if(a=30) //The original meaning might be if a equals 30 to output I love you
    {
        printf("i love u\n"); //Because if statement (a=30) is an assignment statement; A is 3030 is true; must output
    }
    
    if(a==40)//Here is a== (equal to) 40, with a assigned to 30 in the if statement above
    {
        printf(" i hate u\n");   //No Output
    }
    
     if(a==30)
    {
        printf(" i miss u\n");    //Assign a to 30 in the if statement above
    }
    return 0;
}

✨✨✨ I am a dividing line ✨✨✨

🍔 loop

/ There are things that have to be done all the time, like my practice day after day; like everybody's study day after day.

#include <stdio.h>
int main()
{
    int line=0;
    printf("Step in C Rivers and lakes\n");
    while (line < 20000)
    {
        printf("Refine a line of code  %d\n",line);
        line++;
    }
    printf("You'll get the strongest Union offer");
    return 0;
}

✨✨✨ I am a dividing line ✨✨✨

🍔 function

C language, encapsulates some independent functions into a single function

//Sum two numbers
#include <stdio.h>
int a=20;
int main()
{
    int num1=0;
    int num2=0;
    int a=100;
    int b=200;
    int sum;
    scanf("%d%d",&num1,&num2);
    sum=num1+num2;
    sum=a+b;
    printf("sum=%d\n",sum);
    return 0 ;
}

After a function call is made:

#include <stdio.h>
int a=20;
int Add(int x, int y)// Add function name
{
    int z=x+y;
    return z;
}


int main()
{
    int num1=0;
    int num2=0;
    int sum;
    scanf("%d%d",&num1,&num2);
    sum=Add(num1,num2);//sum=num1+num2;
    printf("sum=%d\n",sum);
    sum=Add(2,4);
    printf("sum=%d\n",sum);
    return 0 ;
}

✨✨✨ I am a dividing line ✨✨✨

🍔 array

A collection of elements of the same type

 

 

🍔 Operator  

🍕 Arithmetic Operators  

 

  Here's a special introduction to the%-modulo operator

 

🍕 Shift operators

#include<stdio.h>
int main()
{
    //Shift (binary) operator
    // << Move Left
    // >Move right
    int a=1;//Integer 1 takes 32 bits
    //00000000 00000000 00000000 00000001
    int b = a<<1;//A Move a Binary Bit to the Left
    // 00000000 00000000 00000000 000000010
    int c = a<<2;
    // 00000000 00000000 00000000 000000100
    printf("a=%d\n",a);
    printf("b=%d\n",b);
    printf("c=%d\n",c);
}

🍕 Bitwise operators

(binary) bit operation

#include<stdio.h>
int main()
{
    // &Bitwise and
    //| Bitwise or
    // ^Bitwise XOR
    int a = 3;//011
    int b = 5;//101
    int c = a & b; //The corresponding binary bit has 0 then 0; all 1 is 1
    int d = a | b; //The corresponding binary bit has 1 1; all 0 is 0
    int e = a ^ b; //The corresponding binary bits are the same 0; the difference is 1
    printf("%d\n", c);
    printf("%d\n", d);
    printf("%d\n", e);
    return 0;
}

 

  🍕 Assignment Operators

/=assignment

/==Equality of judgement

int main()
{
    int a=10;
    a=20;
    a=a+10;
    a+=10;//Simplify
    
    a=a-20;
    a-=20;
    
    a=a&2;
    a&=2;
    
    a=a>>2;
    a>>=2;
    
    //Composite evaluator
    // +=  -=  *=  %=  /=  >>=  <<=  &=  |=  ^= 
    return 0;
}

  🍕 unary operator

Case 1:! Logical Inverse Operation

Case 2: sizeof

#include <stdio.h>
int main()
{
    int a=10;
    printf("%d\n",sizeof(a));//4
    printf("%d\n",sizeof(int));//4
    printf("%d\n",sizeof(10));//The type attribute of 10 is int
    printf("%d\n",sizeof a );//Real sizeof can be placed without parentheses
    printf("%d\n",sizeof int);//error 
    int arr[10]={0};
    printf("%d\n",sizeof(arr));//Calculate the number of array elements
    int sz=0;
    sz= sizeof(arr)/sizeof(arr[0]);
    printf("%d\n",sz);
    //Calculate the number of array elements = the total size of the array / the size of each element
    //sizeof calculates the size of the space occupied by a variable or type in bytes
}

Case 3:  ~ Operator action (for positive numbers)

Case 4:  ~ Operator action (for negative numbers)  

 · Case 5: ++/--Operator

1.Postposition++.

#include <stdio.h>
int main()
{
    int a =10;
    int b =a++;//Post++, use first, in++.
    printf("a=%d ,b=%d",a,b);
    return 0 ;
}

2.Pre++.

#include <stdio.h>
int main()
{
    int a =10;
    int b =++a;//Pre++, first++, then use
    printf("a=%d ,b=%d",a,b);
    return 0 ;
}

3. Rear--

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

 

4. Front--

#include <stdio.h>
int main()
{
    int a =10;
    int b =--a;//
    printf("a=%d ,b=%d",a,b);
    return 0 ;
}

🍕 Cast Operator

🍕 Relational Operators

🍕 Logical Operator  

 

 

🍕 Conditional operator (trinomial operator) exp1? Exp2:exp3

//exp1? exp2:exp3 - Conditional Operator
//exp--Imagine it as an expression
//If expression 1 is true, expression 2 executes;
//If expression 1 is false, expression 3 executes
#include <stdio.h>
int main()
{
    int a =10;
    int b =20;
    int max=0;
    if(a>b)
    max=a;
    else
    max=b;
    return 0;
}
//exp1? exp2:exp3 - Conditional Operator
//exp--Imagine it as an expression
//If expression 1 is true, expression 2 executes;
//If expression 1 is false, expression 3 executes
#include <stdio.h>
int main()
{
    int a =10;
    int b =20;
    int max=0;
    max=(a>b ? a : b);
    printf("Larger values are%d\n",max);
    return 0;
}

🍕 comma expression

🍕 Subscript Reference Operator[]  

  This is the end of today's voyage, and the floating shadows on C's Lake end!

Dao Friends! See you tomorrow~

 

Tags: C Back-end

Posted on Fri, 12 Nov 2021 11:22:40 -0500 by brokenshadows