当前课程知识点:C Programming >  Chapter 3 Programming in C >  3.2 Constants, Variables and Assignments >  3.2 Constants, Variables and Assignments.mp4

返回《C Programming》慕课在线视频课程列表

3.2 Constants, Variables and Assignments.mp4在线视频

下一节:Discussion unit

返回《C Programming》慕课在线视频列表

3.2 Constants, Variables and Assignments.mp4课程教案、知识点、字幕

大家好

我是云南大学信息学院的丁海燕老师

欢迎走进C语言程序设计课堂

今天我们讲解的是C语言常量

变量与赋值

C语言中基本类型数据可以分为常量与变量两种

什么是常量

常量:在程序运行过程中

其值不能被改变的量

C语言有5种常量

整型常量

实型常量

字符常量

字符串常量和符号常量

1. 整型常量

在C语言中可以用十进制

八进制或十六进制3种形式来表示整型常量

十进制由数符0~9组成

例如:320、25、0、-4

八进制由数符0~7组成

在C语言中要表示一个八进制整数必须以数字0开头

例如015、023、057

十六进制由数符0~9、A~F或a~f组成

在C语言中要表示一个十六进制整数必须以0x或0X开头

例如0x2A、0xff、0x18

注意

在C语言中要表示八进制数必须以数字0开头

要表示十六进制数必须以0x或0X开头

反之,在程序中见到以0开头的数必为八进制数

且只能出现数符0~7

见到以0x或0X开头的数必定为十六进制数

若无0开头的数则为十进制

2. 实型常量

实型也称浮点型

在C语言中可以用小数形式

或指数形式来表示一个实型常量

小数形式:由数字和小数点组成

小数点的某一侧可以没有数字

如:123.456、0.123、123.0、0.0、123.等

指数形式

用字母e或E后紧跟一个整数来表示以10为底的幂

即10的n次方

如:123e3

表示123×103

注意

用小数形式表示时必须有小数点

小数点的某一侧可以没有数字

用指数形式表示时

字母e(或E)的前后必须有数字

且e后的指数必须为整型

3. 字符常量

C语言规定以单引号‘ ’括起来的单个字符为字符常量

如:‘A’、‘a’、‘2’ 、‘! ’等

在内存中占1个字节的存储空间

存放其ASCII值

在C语言中还有一种特殊的字符常量称为“转义字符”

转义字符是以一个反斜杠“\”开头

后紧跟一个特定字符或一个八进制的ASCII码

或先跟字母x再跟一个十六进制的ASCII码值

常用转义字符见表3.1所示

例如: ‘ ’ 代表回车换行, ‘\\’ 代表反斜线字符

‘\101’ 代表字符‘A’, ‘\141’ 代表字符‘a’

‘\x41’ 代表字符‘A’, ‘\x61’代表字符‘a’

注意

字符常量只能是用一对单引号括起来的一个字

可以是一个转义字符

如: ‘ab’、‘45’均是非法的

表示一个反斜线、单引号

双引号必须用转义字符表示

不能出现单独的一个反斜杠

若要表示一个反斜杠必须用两个连续的反斜杠

‘\\’,‘\’是非法的

例如:‘\128’、‘\0x4a’

‘\’、‘\xh5’、‘ a’均是非法的

每一个字符数据在内存中存储的是其对应的ASCII 码

每个字符都有一个对应的ASCII 码值

ASCII码值本质上是一个整型常量

例:‘a’的ASCII 码值为97

‘A’的ASCII 码值为65

‘0’的ASCII 码值为48

ASCII值的规律是

同一字母的小写与大写ASCII 码值之差为32

例如:‘a’-‘A’=32, ‘E’+32=‘e’

4. 字符串常量

C语言中用双引号将一串字符括起来称为字符串常量

如"asee"、"011"、"We are studying C"等

字符串"A"与字符常量‘A’在内存中存放的长度不同

字符串在最后要多存放一个字符串结束标记符‘\0’

故"A"占2个字节

‘A’占1个字节

注意

1.要注意区分字符常量与字符串常量

字符常量是用‘ ’括起来的一个字符

而字符串常量则是用“ ”括起来的字符序列

2.C语言中没有字符串变量

不能将一个字符串赋给一个字符变量

字符串要用字符数组来存放

3.每一个字符串在内存存放时

字符串末尾都有一个字符串结束符‘\0’

书写时‘\0’省略,只有遇见‘\0’字符串才结束

5.符号常量

例如,#define PI 3.1416

以标识符来代表的常量

事先用编译预处理命令define定义

必须以“#”开头

编译时先由系统替换为它所代表的常量

再进行编译

例如:#define LEN 2.5

用LEN代替2.5

编译时

系统先将源程序中的所有LEN替换为2.5

再编译

注意define是编译预处理命令

必须以“#”开头

什么是变量呢

变量的有关概念

在程序运行过程中

其值改变的数据称为变量

变量用标识符表示称为变量名

变量必须“先定义后使用”

系统为变量分配存储单元

存储变量的值

编写程序时通过变量名来存、取变量值

变量定义的一般格式

数据类型说明符空格变量列表

例如:int i, j

long k, m

float x,y

char ch1,ch2

必须使用合法的标识符作变量名

不能使用关键字为变量命名

变量的初始化

是指允许在说明变量的时候对变量赋初值

例如:int a=5,b=10+2

表示定义变量并对变量存储单元赋值

错误的初始化

int a=3+b,b=5

float m=n=23.16

1.整型变量

整型变量可以分为基本型、短整型、长整型3种

并用类型修饰标识符signed

和unsigned区分无符号和有符号的整型变量

无符号型又分无符号整型

无符号短整型

无符号长整型3种

有符号型又分有符号整型

有符号短整型和有符号长整型3种

整型的类型说明符及相关信息如表3.2所示

其中中括号[]表示该项为可选项

可有可无

表中给出的存储单元字节数和取值范围

是以32位的VC++编译系统为依据

有符号/无符号int占4个字节

有符号/无符号short int占2个字节

有符号/无符号long int占4个字节

2. 实型变量

C 语言中实型变量分为单精度型和双精度型两种

实型常量均作为双精度处理

单精度实型数据占4 个字节

有效数字为6~7 位

双精度实型数据占8 个字节

有效数字为15~16 位

float 变量名表

double 变量名表

在编程中数据类型用错

会导致程序结果的错误

3.字符变量

用于存放字符常量

每个字符变量在内存中占1个字节

因此一个字符变量只能存放一个字符

当把一个字符赋给字符变量时

字符变量的值就是该字符的ASCII码值

能够用于整型数据的所有操作都可以用于字符型数据

字符变量用关键字char定义

定义方式:char 字符变量名

例如:char c1=‘d’,c2=‘s’

char c=‘\101’;

下面我们来看例3.3

输出字符变量的值

字符数据在内存中存放的是它的ASCII编码

当以字符格式输出时

输出为字符

当以整型输出时输出为整型数

程序如下

int main(void)

char c1,c2

c1=97

c2=‘A’

printf("c1=%c,c2=%c ",c1,c2);

printf("c1=%d,c2=%d ",c1,c2);

return 0;

运行结果如下

按字符格式输出c1=a,c2=A

按整型格式输出c1=97,c2=65

程序结果如图所示

在C 程序中变量则必须先说明后使用

如果一个变量没有为其赋值

它的值是不确定的

为变量赋值的方法有如下几种

(1)定义变量的同时给变量赋值(称为变量的初始化)

(2)定义变量之后用赋值语句给变量赋值

(3)定义变量之后用输入语句从键盘上给变量输入值

好了,同学们

C语言常量、变量与赋值我们就学习到这儿

下节课再见

C Programming课程列表:

Chapter 1 Introduction

-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

--Discussion unit

--【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

--Discussion unit

--Running steps of C source program in CodeBlocks

--1.3 Self test questions

-Course references

-chapter 1 Self-Test

Chapter 2 Algorithm

-2.1 The Concept and Description of Algorithms

--2.1 The Concept and Description of Algorithms.mp4

--Discussion unit

--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

--Discussion unit

--2.2 Self test questions

-Chapter 2 Self test questions

Chapter 3 Programming in C

-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

--Discussion unit

--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

--Discussion unit

--【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

Chapter 4 Selection Structure

-4.1 Relational operators, logical operators, and if statements

--4.1 Relational operators, logical operators, and if statements.mp4

--Discussion unit

--【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

--4.2 Switch Statement .mp4

--Discussion unit

--【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

Chapter 5 Loop Structure

-5.1 While and Do-While Statement

--5.1 While and Do…while statement.mp4

--Discussion unit

--【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

--5.2 for statement.mp4

--【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

--【Source program】example 1

--【Source program】example 2

--【Source program】example 3

--【Source program】 Example 4

--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

Chapter 6 Batch Data Processing with Array

-6.1 Definition, Reference and Initialization of One-Dimensional Arrays

--6.1 Definition, Reference of One-Dimensional Arrays.mp4

--Discussion unit

--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

--Discussion unit

--【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

Chapter 7 Modular Programming with Functions

-7.1 Function Concept and How to Define and Call Functions

--7.1 Function Concept and How to Define and Call Functions.mp4

--【Source program】Example 7.1

--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

--Discussion unit

--【Source program】Example 7.2

--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

--Discussion unit

--【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

Chapter 8 Pointer

-8.1 Pointer Concept, Definition and Reference of Pointer Variables

--8.1 Pointer Concept, Definition and Reference of Pointer Variables.mp4

--【Source program】Example 8.1

--8.1 Self test questions

--Discussion unit

-8.2 Pointer variables as function parameters

--8.2 Pointer variables as function parameters.mp4

--Discussion unit

--【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

--Discussion unit

--【Source program】Dynamic memory allocation.

--8.11 Self test questions

-Chapter 8 Self test questions

Chapter 9 Structure

-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

--Discussion unit

--【Source program】Structure array

--9.2 Self test questions

-9.3 Structure pointer

--9.3 Structure pointer.mp4

--【Source program】Structure pointer

--9.3 Self test questions

-Chapter 9 Self test questions

CodeBlocks Baidu online disk download

-CodeBlocks Baidu online disk download address

Final Exam

-Final Exam

--final exam

3.2 Constants, Variables and Assignments.mp4笔记与讨论

也许你还感兴趣的课程:

© 柠檬大学-慕课导航 课程版权归原始院校所有,
本网站仅通过互联网进行慕课课程索引,不提供在线课程学习和视频,请同学们点击报名到课程提供网站进行学习。