“This is the fifth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Recently, I want to review C language, so I will update an article about C language in nuggets every day! Freshmen who are just learning C, and those who want to review C /C++, don’t miss it! Solid foundation, slow down is fast!

Foreword: some people may feel that what the author writes is very simple, there is no need to write! But I want to say, existence is reasonable! Solid foundation is YYDS! Knowledge sharing YYds! Don't spray if you don't like it, thank you! If there are mistakes, welcome to correct!Copy the code

1.C language is a structured programming language

C language has three structures: sequence structure, selection structure, loop structure

Empty statement:;// There is only one semicolon
Copy the code

2. In C language, non-0 means true and 0 means false

Judgment: In C language, 1 indicates true and 0 indicates false. False: Non-0 indicates trueCopy the code

3. Match else and if

The proximity rule: else matches the nearest if

#include<stdio.h>
int main(a)
{
    int a = 0;
    int b = 2;
    if(a == 1)
		if(b == 2)
            printf("hehe\n");
    else
        printf("haha\n");
    return 0;
}
// Print nothing
// Because if matches the nearest else, the first if loop is not entered because a == 0 is not satisfied, so it is not printed
// adds readability to the program
int main(a)
{
    int a = 0;
    int b = 2;
    if(a == 1)
    {
	  if(b == 2)
         printf("hehe\n");
       else
        printf("haha\n");
    }
    return 0;
}		
Copy the code

Program: Print odd numbers from 1 to 100 and count them

// Method 1: deliberately create odd numbers to print, more efficient
int main(a)
{
    int count = 0;
    int i = 1;
    // While loop
    /* while(i < 100) { printf("%d ",i); i += 2; count++; } * /
    
    //for loop
    for(i = 1; i < 100; i+=2)
    {
        printf("%d ",i);
        i += 2;
        count++;
    }
    printf("The odd numbers of 1-100 are: %d\n",count);
}
Copy the code
Int main() {int count = 0; int i = 1; while(i < 100) { if(i % 2 == 1) { printf("%d ",i); } i++; } return 0; }Copy the code

4.EOF-end of file Indicates the end of the file

EOF-> represents -1


Getchar () : Returns EOF on read failure

  1. The returned character, which is essentially an ASCII value, is an integer

  2. Getchar not only returns a normal character, it also returns that EOF is -1, so put it in an integer variable

    End the loop when CTRL + Z is encountered

The method of cyclic input

/ / write 1
while( (c = getchar()) ! =EOF)// Use parentheses
    
while( scanf("%d",&a) ! = EOF)/ / write 2
 while( ~(c = getchar()) )
 
  while( ~(scanf("%d",&a)) )
Copy the code
- 1The original code:10000000 00000000 00000000 00000001Radix-minus-one complement:11111111 11111111 11111111 11111110Complement:11111111	11111111 11111111 11111111The memory stores the complement EOF- 1According to the not- 1The complement of -> full0The sequence is0
Copy the code

Procedure: Password confirmation (buffer)

Input buffer: memory space

Scanf, getchar: Input something from the keyboard to the input buffer, get something from the input buffer

/ / err
int main(a)
{
    int ch = 0;
    char password[20] = {0};
    printf("Please enter your password >:");
    scanf("%S",password);// The array name is the address of the first element
    printf("Please confirm password (Y/N)>:");
    ch = getchar();
    if(ch == 'Y')
    {
        printf("Confirmed success \n");
    }
    else
    {
         printf("Confirmation failed \n");
    }
    return 0;
}
Copy the code


Enter characters from the keyboard into the buffer, andscanf%s can read only one character at a time. %s can read only the character before whitespace. Whitespace: space, newline) Since the last character is a newline character (\n), we need to clean the bufferCopy the code
/ / positive solutions
int main(a)
{
	int ch = 0;
	char password[20] = { 0 };
	printf("Please enter your password >:");
	scanf("%s", password);// The array name is the address of the first element
	printf("Please confirm password (Y/N)>:");
    // Clear the buffer characters
	while(getchar() ! ='\n') {;// Empty statement, no processing
	}
	ch = getchar();
	if (ch == 'Y')
	{
		printf("Confirmed success \n");
	}
	else
	{
		printf("Confirmation failed \n");
	}
	return 0;
}
Copy the code

To read whitespace as a password, use the gets() function

With gets(), you don’t have to worry about buffers

int main(a)
{
	int ch = 0;
	char password[20] = { 0 };
	printf("Please enter your password >:");
	gets(password);			
	printf("Please confirm password (Y/N)>:");
	ch = getchar();
	if (ch == 'Y')
	{
		printf("Confirmed success \n");
	}
	else
	{
		printf("Confirmation failed \n");
	}
	return 0;
}
Copy the code
That’s all for today. Thank you for seeing us! Hope to help you! You are welcome to click on this topic and subscribe! At the same time, welcome the bigwigs to criticize and correct!