The basic knowledge points for getting started with C language include: 1. Naming of C language; 2. Variables and assignments; 3. Basic data types; 4. Formatted output statements; 5. Forced type conversion; 6. Constants; 7. Automatic type conversion. C language is a general-purpose, procedure-oriented computer programming language.
The basic knowledge points for getting started with C language are: 1. Naming of C language; 2. Variables and assignments; 3. Basic data types; 4. Formatted output Statement; 5. Forced type conversion; 6. Constant; 7. Automatic type conversion.
【Recommended course: C Language Tutorial】
(1) Naming in c language
The name given to a variable or function during programming is an identifier. Identifiers in C language cannot be named casually and must abide by certain rules. The C language stipulates that an identifier can be a string consisting of letters (A~Z, a~z), numbers (0~9), and underscore_, and the first character must be a letter or underscore. When using identifiers, pay attention to the following points:
(1) The length of the identifier should not exceed 8 digits, because in some versions of C, the first 8 digits of the identifier are valid. If the first 8 digits of two identifiers are the same, they are considered to be the same identifier.
(2)Identifiers are strictly case-sensitive. For example, Imooc and imooc are two different identifiers.
(3) It is best to choose meaningful English words for identifiers to achieve "knowing the meaning by seeing the name". Do not use Chinese.
(4) The identifier cannot be a keyword in C language. If you want to know more about C language keywords, please consult the WIKI.
For example:
(2) Variables and assignments
Variables are quantities that can be changed, and each Each variable will have a name (identifier). Variables occupy certain storage units in memory. Variables must be defined before using them. Variable names and variable values must be distinguished as two different concepts. Just like: the guests staying in the room and the room number are two different concepts.
The general form of variable definition is: data type variable name;
Multiple variables of the same type: data type variable name, variable name, variable name...;
The naming conventions for variable names and identifiers are exactly the same. %d will be explained in detail later
Note: Continuous assignment is not allowed in the definition, such as int a=b=c=5; is illegal.
There are two ways to assign variables:
1. Declare first and then assign value
2. Assign value while declaring
(3) Basic data types
Data in C language also has types. In C language, data types can be divided into: basic data types, constructed data types, There are four categories: pointer type and null type. As shown in the figure:
Example:
(4) Formatted output statement
Formatted output statement, which can also be said to be placeholder output, displays various types of data from the computer according to the formatted type and specified location. The advantage of this is that it helps the computer accurately give us the type of data we want.
The format is:
printf("输出格式符",输出项);
Common formatting characters in C language:
(5) Unchangeable Constant
A quantity whose value does not change during program execution is called a constant. Constants in C language can be divided into direct constants and symbolic constants.
Direct constants are also called literals, which are quantities that can be used directly without explanation, such as:
Integer constants: 13, 0, -13;
Real constants: 13.33, -24.4;
Character constants: 'a', 'M'
String constants: "I love php!"
In C In the language, an identifier can be used to represent a constant, which is called a symbolic constant. Symbolic constants must be defined before use, and their general form is:
#define 标识符 常量值
It is generally customary to use uppercase letters for identifiers of symbolic constants, and lowercase letters for variable identifiers to distinguish them. The following is a small example of using symbolic constants:
(6) Automatic type conversion
The data type has automatic conversion , automatic conversion occurs when operating on different data types and is automatically completed during compilation. The rules followed by automatic conversion are just like a small box can be put into a large box. The following figure shows the rules for automatic type conversion.
(7)强制类型转换
强制类型转换是通过定义类型转换运算来实现的。其一般形式为:
(数据类型) (表达式)
其作用是把表达式的运算结果强制转换成类型说明符所表示的类型,例如:
注意:double类型的数据的格式符 %f或%lf
输入结果:
在使用强制转换时应注意以下问题:
1、数据类型和表达式都必须加括号,如把(int)(x/2+y)写成(int)x/2+y则成了把x转换成int型之后再除2再与y相加了。
2、转换后不会改变原数据的类型及变量值,只在本次运算中临时性转换。
3、强制转换后的运算结果不遵循四舍五入原则。
(8)运算符
那么C语言中又有哪些运算符呢?
如下所示:
算术运算符
赋值运算符
关系运算符
逻辑运算符
三目运算符
算术运算法
C语言基本算术运算符如下表:
赋值运算符
C语言中赋值运算符分为简单赋值运算符和复合赋值运算符,之前我们已经接触过简单赋值运算符“=”号了,下面讲一下复合赋值运算符:
复合赋值运算符就是在简单赋值符“=”之前加上其它运算符构成,例如+=、-=、*=、/=、%=。
看一个小例子:
int a=3; a += 5;
分析:定义整型变量a并赋值为3,a += 5;这个算式就等价于a = a+5; 将变量a和5相加之后再赋值给a
注意:复合运算符中运算符和等号之间是不存在空格的。
关系运算符
下面是C语言中的关系运算符:
关系表达式的值是“真”和“假”,在C程序用整数1和0表示。
逻辑运算符
下面我们看一下C语言中的逻辑运算符:
三木运算符
C语言中的三目运算符:“?:”
,其格式为:
表达式1 ? 表达式2 : 表达式3;
运算符优先级比较
The above is the detailed content of What are the basic knowledge points for getting started with C language?. For more information, please follow other related articles on the PHP Chinese website!