当前课程知识点:C Programming > Chapter 3 Programming in C > 3.8 Basic Data Types of C Language > 3.8 Basic Data Types of C Language.mp4
大家好
我是云南大学信息学院丁海燕老师
欢迎走进C语言程序设计课堂
今天我们讲解C语言基本数据类型
用计算机解决实际问题时
程序处理的主要对象是数据
对不同的问题而言
涉及的数据类型也是多种多样
例如23、-15.6这样的数值型数据
或者’A’、"student"这样的用字符表示的非数值数据。
在计算机中
不同类型的数据在内存中存储的形式是不同的
系统对它们进行的操作处理也不相同
为了满足系统对各种类型数据操作的需要
C语言引入了数据类型的概念
要求对C程序中使用的每个数据都必须指出它的类型
•所谓类型,就是对数据分配存储单元的安排
包括存储单元的长度(占多少字节)以及数据的存储形式
•不同的类型分配不同的长度和存储形式
C语言的数据结构是以数据类型形式出现的
不同数据类型的数据在内存中存储的方式不同
占用的内存字节数也不同
因此
在编写程序时需要根据不同的需要选择合适的数据类型
C的数据类型分为基本类型
构造类型和其他类型,如下所示
基本类型:整型,字符型
实型(浮点型)(单精度型和双精度型)
构造类型:数组类型
结构体类型,共用体类型,枚举类型
其他类型:指针类型,空类型
由以上这些数据类型还可以构成更复杂的数据结构
例如利用指针和结构体类型可以构成表
树、栈等复杂的数据结构
在程序中对用到的所有数据都必须指定其数据类型
下面讲解整型数据在内存中的存储
不同的编译系统为int变量开辟的内存单元大小不同
32位编译系统的VC++和codeblocks中
整型数据除短整型占2个字节外,其它都是占4个字节
带符号整数在内存中存放时最高一位为“符号位”
符号位为0表示正数,为1表示负数
以下仅讨论短整型数据的内存形式
1. 正整数
正整数在内存中用“原码”形式存放
求原码的方法是
将该整数的绝对值直接转换成相应的二进制数
最左边一位为符号位(0或1)
并用0在二进制数前补足16位即可
例如:整数5的原码为:00000000 00000101
整数-5的原码为:10000000 00000101
2. 负整数
负整数在内存中以“补码”形式存放
求负数补码的方法是
求出-5的原码,将所得原码,
符号位不变,其他各位按位取反(0变1,1变0)得到反码
末位再加1,即为-5的补码。
例如,-5的原码、反码
补码在内存中的存放形式如表3.7所示
将负数的补码(符号位为1)
转换成相应整数的方法
将补码按位取反再加1
然后转换成十进制数再加上“负号”即可
例如:将二进制数10000000 00000000
转换成带符号十进制数的方法是:
将补码10000000 00000000按位取反得到:
01111111 11111111
再加1得到:10000000 00000000,
直接将二进制数转换成十进制数32768
再加上负号:-32768
【注意】
short int 型变量能存放的最小整数为-32768
内存形式为:10000000 00000000
正整数的原码、反码、补码相同
3. 无符号整数
无符号整数在内存中以“原码”形式存放
此时全部二进制位均用来存储整数
即最高位不再用来存放整数的符号
当一个二进制数最高位为0时
将其转换成带符号整数和无符号整数得到的值是相同的
当一个二进制数最高位为1时
将其转换成带符号整数和无符号整数得到的值是不同的
见表3.8所示
整型的类型说明符及相关信息如下
(以VC++和codeblocks为例)
带符号整型 [signed] int
带符号短整型 [signed] short [int]2个字节
带符号长整型 [signed] long [int]4个字节
无符号整型 unsigned int 4个字节
无符号短整型 unsigned short [int]2个字节
无符号长整型 unsigned long [int]4个字节
下面介绍实型数据在内存中的存储
实型数据是指带小数点和小数部分的数据
实型数也称为浮点数
C语言中实型数据有单精度和双精度两种类型
与整型数据存储不同的是
系统将实型数据的存储位划分为两部分
分别存储它的小数(尾数)
和指数(阶码)对应的二进制数。
为了便于计算机中小数点的表示
规定将浮点数写成规范化的形式
即尾数的绝对值大于或等于0.1并且小于1
从而唯一地规定了小数点的位数
采用这种存储方式
可以大大扩展实型数据的数值表示范围。
图示给出实型数据在内存中存放的一般形式
分为4部分
小数的符号位,小数部分,指数的符号位,指数部分
例如,二进制数-1010.01以规格化形式表示为
表3.9列出了VC++环境下C语言实型数据的类型及规定
C语言标准并未具体规定各种实型数据所占字节数
也不规定具体小数和指数部分的位数
一般而言,数据表示的有效数字多
精度就高;而指数部分位数多
则表示的数据范围更大
单精度实型 float 32 7~8 10-37~1038
双精度实型 double 64 15~16 10-307~10308
下面介绍字符型数据的存储
字符型数据是指字母
数字和各种符号等用ASCII值表示的字符
在C语言中用类型标识符char表示
系统为char类型数据分配1个字节的存储单元(8bit)
用于存放字符的ASCII值
例如,’b’是一个 char类型数据,称为字符常量
系统为它在内存中分配一个字节的存储单元
存放小写字母b的ASCII值
(即二进制数01100010,表示成十进制数是98),
存储形式如下:
字符型数据的类型及规定如表3.10所示
字符型 char 8 0~255
有符号字符型 signed char 8 -128~127
无符号字符型 unsigned char 8 0~255
【注意】
(1)字符数据值为0~127时存储的是基本ASCII值
为128~255时存储的是扩展ASCII值
(2)在C语言中
多数情况下字符数据和整型数据可以互相通用
即当给一个字符变量赋值98时
等价于将字符’b’赋给它
反之将字符’b’赋给一个整型变量时
等价于将数值98赋给它
(3)由于字符数据在内存中以ASCII值存储
是一个整型数据形式
不同的C编译系统对其处理是不同的
有些视其为有符号的
有些视其为无符号的
因此C语言允许使用signed
和unsigned修饰char类型数据
但只有在按照整型数据形式输出时(%d)
才能显示不同定义的区别
好了,同学们
C语言基本数据类型我们就学习到这儿
下节课再见
-1.1 The development and characteristics of C language
--1.The development and characteristics of C language .mp4
--1.1 Self test questions
-1.2 A simple C Language program
--1.2 A simple C Language program.mp4
--【Source program】 Example 1.1 output a line of text Hello, world! "
--【Source program】 Example 1.2 program composed of multiple functions, find the larger of two integers
--1.2 Self test questions
-1.3 Program, Programming Language and C Program Running Steps
--1.3 Program, Programming Language and C Program Running Steps.mp4
--Running steps of C source program in CodeBlocks
--1.3 Self test questions
-chapter 1 Self-Test
-2.1 The Concept and Description of Algorithms
--2.1 The Concept and Description of Algorithms.mp4
--2.1 Self test questions
-2.2 Examples of Simple Algorithms, Computational Thinking and Structured Programming
--2.2 Examples of Simple Algorithms, Computational Thinking and Structured Programming.mp4
--2.2 Self test questions
-Chapter 2 Self test questions
-3.1 Simple Structure and Identifier of C Language Program
--3.1 Simple Structure and Identifier of C Language Program.mp4
--【Source program】 Example 3.1 input two integers and output the sum of two numbers.
--【Source program】 Example 3.2 input two integers and output the average value.
--【Source program】 Example 3.3 output the value of the character variable.
--3.1 Self test questions
-3.2 Constants, Variables and Assignments
--3.2 Constants, Variables and Assignments.mp4
--3.2 Self test questions
-3.3 Arithmetic, assignment, increment and decrement operators
--3.3 Arithmetic, assignment, increment and decrement operators.mp4
--3.3 Self test questions
-3.4 Conditions, commas, addresses, byte operators, and mixed operations among various numerical data
--3.4 Conditions, commas, addresses, byte operators, and mixed operations among various numerical data
--【Source program】 example of sizeof
--3.4 Self test questions
-3.5 Input and Output Examples and Character Input and Output
--3.5 Input and Output Examples and Character Input and Output.mp4
--【Source program】 Example. Find the root of quadratic equation of one variable.
--【Source program】 Example 1. Successively output three characters of BOY.
--【Source program】Example 2. Input 3 characters of BOY from the keyboard and output them to the screen
--3.5 Self test questions
-3.6 Formatted output printf function
--3.6 Formatted output printf function.mp4
--【Source program】 Example 3.4 output of integer data.
--【Source program】 Example 3.5 real data output.
--【Source program】 Example 3.6 character data output.
--【Source program】 Example 3.7 uses %s to output a string.
--3.6 Self test questions
-3.7 Formatted input scanf function
--3.7 Formatted input scanf function.mp4
--【Source program】 Example 3.8 Input and output integer data
--【Source program】Example 3.9 Input and output single precision and double precision real data.
--【Source program】Example 3.10 input and output character data.
--【Source program】Example 3.11 input and output string.
--3.7 Self test questions
-3.8 Basic Data Types of C Language
--3.8 Basic Data Types of C Language.mp4
--3.8 Self test questions
-Chapter 3 Self test questions
-Operator and expression self test questions
-4.1 Relational operators, logical operators, and if statements
--4.1 Relational operators, logical operators, and if statements.mp4
--【source example】Example 4.1 Find the root of the quadratic equation of one variable.
--【Source program】Example 4.2 Input two real numbers and output them from small to large.
--4.1 Self test questions
-4.2 Switch Statement
--【Source program】Example 4.3 uses switch statement to implement simple menu program
--【Source program】Example 4.4 converts centesimal score into the corresponding grade system score.
--4.2 Self test questions
-4.3 Examples of Selection Structural Programs
--4.3 Examples of Selection Structural Programs.mp4
--【Source program】 Example 4.5 determines whether a year is a leap year.
--【Source program】 Example 4.6 find the solution of quadratic equation of one variable.
--【Source program】Example 4.7 calculate the transportation cost for the user.
--4.3 Self test questions
-Chapter 4 Self test questions
-5.1 While and Do-While Statement
--5.1 While and Do…while statement.mp4
--【Source program】example 5.1 Use while statement to find 1 + 2 + 3 + +100
--【Source program】example 5.2 using do while statement to find 1 + 2 + 3 + +100
--5.1 Self test questions
-5.2 For Statement
--【Source program】example 5.3 Use for loop to find 1 + 2 + 3 +100
--5.2 Self test questions
-5.3 Change the State of Loop Execution and Nested Loop
--5.3 Change the state of loop execution and nested loop .mp4
--【Source program】example 5.4 collecting charity donations
--【Source program】example 5.5 output a number between 100 and 200 that cannot be divided by 3.
--【Source program】multiplication table
--【Source program】example 5.6 Output the following 4*5 matrix.
--5.3 Self test questions
-5.4 Loop Structure Program example 1
--5.4 Loop structure program example 1.mp4
--5.4 Self test questions
-5.5 Loop Structure Program example 2
--5.5 Loop structure program example 2.mp4
--【Source program】example 5. Output the following figure.
--【Source program】example6. The problem of 100 chickens and 100 coins
--【Source program】example 7. Find the approximate value of PI
--5.5 Self test questions
-Chapter 5 Self test questions
-6.1 Definition, Reference and Initialization of One-Dimensional Arrays
--6.1 Definition, Reference of One-Dimensional Arrays.mp4
--6.1 Self test questions
-6.2 One-Dimensional Array Programming
--6.2 One-Dimensional Array Programming.mp4
--【Source program】example 1. Fibonacci sequence
--【Source program】example 2. Find the maximum value and the minimum value.
--【Source program】example 3 exchanging array elements in reverse order.
--【Source program】example 4. Bubble sorting.
--6.2 Self test questions
-6.3 Definition, Reference and Initialization of Two-Dimensional Arrays
--6.3 Definition, Reference of Two-Dimensional Arrays.mp4
--6.3 Self test questions
-6.4 Two-Dimensional Array Programming
--6.4 Two-Dimensional Array Programming.mp4
--【Source program】example 1. Get the average score of each subject.
--【Source program】Example 2. The elements of row and column of a 2D array are interchanged
--6.4 Self test questions
-6.5 Definition, initialization and input and output of character arrays
--6.5 Definition, initialization and input and output of character arrays .mp4
--【Source program】Example 6.6 Output a known string.
--【Source program】Example 6.7 Output a diamond.
--【Source program】Example 6.10 Sorting of strings.
--6.5 Self test questions
-6.6 String Processing Function
--6.6 String Processing Function.mp4
--6.6 Self test questions
-6.7 Character Array Programming
--6.7 Character Array Programming.mp4
--6.7 Self test questions
-Chapter 6 Self test questions
-7.1 Function Concept and How to Define and Call Functions
--7.1 Function Concept and How to Define and Call Functions.mp4
--7.1 Self test questions
-7.2 Data Transfer in Function Call, Call Procedure and Function Return Value
--7.2 Data Transfer in Function Call, Call Procedure and Function Return Value.mp4
--7.2 Self test questions
-7.3 Declarations of called functions and nested calls to functions
--7.3 Declarations of called functions and nested calls to functions .mp4
--【Source program】Example 7.4 use a function to find the sum of t
--7.3 Self test questions
-7.4 Recursive call to function
--7.4 Recursive call to function.mp4
--【Source program】Example 7.6. How old is the fifth student?
--【Source program】Example 7.7 Use recursion method to find n!
--7.4 Self test questions
-7.5 Array as function parameter (1)
--7.5 Array as function parameter (1).mp4
--【Source program】Example 7.10
--7.5 Self test questions
-7.6 Array as function parameter (2)
--7.6 Array as function parameter (2).mp4
--【Source program】Selection sorting.
--【Source program】Example 7.13 find the maximum of a 3×4 matrix.
--7.6 Self test questions
-7.7 Local and global variables, internal and external functions
--7.7 Local and global variables, internal and external functions.mp4
--【Source program】Example 7.14
--7.7 Self test questions
-7.8 The Survival Period of Variables and the Storage Mode of Local Variables
--7.8 The Survival Period of Variables and the Storage Mode of Local Variables.mp4
--【Source program】Example 7.17
--7.8 Self test questions
-7.9 Storage Categories of Global Variables
--7.9 Storage Categories of Global Variables.mp4
--7.9 Self test questions
-Chapter 7 Self test questions
-8.1 Pointer Concept, Definition and Reference of Pointer Variables
--8.1 Pointer Concept, Definition and Reference of Pointer Variables.mp4
--8.1 Self test questions
-8.2 Pointer variables as function parameters
--8.2 Pointer variables as function parameters.mp4
--【Source program】Example 8.3. Exchange two data with function call.
--8.2 Self test questions
-8.3 Pointer of array element, operation of pointer and reference of array element by pointer
--8.3 Pointer of array element, operation of pointer and reference of array element by pointer.mp4
--【Source program】Point to array elements with pointer variables.
--8.3 Self test questions
-8.4 Using Array Name as Function Parameter
--8.4 Using Array Name as Function Parameter.mp4
--【Source program】Store array elements in reverse order.
--8.4 Self test questions
-8.5 Reference to multidimensional arrays by pointers
--8.5 Reference to multidimensional arrays by pointers.mp4
--【Source program】Pointer variable pointing to one-dimensional array.
--8.5 Self test questions
-8.6 Referencing strings through pointers
--8.6 Referencing strings through pointers.mp4
--【Source program】Reference string by pointer.
--8.6 Self test questions
-8.7 Character pointer as function parameter
--8.7 Character pointer as function parameter.mp4
--【Source program】Character pointer as function parameter.
--8.7 Self test questions
-8.8 Pointer pointing to function
--8.8 Pointer pointing to function.mp4
--【Source program】Pointer variable pointing to the function.
--8.8 Self test questions
-8.9 Functions that return pointer values
--8.9 Functions that return pointer values.mp4
--【Source program】Intercept substring.
--8.9 Self test questions
-8.10 Pointer arrays and multiple pointers
--8.10 Pointer arrays and multiple pointers.mp4
--【Source program】Sorting of strings.
--8.10 Self test questions
-8.11 Dynamic memory allocation and pointer variables pointing to it
--8.11 Dynamic memory allocation and pointer variables pointing to it.mp4
--【Source program】Dynamic memory allocation.
--8.11 Self test questions
-Chapter 8 Self test questions
-9.1 Define and use structural variables
--9.1 Define and use structural variables.mp4
--【Source program】Structure variable
--9.1 Self test questions
-9.2 Using structure arrays
--9.2 Using structure arrays.mp4
--【Source program】Structure array
--9.2 Self test questions
-9.3 Structure pointer
--【Source program】Structure pointer
--9.3 Self test questions
-Chapter 9 Self test questions
-CodeBlocks Baidu online disk download address
-Final Exam
--final exam