C Language Basics [Program = Algorithm + Data Structure] - (Data Structure - Basics)

Comments from friends are welcome✨✨ This chapter series is an in-depth thinking and summary of C language, and the content of C language will be continuously updated.


✨Preface

  1. This section explains in detail the form of computer language data representation. The so-called form means that data has two forms of representation: constants and variables.
  2. This section explains the basic data types in the program in detail. The so-called type refers to the arrangement of the data allocation storage unit, including the length of the storage unit (how many bytes it occupies) and the storage form of the data. Different types allocate different lengths and storage forms.
  3. This section explains the basic data structure in the program in detail. The so-called structure is a collection of data elements that have one or more specific relationships with each other. It is a collection of data elements with structure. It refers to the mutual relationship between data elements. Relationships, the organization of data.

The types allowed by the C language are shown in the figure below:
insert image description here


✨Representation of data

1. Constants A

quantity whose value cannot be changed while the program is running is called a constant.
(1) Integer constants . Such as 12345, 0, -12345, etc. are integer constants.
(2) Real constant . There are two representation forms:
① Decimal form, composed of numbers and decimal points. Such as 123.456, 0.345, -56.79, 13.0, etc.
② Exponential form, such as 12.34e3 (represents 12.34×10×10×10), -12.34e-3 (represents -12.34×-10×-10×-10) because it cannot represent the upper or lower corner when inputting or outputting the computer , so it is stipulated that the letter e or E represents the index with a value as low as 10.
But it should be noted: there must be a number before e or E, and there must be an integer after e or E. If it cannot be written as e4, 12e2.5.
(3) Character constants . There are two forms of character constants:
①Common character, a character surrounded by apostrophes, such as: 'a', 'Z', '2', '?', '#'. Cannot be written as 'ab' or '12'.
Please note: But the apostrophe is only a delimiter, and the character constant can only be one character, not including the apostrophe. 'a' and 'A' are different character constants. When a character constant is stored in a computer storage unit, it does not store the character (such as a, z.#, etc.) itself, but stores it in its code (generally using ASCII code), for example, the ASCII code of the character 'a' is 97, Therefore, what is stored in the memory location is 97 (stored in binary form).
②Escape character, in addition to the character constants in the above forms, C language also allows a special form of character constants, which is the character sequence starting with the character "\".
Commonly used special characters starting with "\" are shown in the table below.

escape character character value output result
\’ a single apostrophe ( ' ) output the single apostrophe character '
\" a double apostrophe ( " ) output the double apostrophe character "
\? a question mark ( ? ) Output the question mark character?
\ a backslash ( \ ) output the backslash character \
\a warning (alert) produce audible or visual signals
\b Backspace ( backspace ) Move the current position of the cursor back one character
\f Page change ( form feed ) Move the current position of the cursor to the beginning of the next page
\n new line Move the current position of the cursor to the beginning of the next line
\r carriage return Move the current position of the cursor to the beginning of the line
\t horizontal tab Move the current cursor position to the next Tab position
\v vertical tab Move the current cursor position to the next vertical tab alignment point
\o, \oo, \ooo where o represents an octal digit ASCII character corresponding to the octal code the character corresponding to the octal code
\xh[h…] where represents a hexadecimal digit The ASCII character corresponding to the hexadecimal code The character corresponding to the hexadecimal code

As shown in the above table, the characters in the list are called escape characters, which means to convert the characters after "\" into another meaning. For example, the "n" in "\n" does not represent the letter n but acts as a "newline" character.
(4) String constants . Such as "boy", "123", etc., enclose several characters with double quotes, and the string constant is all the characters in the double quotes (but not including the double quotes themselves). Be careful not to write 'CHINA', 'boy', '123' by mistake. Single apostrophes can only contain one character, and double apostrophes can contain a string.
(5) Symbolic constants . With the #define directive, specify a character name to represent a constant. Such as:
#define PI3.1416 //Note that there is no semicolon at the end of the line.
Such a symbol that represents a constant is called a symbol variable. After precompilation, symbolic constants have all become literal constants (3.1416).
The use of symbolic constants has the following advantages
: 1. Clear meaning
; 2. When you need to change the same constant used in multiple places in the program, you can achieve "one change and all change".

2. Variable

A variable represents a storage unit with a name and specific attributes. It is used to store data, that is, to store the value of a variable. The value of a variable can be changed during program execution.
Variables must be defined before they are used . Specify the name and type of the variable at definition time.
A variable should have a name in order to be referenced. Please pay attention to distinguish between the two different concepts of variable name and variable value. The variable name is actually a storage address represented by a name. When compiling and linking the program, the compiling system assigns a corresponding memory address to each variable name. To get a value from a variable is actually to find the corresponding memory address through the variable name and read data from the storage unit.

3. Constant variables

C99 allows the use of constant variables. The method is to add a keyword const in front of the variable definition, such as:
const int a=3;
define a as an integer variable, specify its value as 3, and when the variable exists During this period its value cannot be changed.
The similarities and differences between a constant variable and a constant are: a constant variable has the basic attributes of a variable: it has a type and occupies a storage unit, but its value is not allowed to be changed. It can be said that a constant variable is an invariant with a name, and a constant is an invariant without a name. Having a name makes it easy to be referenced in the program.
Fourth, the identifier

Simply put, an identifier is the name of an object.
The C language stipulates that identifiers can only be composed of three characters: letters, numbers, and underscores, and the first character must be a letter or an underscore. The following are legal identifiers that can be used as variable names:
sum ,_total .

✨Data Types

In a computer, data is stored in a storage unit, which exists concretely.
The type is the arrangement of data allocation storage units, and different types allocate different lengths and storage forms.

1. Basic type

1.1. Integer type

A byte is equal to 8 bits.
A byte is a data storage size of -128~127, unsigned is 255.

1.1.1. Basic integer type (int)

编译系统分配给int型数据2个字节或4个字节(由具体的C编译系统自行决定)
如果给整型变量分配2个字节,则存储单元中能存放的最大值为0111111111111111
第一位为0代表正数,后面15位为全1,此数值是(2的15次方减1),即十进制32767。最小值为1000000000000000 ,此数值是(负2的15次方),即-32768。
因此一个整型变量的范围是 -32768~32767。超过此范围,就出现数值的“溢出”,
输出的结果显然不正确。如果给整型变量分配4个字节,其能容纳的数值范围为(负2的31次方)至(2的31次方减1),即-2147483648~2147483647

代码实例

A、有符号整型未溢出

#include <stdio.h>
 
int main()
{
    
    
	
	int C_int;
	C_int=2147483647;
	printf("C_int=%d\n",C_int); //打印正数
	C_int=-2147483648;
	printf("C_int=%d\n",C_int); //打印正数
    return 0;
}

图示
insert image description here

B、有符号整型溢出

#include <stdio.h>
 
int main()
{
    
    
	
	int C_int;
	C_int=2147483648;
	printf("  C_int=%d\n",C_int); //打印正数
	C_int=-2147483648;
	printf("  C_int=%d\n",C_int); //打印正数
    return 0;
}

图示
insert image description here
C、查看有符号整型溢出字节

#include <stdio.h>
 
int main()
{
    
    
	
	int C_int;
	C_int=2147483648;
	char *p;
	p=&C_int;
	printf("  C_int=%d\n",C_int);//打印正数
	printf("  %02x %02x %02x %02x \n",p[0],p[1],p[2],p[3]); //打印字节
	C_int=-2147483648;
	printf("  C_int=%d\n",C_int); //打印正数
    return 0;
}

图示

insert image description here

1.1.2、短整型(short int)

类型名为short int或short。编译系统分配给int 4 个字节,短整型2个字节。存储方式与int 型相同。一个短整型变量的值的范围是-32768~32767。

1.1.3、长整型(long int)

类型名为long int 或 long 。编译系统分配给long 4个字节(即32位),因此long int型变量的值的范围-2147483648~2147483647 。

1.1.4、*双长整型(long long int)

类型名为long long int 或 long long ,一般分配8个字节。这是C99新增的类型,但许多C编译系统尚未实现。
C标准没有具体规定各种类型数据所占用存储单元的长度,这是由各编译系统自行决定的。
C标准只要求long型数据长度不短于int型,short型不长于int型。
即 sizeof(short)<=sizeof(int)<=sizeof(long)<=sizeof(long long)

整型变量的符号属性
以上介绍的几种类型,变量值在存储单元中都是以补码形式存储的,存储单元中的第1个二进制代表符号。整型变量的值的范围包括负数到正数。
为了充分利用变量的值的范围,可以将变量定义为“无符号”类型。可以在类型符号前面加上修饰符 unsigned ,表示指定该变量是”无符号整数“ 类型。如果加上修饰符 signed ,则是”有符号类型“。因此,在以上4种整型数据的基础上可以扩展为以下8种整型数据
有符号基本整型 [signed] int
无符号基本整型 unsigned int
有符号短整型 [signed] short [int]
无符号短整型 unsigned short [int]
有符号长整型 [signed] long [int]
无符号长整型 unsigned long [int]
有符号双长整型* [signed] long long [int]
无符号双长整型* unsigned long long [int]

类型 字节数 取值
int() 4 -2147483648 ~ 2147483647
unsigned int() 4 0 ~ 4294967295
short() 2 -32768 ~ 32767
unsigned short() 2 0 ~ 65535
long() 4 -2147483648 ~ 2147483647
unsigned long() 4 0 ~ 4294967295
long long() 8 -9223372036854775808 ~ 9223372036854775807
unsigned long long() 8 0 ~ 18446744073709551615

以上有”*“ 的是C99增加的,方括号表示其中的内容是可选的,即可以有,也可以没有。如果既未指定为signed 也未指定为unsigned的,默认为”有符号类型“。如signed int a 和 int a 等价。
有符号整型数据存储单元中最高位代表数值的符号(0 为正,1为负)。如果指定unsigned (为无符号)型,存储单元中全部二进位(b)都用作存放数值本身,而没有符号。无符号型变量只能存放不带符号的整数,如123,而不能存放负数,如-123。
由于左面最高位不再用来表示符号,而用来表示数值,因此无符号整型变量中可以存放的正数的范围比一般整型变量中正数的范围扩大一倍。

1.1.5、字符型(char)

由于字符是按其代码(整数)形式存储的,因此C99把字符型数据作为整数类型的一种。但是,字符型数据在使用上有自己的特点,因此把它单独列为一节来介绍。

字符与字符代码
字符与字符代码并不是任意写一个字符,程序都能识别。例如代表圆周率的Π在程序中是不能识别的,只能使用系统的字符集中的字符,目前大多数系统采用ASCII字符集。各种字符集(包括ASCII字符集)的基本集都包括了127个字符。其中包括:
字母:大写英文字母A~Z ,小写英文字母a ~ z。
数字:0~9。
专门符号:29个,包括
!“ # & ‘ () * + — , / : ; < = > ? [ \ ] ^ _ ` { | } ~
空格符:空格、水平制表符(tab) 、垂直制表符 、换行 、换页(form feed)。

不能显示的字符:空(null)字符 (以 ’\0‘表示)、警告(以’\a’表示)、退格(以’\b‘表示)、回车(以’\r’表示)等。

前已说明,字符是以整形形式(字符的ASCII代码)存放在内存单元中的。例如
大写字母’A’ 的ASCII 代码是十进制数 65 ,二进制 形式为 1000001 。

代码实例

#include <stdio.h>
 
int main()
{
    
    
	//输出字符A的整型
	printf("'A' is %d\n",'A');
	//输出整型65的字符型 
	printf("65 is %C\n",65);
    return 0;
}

图示

insert image description here

1.1.6、*布尔型(bool)

布尔(Boolean)以英国数学家、布尔代数的奠基人乔治布尔(George Boole)命名。
bool是在C99标准中引入的类型,bool是专门用来表示真假的类型(即:bool类型只有两种情况true或false,也就是真或者假,没有第三种情况)。在引用bool类型前需要引用头文件<stdbool.h>。

代码实例

#include <stdio.h>
#include <stdbool.h> 



int main()
{
    
    
	bool a;
	a=10;	
	printf("bool is %d\n",a);
	a=-1;
	printf("bool is %d\n",a);
	a=0;
	printf("bool is %d\n",a);
	
    return 0;
}

图示

insert image description here

2.1、浮点类型

2.1.1、单精度浮点型(float)

(1) float 型(单精度浮点型)。
编译系统为每一个float型变量分配 4个字节,数值以规范化的二进制数指数形式存放在存储单元中。
在存储时,系统将实型数据分成小数部分和指数部分两个部分,分别存放。小数部分的小数点前面的数为0。

2.1.2、双精度浮点型(double)

(2)double型(双精度浮点型)。
为了扩大能表示的数值范围,用8个字节存储一个double型数据,可以得到15位有效数字,为了提高运算精度,在C语言中进行浮点数的算术运算时,将float型数据都自动转换为double型,然后进行运行。

2.1.3、复数浮点型(float_complex,double_complex,long long_complex)

(3)long double(长双精度)型,不同的编译系统对 long double 型的处理方法不同,有的编译器对long double 型分配16个字节,而有的编译器则对 long double型和double型一样处理,分配8个字节。

二、枚举类型(enum)

如果一个变量只有几种可能的值,则可以定义为枚举(enumeration)类型,所谓“枚举”就是指把可能的值一 一 列举出来,变量的值只限于列举出来的值 的范围内。
声明枚举类型用enum开头。例如:

enum Weekday{sun,mon,tue,wed,thu,fri,sat};
以上声明了一个枚举类型 enum Weekday 。 然后可以用此类型来定义变量。例如:
[枚举类型]enum Weekday [枚举变量] workday , weekend ;
workday 和 weekend 被定义为枚举变量,花括号中的 sun , mon , … ,sat 称为枚举元素或枚举常量。它们是用户指定的名字。枚举变量和其他数值型量不同,
它们的值只限于花括号中指定的值之一。例如枚举变量workday 和 weekend 的值只能是 sun 到 sat 之一。
枚举常量是由程序设计者命名的,用什么名字代表什么含义,完全由程序员根据自己的需要而定,并在程序中作相应处理。
也可以不声明有名字的枚举类型,而直接定义枚举变量,例如:
enum {sun ,mon ,tue,wed,thu,fri,sat} workday ,weekend ;
声明枚举类型的一般形式为
enum [枚举名] {枚举元素列表};
其中,枚举名应遵循标识符的命名规则,上面的Weekday 就是合法的枚举名。

说明:
(1) C编译对枚举类型的枚举元素按常量处理,故称枚举常量。不要因为它们是标识符(有名字)而把它们看作变量,不能对它们赋值。
例如:
sun=0; mon=1; //错误,不能对枚举元素赋值
(2) 每一个枚举元素都代表一个整数,C语言编译按定义时的顺序默认它们的值为0,1,2,3,4,5… 。
也可以人为地指定枚举元素的数值,在定义枚举类型时显式地指定,例如:
enum Weekday {sun = 7 , mon=1,tue,wed,thu,fri,sat} workday ,week_end;
指定枚举常量sun的值为7,mon为1,以后顺序加1,sat 为 6。

2.1、实用场景
宏定义7个别名

#define MON  1
#define TUE  2
#define WED  3
#define THU  4
#define FRI  5
#define SAT  6
#define SUN  7

使用枚举实现

enum DAY
{
    
    
      MON=1, TUE, WED, THU, FRI, SAT, SUN
};

代码实例

#include <stdio.h>
 
enum DAY
{
    
    
      MON=1, TUE, WED, THU, FRI, SAT, SUN
};
 
int main()
{
    
    
    enum DAY day; 
    day = MON;
    printf("The %d day \n",day);
    
    day = TUE;
    printf("The %d day \n",day);
    
    day = WED;   
    printf("The %d day \n",day);
    
    day = THU; 
    printf("The %d day \n",day);
    
    day = FRI;  
    printf("The %d day \n",day);
    
    day = SAT;   
    printf("The %d day \n",day);
    
    day = SUN;   
    printf("The %d day \n",day);
    return 0;
}

图示
insert image description here

三、空类型(void)

void 类型指定没有可用的值。它通常用于以下三种情况下:
1、函数返回为空

C中有各种函数都不返回值,或者您可以说它们返回空。不返回值的函数的返回类型为空。
例如: void printf(int num);
代码实例

#include <stdio.h>
 
void printf_d(int num);

int main()
{
    
    
	printf_d(1); 
	   
    return 0;
}



void   printf_d(int num)
{
    
    
	printf("The num is %d \n",num);
}
 

图示
insert image description here

2、函数参数为空

C中有各种函数不接收任何参数。不带参数的函数可以接收一个void。
例如 int num(void) 。
代码实例

#include <stdio.h>
 
int  num(void);

int main()
{
    
    
	int Data;
	Data=num(); 
	printf("The num is %d\n",Data);	   
    return 0;
}



int  num(void)
{
    
    
	return 1;
}
 

图示

insert image description here

3、指针指向 void
类型为void*的指针代表对象的地址,而不是类型。例如,内存分配函数 void* malloc(size_t size); 返回指向void的指针,可以转换为任何数据类型。
返回值是一个void*(可以简单理解为一个没人知道它是什么类型的指针),一般我们都要对结果进行类型转换;
因为我们的a的类型是int*,而malloc返回的是void*,所以我们要通过(int*)来对malloc进行类型转换来得到我们需要的空间,可以是int也可以是double;

代码实例

#include<stdio.h>
#include<stdlib.h>
main()
{
    
    
	int Data_d;
	int *a;
	//相当于 int a[4]  整型为4个字节
	a=(int*)malloc(4);  //分配空间
	a[0]=0;
	a[1]=1;
	a[2]=2;
	a[3]=3;
	for(int i=0; i<4 ; i++)
    {
    
    
    	printf("a[%d] is %d\n",i,a[i]);  	
    }		
	free(a); //释放空间
	printf("The value after freeing the space \n");
	for(int i=0; i<4 ; i++)
    {
    
    
    	printf("a[%d] is %d\n",i,a[i]);  	
    }
	
}

图示
insert image description here

四、派生类型

4.1、指针类型(*)

每一个学习和使用C语言的人,都应当深入地学习和掌握指针。可以说,不掌握指针就是没有掌握C的精华。
内存区的每一个字节有一个编号,这就是“地址”。
由于通过地址能找到所需的变量单元,可以说,地址指向该变量单元 ,因此,将地址形象化地称为“指针”,意思是通过它能找到以它为地址的内存单元。
访问变量的方式:
1、直接按变量名进行的访问,称为”直接访问“方式。
2、将变量i的地址存放在另一个变量中,然后通过该变量来找到变量i的地址,从而访问i变量。
指针指向-> :指向就是通过地址来体现的,如指针 p 中的值是变量 i 的地址(2000),这样就在 p 和变量 i 之间建立起一种联系,即便通过 p 能知道 i 的地址,从而找到变量 i 的 内存单元。
如果有一个变量专门用来存放另一变量的地址(即指针),则它称为“指针变量”。 指针变量就是地址变量,用来存放地址,指针变量的值是地址(即指针)。

代码实例

#include<stdio.h>

int main()
{
    
    
	int a=100,b=10;             //定义整型变量a,b,并初始化
	int *pointer_1,*pointer_2;  //定义指向整型数据的指针变量 pointer_1,pointer_2 
	pointer_1 = &a;             //把变量a的地址赋给指针变量 pointer_1
	pointer_2 = &b;             //把变量b的地址赋给指针变量 pointer_2
	printf("a=%d,b=%d\n",a,b);  //输出变量a和b的值
	printf(" *pointer_1 = %d , * pointer_2 =%d\n",*pointer_1 ,*pointer_2); 
	                                                  //输出a和b的值
}

图示
insert image description here

4.2、数组类型([ ])

一批具有同名的同属性的数据就组成一个数组。
可知:
(1)数组是一组有序数据的集合。数组中各数据的排列是有一定规律的,下标代表数据在数组中的序号。
(2)用一个数组名(如S)和下标(如15)来唯一地确定数组中的元素。
(3)数组中的每一个元素都属于同一个数据类型。

定义一维数组的一般形式为:
类型符 数组名[常量表达式];

代码实例

#include<stdio.h>
int main()
{
    
    
	int i,a[10];
	for(i=0;i<=9;i++)
	{
    
    
		a[i]=i;  //对数组元素a[0]~a[9]赋值
	}
	for(i=9;i>=0;i--)
	{
    
    
		printf("%d " ,a[i]);	//输出a[9]~a[0]共10个数组元素	
	}
	printf("\n");
	return 0;
}

图示
insert image description here

4.3、结构体类型(struct)

C语言允许用户自己建立由不同类型数据组成的组合型的数据结构,它称为结构体(structre)。
声明一个结构体类型的一般形式为:
struct 结构体名 {成员表列};
类型名 成员名;

定义结构体类型变量
1、先声明结构体类型,再定义该类型的变量

struct Student student_1, student_2;
结构体类型名 结构体变量名

2、在声明类型的同时定义变量

struct 结构体名{ 成员表列
}变量名表列;

3、不指定类型名而直接定义结构体类型变量

struct { 成员表列
}变量名表列;

结构体变量的初始化和引用
(1)在定义结构体变量时可以对它的成员初始化。初始化列表是用花括号括起来的一些常量,这些常量依次
赋给结构体变量中的各成员。注意:是对结构体变量初始化,而不是对结构体类型初始化。
C99 标准允许对某一成员初始化,如:
struct Student b={.name=“Zhang Fang”}; //在成员名前有成员运算符“.”
“.name” 隐含代表结构体变量b中的成员 b.name。其他未被指定初始化的数值型成员被系统初始化为0,字符型成员被
系统初始化为’\0’ ,指针型成员被系统初始化为NULL。
(2)可以引用结构体变量中成员的值,引用方式为
结构体变量名.成员名
在程序中可以对变量的成员赋值,如:
student_1.num = 10010;
"."是成员运算符,它在所有的运算符中优先级最高,因此可以把 student_1.num 作为一个整体来看待,相当于一个变量。上面赋值语句的作用是将整数 10010赋给student_1 变量中的成员num。
(3)如果成员本身又属一个结构体类型,则要用若干个成员运算符,一级一级地找到最低的一级成员。只能对最低级的成员进行赋值或存取以及运算。
(4)对结构体变量的成员可以像普通变量一样进行各种运算(根据起类型决定可以进行的运算)
(5)同类的结构体变量可以互相赋值
(6)可以引用结构体变量成员的地址,也可以引用结构体变量的地址

代码实例

#include <stdio.h>
 
struct Books
{
    
    
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} book = {
    
    "C language ", "QIAO YI BO YI", "Programming language", 123456};
 
int main()
{
    
    
    printf("title : %s\nauthor: %s\nsubject: %s\nbook_id: %d\n", book.title, book.author, book.subject, book.book_id);
}

图示
insert image description here

4.4、共用体类型(union)

几个不同的变量共享同一段内存的结构,称为“共用体”类型的结构。
定义共用体类型变量的一般形式为
union 共用体名
{ 成员表列
}变量表列;

可以看到,“共用体”与 “结构体”的定义形式相似。但它们的含义是不同的。
结构体变量所占内存长度是各成员占的内存长度之和。每个成员分别占有其自己的内存单元。而共用体变量所占的内存长度等于最长的成员的长度。

代码实例

#include <stdio.h>
#include <string.h>
 
union Data
{
    
    
   int i;
   char str[4];
};
 
int main( )
{
    
    
   union Data data;        
 
   printf( "Memory size occupied by data : %d\n", sizeof(data));
   
   data.i=1;
   
   for(int i=0;i<4;i++)
   {
    
    
    printf("%02x ",data.str[i]);   //获取内存中的数据
   }
   
   return 0;
}

图示

insert image description here

4.5、函数类型

函数就是功能。每一个函数用来实现一个特定的功能,函数的名字应反映其代表的功能。
(1)一个C程序由一个或多个程序模块组成,每一个程序模块作为一个源程序文件。
对较大的程序,一般不希望把所有内容全放在一个文件中,而是将它们分开放在若干个源文件中,由若干个源程序文件组成一个C程序。
一个源程序文件可以为多个C程序共用。
(2)一个源程序文件由一个或多个函数以及其他有关内容(如指令、数据声明与定义等)组成。一个源程序文件是一个编译单位,在程序编译时是以源程序文件为单位进行编译的
,而不是以函数为单位进行编译的。
(3)C程序的执行是从main函数开始的,如果在main函数中调用其他函数,在调用后流程返回到main 函数,在main函数中结束整个程序的运行。
(4)所有函数都是平行的,即在定义函数时是分别进行的,是相互独立的的。一个函数并不从属于另一个函数,即函数不能嵌套定义。函数间可以互相调用,但不能调用main函数。
main函数是被操作系统调用的。
(5)从用户使用的角度看,函数有两种。
①库函数,它是由系统提供的,用户不必自己定义,可直接使用它们。应该说明,不同的C语言编译系统提供的库函数数量和功能会有一些不同,当然许多基本的函数是共同的。
②用户自己定义的函数。它是用以解决用户专门需要的函数。
(6)从函数的形式看,函数分两类。
①无参函数
无参函数可以带回或不带回函数值,但一般以不带回函数值的居多。
②有参函数。
在调用函数时,主调函数在调用被调用函数时,通过参数向被调用函数传递数据,此时有参函数应定义为与返回值相同的类型。

code example

#include <stdio.h>
 
/* 函数声明 */
int max(int num1, int num2);
 
int main ()
{
    
    
   /* 局部变量定义 */
   int a = 100;
   int b = 200;
   int ret;
 
   /* 调用函数来获取最大值 */
   ret = max(a, b);
 
   printf( "Max value is : %d\n", ret );
 
   return 0;
}
 
/* 函数返回两个数中较大的那个数 */
int max(int num1, int num2) 
{
    
    
   /* 局部变量声明 */
   int result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
}

icon

insert image description here

✨Summary

In a computer, data is stored in a storage unit, which exists concretely. Moreover, the storage unit is composed of finite bytes, and the range of data stored in each storage unit is limited, and it is impossible to
store "infinite" numbers, nor can it store recurring decimals. The so-called type refers to the arrangement of data allocation storage units, including the length of the storage unit (how many bytes it occupies) and the storage form of the data.
Different types allocate different lengths and storage forms.

Guess you like

Origin blog.csdn.net/weixin_44759598/article/details/128091766