c语言分数运算程序(c语言程序设计基本算术运算)(1)

今天我想要分享给大家的是C语言程序设计第二章——基本算术运算的内容。今天的主要内容有三部分,分别是常量与变量,简单的屏幕输出,数据类型和如何计算变量或数据类型所占空间的大小。

What I want to share with you today is the second chapter of C language programming-basic arithmetic operations. Today's main content has three parts, namely constants and variables, simple screen output, data types and how to calculate the size of the space occupied by variables or data types.

No.1 C运算符和表达式

c语言分数运算程序(c语言程序设计基本算术运算)(2)

c语言分数运算程序(c语言程序设计基本算术运算)(3)

C语言中的算术运算符如表所示。由算术运算符及其操作数组成的表达式称为算术表达式。其中,操作数也称为运算对象,它既可以是常量,变量,也可以是函数。不同于数学中的算术运算,C语言中的算术运算的结果与参与运算的操作数类型相关。以除法运算为例,两个整数相除之后的商仍为整数。例如,1/2与1.0/2运算的结果值是不同的,前者是整数除法,后者则是浮点数除法。前者的结果值是整数2,后者的结果值才是浮点数2.4。

赋值方法有两种:简单的赋值和多重赋值。涉及算术运算的复合赋值运算符有5个,分别为 =,-=,*=,/=,%=。

The arithmetic operators in C language are shown in the table. An expression composed of arithmetic operators and their operands is called an arithmetic expression. Among them, the operand is also called the operand, which can be a constant, a variable, or a function. Different from arithmetic operations in mathematics, the results of arithmetic operations in C language are related to the types of operands involved in the operation. Taking division as an example, the quotient after dividing two integers is still an integer. For example, the result values of 1/2 and 1.0/2 operations are different. The former is integer division, and the latter is floating-point division. The result value of the former is the integer 2, and the result value of the latter is the floating-point number 2.4.

There are two assignment methods: simple assignment and multiple assignment. There are 5 compound assignment operators involving arithmetic operations, namely =, -=, *=, /=, %=.

No.2宏常量与宏替换

c语言分数运算程序(c语言程序设计基本算术运算)(4)

宏常量也称为符号常量,是指用一个标识符号来表示的常量,这时该标识符号与此常量是等价的宏常量是由宏定义编译预处理命令来定义的。宏定义中的标识符被称为宏名。为了与源程序中的变量名有所区别,习惯上用字母全部大写的单词来命名宏常量。将程序中出现的宏名替换成字符串的过程称为宏替换。宏替换就是一种“傻瓜式”的字符串替换。

Macro constants are also called symbolic constants, which refer to constants represented by an identification symbol. At this time, the identification symbol and this constant are equivalent to the macro constant defined by the macro definition compilation preprocessing command. The identifier in the macro definition is called the macro name. In order to be distinguished from the variable names in the source program, it is customary to name macro constants with words with all capital letters. The process of replacing the macro name that appears in the program with a character string is called macro replacement. Macro substitution is a kind of "dumb" string substitution.

No.3const常量

c语言分数运算程序(c语言程序设计基本算术运算)(5)

宏常量没有数据类型,此时,我们就需用到const常量来声明具有某种数据类型的常量。在声明语句中,只要将const类型修饰符放在类型名前面,即可将类型名后的标识符声明为该类型的const常量。const常量只能在定义时赋初值。

Macro constants have no data types. At this time, we need to use const constants to declare constants with a certain data type. In the declaration statement, as long as the const type modifier is placed in front of the type name, the identifier after the type name can be declared as a const constant of that type. A const constant can only be assigned an initial value when it is defined.

No.4 自动类型转换与强制类型转换运算符

c语言分数运算程序(c语言程序设计基本算术运算)(6)

1.表达式中的自动类型转换:

在对操作数进行运算之前将所有操作数都转换成取值范围较大的操作数类型是类型提升,将所有的char和short都提升为int的过程是整数提升

2.赋值中的自动类型转换:

在一个赋值语句中,若赋值运算符左侧(目标侧)变量的类型和右侧表达式的类型不一致,则赋值时将发生自动类型转换。

3.强制类型转换运算符

强制类型转换运算符简称强转运算符或转型运算符,它的主要作用是将一个表达式值的类型强制转换为用户指定的类型。

1. Automatic type conversion in expressions:

Before performing operations on the operands, converting all operands into operand types with a larger value range is type promotion, and the process of promoting all char and short to int is integer promotion

2. Automatic type conversion in assignment:

In an assignment statement, if the type of the variable on the left (target side) of the assignment operator is inconsistent with the type of the expression on the right, automatic type conversion will occur during assignment.

3. Cast operator

The forced type conversion operator is abbreviated as forced conversion operator or cast operator. Its main function is to force the type of an expression value to a user-specified type.

参考资料:文字:百度;图片:微博;翻译:百度翻译

本文由LearningYard新学苑原创,部分图片文字来自网络,如有侵权请联系。

,