C language - basic knowledge summary 2

Table of contents

1. String + escape character

1. String

2. Escape characters 

2. Notes  

3. Array

1. Definition of array 

2. The subscript of the array

3. The use of arrays 

Fourth, the operator 

 5. Common keywords  

1. Keyword typedef

2. The keyword static 

6. #define defines constants and macros 


1. String + escape character


1. String

A string of characters enclosed in double quotes is called a string literal, or string for short. In C language, character arrays are usually used to store strings. (The end mark of the string is an escape character of \0 . When calculating the length of the string, '\0' is the end mark and is not counted as the content of the string.)

The following code highlights the significance of '\0' in strings :

#include<stdio.h>
#include<string.h>
int main()
{
	char a[] = "bit";
	char b[] = { 'b','i','t' };
	char c[] = { 'b','i','t','\0' };
	printf("%s\n", a);
	printf("%s\n", b);
	printf("%s\n", c);
	printf("%d\n", strlen(b));
	printf("%d\n", strlen(c));
	return 0;
}

The result of its operation is:

Judging from the above results:

Comparing the output of the second line with the output of the third line, we can see that when the string does not end with '\0', random values ​​will be output later. In the absence of '\0' at the end, the length 15 of the array b output in the fourth line is a random value.

In the above code, the strlen function used is a string function, which is used to find the length of the string, that is, from the given address, count the characters backward until the end of '\0', '\0' is not Statistics included.

For more detailed string functions, you can read this article:  http://t.csdn.cn/d321I

2. Escape characters 

First of all, let us think about this problem, print a directory on the screen: c:\code\test.c, how should we write the code at this time?

Let's try to output directly with printf first, the code is as follows:

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

The result of its operation is:

Obviously it is not the result we want. If we want to output the correct result, we need to use escape characters at this time .

The escape character, as the name suggests, is to change the meaning. Some escape characters are listed below:

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 Form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\ddd ddd represents 1~3 octal numbers. For example: \130 means character X
\xdd dd represents two hexadecimal digits. For example: \x30 means character 0

The escape characters \ddd and \xdd need to use the ASCLL and base conversion of the characters. If you are not familiar with the ASCLL table, you can go down by yourself and carefully record the ASCLL values ​​​​of commonly used characters.

Then print a directory on the screen: c:\code\test.c The correct code is:

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

The result of the operation is: 

For the escape character \?, its analysis code is as follows:

#include <stdio.h>
int main()
{
	//\?是为了不让?被解析为三字母词
	//三字母词 - ??) ——>  ]
	printf("%s\n","(are you ok\?\?)");
	//输出结果为:(are you ok??),否则结果为:(are you ok]
	return 0;
}

 Let's look at an example:

#include <stdio.h>
int main()
{
    printf("%d\n", strlen("abcdef"));
    // \x11和\62被解析成一个转义字符
    printf("%d\n", strlen("c:\test\x11\628\test.c"));
    return 0;
}

What should be the output of the above code?

The result is 6,15. The first output is easy to understand, that is, the number of characters, so why is the second output 15? The answer is that \t, \x11 and \62 in the string are all escape characters.

2. Notes  


 Comments come in two flavors:

  • C- style comments /*xxxxxx*/, defect: cannot nest comments
  • C++ style comment //xxxxxxxx, you can comment one line or multiple lines

for example: 

#include <stdio.h>
int Add(int x, int y)
{
    return x+y;
}
/*C语言风格注释
int Sub(int x, int y)
{
    return x-y;
}
*/
int main()
{
    //C++注释风格
    //int a = 10;
    //调用Add函数,完成加法
    printf("%d\n", Add(1, 2));
    return 0;
}

 

3. Array


Arrays are defined in C language as follows: a collection of elements of the same type.

1. Definition of array 

int arr[10] = {1,2,3,4,5,6,7,8,9,10};//定义一个整形数组,最多放10个元素

The array arr, where the square brackets [ ] can also have no size, the size is determined according to the number of elements given later.

2. The subscript of the array

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};
//如果数组有10个元素全为0,下标的范围是0-9

 

3. The use of arrays 

code:

#include <stdio.h>
int main()
{
 int i = 0;
 int arr[10] = {1,2,3,4,5,6,7,8,9,10};
 for(i=0; i<10; i++)
 {
       printf("%d ", arr[i]);
 }
 printf("\n");
    return 0;
}

Fourth, the operator 


arithmetic operator             +        -            *       /        %                                                                                       
shift operator         >>        <<
bitwise operator         & ^ |        (like the shift operator, it operates on binary bits)
assignment operator =        +=        -=        *=        /=        &=        |=        >>=        <<=
relational operator >        >=        <        <=        !=        ==
logical operator && (logical AND)        || (logical OR)
conditional operator
exp1 ? exp2 : exp3
comma expression
exp1, exp2 exp3,...expn  
Subscript references and function calls [ ]        ( )
structure member       ->

 / Division in Arithmetic Operators:

  1. The result of the calculation is the quotient after divisibility
  2. Division includes integer division and floating-point division. In floating-point division, at least one of the divisor and dividend must be a floating-point number.

 Modulo operator %: The result is the remainder, which is only applicable to integers .

The usage code of the conditional operator: 

#include<stdio.h>
int main()
{
	int a = 0;
	int b = 0;
	//输入
	scanf("%d %d", &a, &b);
	int m = 0;
	//(a>b) ? (m = a) : (m = b);
	m = (a > b ? a : b);

	/*if (a > b)
		m = a;
	else
		m = b;*/
	printf("%d\n", m);

	return 0;
}

That is, when the expression exp1 is true, the expression exp2 is executed; otherwise, the expression exp3 is executed.

The usage code of the comma expression: 

#include<stdio.h>
int main()
{
	int a = 3;
	int b = 2;
	int c = 5;
	//逗号表达式,是从左向右依次计算的,逗号表达式的结果是最后一个表达式的结果
	int d = (a+=3, b=5, c=a+b, c-4);
	//      a=6    b=5  c=11   7
	printf("%d\n", d);
	return 0;
}

Unary operator: 

                       logical inversion
-                         negative value
+                         positive value
&                         take address
sizeof                         the type length of the operand in bytes
~                         Bitwise inverse of a number
--                         front, rear--
++                         Front, rear++
*                         indirect access operator (dereference operator)
(type)                         cast

 
5. Common keywords  


auto (automatic)    break   case      char         const continue default do         double else enum extern (declaration of external symbols)       float         for   goto       if       int     long         register (register)     return   short signed     sizeof           static struct         switch       typedef union  (union type)     unsigned        void        volatile        while
C 语言提供了丰富的关键字,这些关键字都是语言本身预先设定好的,用户自己是不能创造关键字的。

1、关键字typedef

typedef 顾名思义是类型定义,这里应该理解为类型重命名。

代码:

//将unsigned int 重命名为uint_32, 所以uint_32也是一个类型名
typedef unsigned int uint_32;
int main()
{
    //观察num1和num2,这两个变量的类型是一样的
    unsigned int num1 = 0;
    uint_32 num2 = 0;
    return 0;
}

2、关键字static 

C 语言中:
static 是用来修饰变量和函数的
        1. 修饰局部变量 - 称为静态局部变量
        2. 修饰全局变量 - 称为静态全局变量
        3. 修饰函数 - 称为静态函数 
  • 修饰局部变量

static 修饰局部变量的时候,本来一个局部变量是存放在栈区的,如果被static修饰就存储到静态区了,static 修饰局部变量改变了变量的存储类型(位置),使得这个静态变量的生命周期变长了,直到程序结束才结束但是作用域不变。

代码:

//代码1
#include <stdio.h>
void test()
{
    int i = 0;
    i++;
    printf("%d ", i);
}
int main()
{
 int i = 0;
    for(i=0; i<10; i++)
   {
        test();
   }
    return 0;
}
//运行结果为10个1
//代码2
#include <stdio.h>
void test()
{
    //static修饰局部变量
    static int i = 0;
    i++;
    printf("%d ", i);
}
int main()
{
    int i = 0;
    for(i=0; i<10; i++)
   {
        test();
   }
    return 0;
}
//运行结果为1 2 3 4 5 6 7 8 9 10
  • 修饰全局变量 

 全局变量具有外部链接属性,所以在其他源文件内部依然可以使用(方法要正确),static修饰全局变量,改变了这个全局变量的链接属性,由外边链接属性变成了内部链接属性,这个静态变量只能在自己所在的源文件内部使用,不能在其他源文件内部使用了,感觉像是作用域变小了。

代码:

//代码1
//add.c
int g_val = 2023;
//test.c
int main()
{
    printf("%d\n", g_val);
    return 0;
}
//代码2
//add.c
static int g_val = 2023;
//test.c
int main()
{
    printf("%d\n", g_val);
    return 0;
}

代码1正常,代码2在编译的时候会出现连接性错误。  

  • 修饰函数 

 A static modified function is the same as a static modified global variable. The function has an external link attribute, but if it is modified by static, it becomes an internal link attribute, so that this function can only be used inside the source file where it is located, and cannot be used in other Used internally by the file.

code:

//代码1
//add.c
int Add(int x, int y)
{
    return x+y;
}
//test.c
int main()
{
    printf("%d\n", Add(2, 3));
    return 0;
}
//代码2
//add.c
static int Add(int x, int y)
{
    return x+y;
}
//test.c
int main()
{
    printf("%d\n", Add(2, 3));
    return 0;
}

Code 1 is normal, and code 2 will have a connectivity error when compiling  .

 Here we will first focus on explaining  the two keywords typedef  and  static  .

6. #define defines constants and macros 


 Look directly at the code for understanding:

//define定义标识符常量
#define MAX 1000
//define定义宏
#define ADD(x, y) ((x)+(y))
#include <stdio.h>
int main()
{
    int sum = ADD(2, 3);
    printf("sum = %d\n", sum);
    //输出sum = 5
    sum = 10*ADD(2, 3);
    printf("sum = %d\n", sum);
    //输出sum = 50
    return 0;
}

 The macros defined by #define are similar to the definitions of functions.

Macros defined by #define:

#define ADD(x, y) ((x)+(y))
//ADD为宏的名字
//(x,y)中x,y是参数
//((x)+(y))是宏的实现体

Function definition:

int ADD(int x,int y)
{
    return x+y;
}

This time the summary is here first, and I look forward to the likes and support of my friends. Your encouragement is the driving force for me to move forward!

 

 

Guess you like

Origin blog.csdn.net/m0_61876562/article/details/130072254