[First knowledge of C language] Notes (1-2)

constant

strings and escape characters

string

escape character

note

select statement

loop statement

function

array

array definition

subscript of the array

Use of arrays

1. Constant

The constants in C language are divided into the following categories:
literal constant
insert image description here
const modified constant variable

#include <stdio.h>
int main(){
    
    
   const //是常属性的
  const int num = 10;//num本质上是个变量,但是具有了常熟悉,不能被修改
   printf("%d\n", num);//10
   num = 20;//不能被修改
   printf("%d\n", num);//20
   return 0;
   }
  

Identifier constants defined by #define

#include <stdio.h>
#define MAX 1000//MAX不能被修改
int main()
{
    
    
   int arr[MAX];
#define MIN 0//局部使用
printf("%d\n", MAX);
printf("%d\n", MIN);
   return 0;
}

enumeration constant
Enumeration—enum (list one by one)
such as: gender (male, female, confidential)

#include <stdio.h>
enum Sex
 {
    
    
 	//下面三个符号是Sex这个枚举类型的未来的可能取值
 	//MALE,FEMALE,SECRET为枚举常量
 	MALE,  //0
 	FEMALE,//1
 	SECRET, //2
 };
 
 int main()
 {
    
    
 	//enum Sex s = FEMALE;
 	printf("%d\n", MALE);
 	printf("%d\n", FEMALE);
 	printf("%d\n", SECRET);
 
 	return 0;
 }//若想改变枚举的取值用MALE=5对其进行赋值

2. Strings and escape characters

2.1 string

A string of characters enclosed in double quotes like "hello\n" is called a string literal, or simply a string.

#include <stdio.h>
int main()
{
    
    
//数组可放多个字符
//字符数组
 char arr1[] = "bit";//b i t /0
 char arr2[] = {
    
     'b', 'i', 't' };//b i t
 char arr3[] = {
    
     'b', 'i', 't', '\0' };//b i t \0
 printf("%s\n", arr1);
 printf("%s\n", arr2);
 printf("%s\n", arr3);
 return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
    
    
 //strlen 是一个库函数,专门用来求字符串长度的

 int len = strlen("abcdef");
 printf("%d\n", len);

 return 0;
}//长度为6
  • %d - print integer

  • %s - print string

  • %c - print character's
    Note: The end of the string is a \0 escape character. When calculating the length of the string, \0 is the end mark, and it is not counted as the content of the string.

2.2 escape characters

If we want to print a directory on the screen: c:\code\test.c
how to write code

#include <stdio.h>
int main()
{
    
    
 printf("c:\code\test.c\n");
    return 0;
}

After running, you will find that the running result of this code is c:code est.c
This reflects the importance of the escape character, the escape character is the character that changes the meaning
The following are some common escape characters

escape character paraphrase
\? Used when writing multiple question marks in a row, preventing them from being parsed into three-letter words
\’ Used to represent character constants'
\“ Used to denote double quotes inside a string
\\ Used to denote a backslash, preventing it from being interpreted as an escape sequence
\a warning character, beep
\b backspace
\f Feed character
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\ddd ddd represents 1~3 octal numbers. Such as: \130 X
\xdd dd means 2 hexadecimal digits. Such as: \x30 0

So if you want the running result to be c:\code\test.c, you should rewrite the code as

#include <stdio.h>
int main()
{
    
    
printf("c:\\code\\test.c\\n");
   return 0;
}

\ddd usage, input \130 print result is X
insert image description here
octal 130 converted to decimal is 88, the ASCII code value corresponding to 88 is X

The figure below is the ASCII table

insert image description here

3. Notes

  1. Unnecessary code in the code can be deleted directly or commented out
  2. Some codes in the code are difficult to understand, you can add a comment text
#include <stdio.h>
int main()
{
    
    
	/*
int a = 10;//创建一个变量,并赋值10
*/
return 0;
}

Comments come in two flavors:
insert image description here

4. Select statement

If you study hard, get a good offer during school recruitment, and reach the pinnacle of life.
If you don't study, graduation is equivalent to unemployment, go home and sell sweet potatoes.

#include <stdio.h>
int main()
{
    
    
    int coding = 0;
    printf("你会去敲代码吗?(选择1 or 0)");
    scanf("%d", &coding);
    if(coding == 1)
   {
    
    
       prinf("坚持,你会有好offer\n");
   }
    else
   {
    
    
       printf("放弃,回家卖红薯\n");
   }
    return 0;
}

Select statement structure:if(){}else{}

5. Loop statement

Cycle refers to: some things must be done all the time, such as my lectures day after day, like everyone, study day after day.
How to implement loop in C language?

  • while statement
  • for statement
  • do ... while statement
//while循环的实例
#include <stdio.h>
int main()
{
    
    
    printf("好好学习\n");
    int line = 0;
    while(line<=20000)
   {
    
    
        line++;
        printf("我要继续努力敲代码\n");
   }
    if(line>20000)
        printf("好offer\n");
    return 0;
}

6. Function

//两数求和
#include <stdio.h>
int main()
{
    
    
    int num1 = 0;
   int num2 = 0;
    int sum = 0;
    printf("输入两个操作数:");
    scanf("%d %d", &num1, &num2);
    sum = num1 + num2;
    printf("sum = %d\n", sum);
    return 0;
}

The above code is written as a function as follows:

#include <stdio.h>
int Add(int x, int y)
{
    
    
   int z = x+y;
   return z;
}
int main()
{
    
    
    int num1 = 0;
   int num2 = 0;
    int sum = 0;
    //输入
    printf("输入两个操作数:");
    scanf("%d %d", &num1, &num2);
    //求和
    sum = Add(num1, num2);
    //输出
    printf("sum = %d\n", sum);
    return 0;
    }

The function is characterized bySimplify code and reuse code.

7. Array

The definition of an array in C language: a collection of elements of the same type

7.1 Definition of array

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

7.2 Array Subscripts

The C language stipulates that each element of the array has a subscript, and the subscript starts from 0.
Arrays can be accessed by subscripting.
for example:

int arr[10] = {0};//If the array has 10 elements, the subscript range is 0-9
insert image description here

7.3 Use of arrays

insert image description here

Guess you like

Origin blog.csdn.net/2201_75366661/article/details/128170368