Getting to know C language for the first time (below)

8. Function

C 语言中的函数是一段可以重复使用的代码,用来独立地完成某个功能,它可以接收用户传递的参数,也可以不接收。它由两部分组成:函数头和函数体。
**函数头:**包括返回值类型、函数名和形参声明;
**函数体:**复合语句,仅在摸个函数中使用的变量,原则上应在该函数中声明和使用,但要注意不能声明和形参同名的变量,否则会发生变量名冲突的错误。

code demo

#include <stdio.h>
int main()
{
    
    
    int a = 0;
    int b = 0;
    int c= 0;
    printf("输入两个操作数:");
    scanf("%d %d", &a, &b);
    c = a+b;
    printf("sum = %d\n", c);
    return 0; }
    //缺点:每两个数相加都要重复上述代码.如果需要执行复杂的功能,每次都要重复写很多代码,很麻烦
上述代码,写成函数如下:
#include <stdio.h>
int Add(int x, int y) 
{
    
    
   int z = x+y;
   return z; 
}
int main()
{
    
    
   int a= 0;
   int b = 0;
   int c= 0;
    printf("输入两个操作数:");
    scanf("%d %d", &a, &b);
    c = Add(a, b);
    printf("sum = %d\n", c);
    return 0; 
    }
//函数可以反复使用,每次只需调用Add函数,就可以完成加法。其他复杂的功能也一样,调用函数,就可以达到效果,简化了代码

9. Arrays

(1) Array definition:
In program design, for the convenience of processing, several variables with the same type are organized in an orderly form. These ordered collections of homogeneous data elements are called arrays.
E.g

arr[5]={
    
    1,2,3,4,5};
//arr数组大小为5,依次放了1,2,3,4,5

(2) Array subscript The
insert image description here
array subscript starts from 0 and increases by 1 in turn

( 3) The use of arrays

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

insert image description here

10. Operators

insert image description here
unary operator

!           逻辑反操作
-           负值
+           正值
&           取地址
sizeof      操作数的类型长度(以字节为单位)
~           对一个数的二进制按位取反
--          前置、后置--
++          前置、后置++
*           间接访问操作符(解引用操作符) 
(类型)       强制类型转换

relational operator

>
>=
<
<=
!=   用于测试“不相等”
==      用于测试“相等”

logical operator

&&     逻辑与
||     逻辑或

conditional operator

exp1 ? exp2 : exp3

comma expression

exp1, exp2, exp3, …expN

Subscript references, function calls, and struct members

[] () . ->

11. Common Keywords

(1) Keyword typedef (type redefinition)

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

(2) The keyword static
in the C language:
static is used to modify variables and functions
a. Modified local variables - called static local variables

#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; 
}

insert image description here

b. Decorate global variables - called static global variables

int g_val = 2018;
int main()
{
    
    
    printf("%d\n", g_val);
    return 0; }

insert image description here

c**. Decorated functions - called static functions**

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


//代码2
//add.c
static int Add(int x, int y) 
{
    
    
    return c+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.
Conclusion :
A function is modified by static, so that this function can only be used in this source file and cannot be used in other source files.

12. #define define constants and macros

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

insert image description here

13. Pointers

(1) Memory
The memory is a particularly important memory on the computer, and the operation of the programs in the computer is carried out in the memory.
Therefore, in order to use the memory efficiently, the memory is divided into small memory units, and the size of each memory unit is 1 byte.
In order to effectively access each unit of memory, the memory unit is numbered, and these numbers are called the
address of the memory unit.

变量是创建内存中的(在内存中分配空间的),每个内存单元都有地址,所以变量也是有地址的。
取出变量地址如下:
#include <stdio.h>
int main()
{
    
    
 int num = 10;
 &num;//取出num的地址
    //注:这里num的4个字节,每个字节都有地址,取出的是第一个字节的地址(较小的地址)
 printf("%p\n", &num);
 return 0;
 }
 //打印地址,%p是以地址的形式打印

insert image description here
The storage of the address requires the definition of a pointer variable

int num = 10;
int *p;//p为一个整形指针变量
p = &num;

Examples of pointer usage:

#include <stdio.h>
int main()
{
    
    
 int num = 10;
 int *p = &num;
 *p = 20;
    return 0; 
}

Take the integer pointer as an example and extend it to other types

#include <stdio.h>
int main()
{
    
    
 char ch = 'w';
 char* pc = &ch;
 *pc = 'q';
 printf("%c\n", ch);
    return 0; 
 }
 //输出结果为q

(2) The size of the pointer variable

//指针变量的大小取决于地址的大小
//32位平台下地址是32个bit位(即4个字节)
//64位平台下地址是64个bit位(即8个字节)
#include <stdio.h>
int main()
{
    
    
    printf("%d\n", sizeof(char *));
    printf("%d\n", sizeof(short *));
    printf("%d\n", sizeof(int *));
    printf("%d\n", sizeof(double *));
    return 0; 
}

14. Structures

结构体是C语言中特别重要的知识点,结构体使得C语言有能力描述复杂类型。
比如描述学生,学生包含: 名字+年龄+性别+学号 这几项信息。

Only structures can be used to describe them here.
E.g:

struct Stu
{
    
    
    char name[10];//名字
    int age;      //年龄
    char sex[5];  //性别
    char id[10]//学号
}

Guess you like

Origin blog.csdn.net/qq_62316056/article/details/124055817