C language - study notes 3

Table of contents

3.1 Introduction to basic data types of C language

 3.2 Identifiers, constants and variables

1. Identifier

1), the concept of identifier

2) Types of identifiers

2. Constant

1), numerical constants

2), character constants 

3. Variables

1), the definition of variables

2) Description of variables

3.3 Basic data types

1. Integer

1), the definition of integer variables

2) The storage form of integer numbers

3) Integer variable assignment and its description

2. Solid type

1), the definition of real variables

2) The storage form of real data

 3) Assignment of real variable and its description

3. Character type

1), the definition of character variables

2) Assignment of character variables and representation of character constants

3.4 Data input and output

1. Character output function

2. Character input function

3. Format output function

1), integer format specifier

2), real format specifier

3), character format specifier

4), string format specifier

4. Format input function

1), address list

2), format control instructions


3.1 Introduction to basic data types of C language

The data types of C language can be divided into four categories: basic types, constructed types, pointer types and empty types.

 3.2 Identifiers, constants and variables

1. Identifier

1), the concept of identifier

The identifier is used to identify the name of an object in the source program, and these objects can be statements, data types, functions, variables, constants, arrays, etc. The identifier is composed of letters, numbers, and underscores, and the first position can only take letters or underscores.

Pay attention to the following rules when using identifiers:

(1) The identifier must start with a letter (a~z, A~Z) or an underscore.

(2) Other parts of the identifier can be composed of letters, underscores or numbers (0~9).

(3) Uppercase and lowercase letters represent different meanings, that is, represent different identifiers.

(4) The effective digits of identifier characters are different in different systems.

(5) The keyword of Turbo C2.0 cannot be used for the identifier.

2) Types of identifiers

(1) Keywords: The so-called keywords are words that have been used by Turbo C 2.0 itself and cannot be used for other purposes. The keywords reserved by the system have specific meanings and cannot be used as variable names, function names, etc. The C language has the following 32 keywords.

auto double int struct break else
long switch case enum register typedef
char extern return union const float
short unsigned continue for signed void
default goto sizeof to the cast do if
while static

(2) Compile preprocessed command words: such as #include, #define, etc.

(3) User identifier: the variable name, constant name and function name defined by the user.

2. Constant

Constants refer to quantities that cannot be changed during the running of a program, and are divided into two types: numeric constants and character constants.

1), numerical constants

(1) Integer constant (integer)

Such as 207, 30 and 181 are integer constants. Integer constants have the following three forms: decimal integers, octal integers, and hexadecimal integers.

(2) Real constant (real number)

Real constants represent the real numbers usually represented in mathematics, and can represent numbers with a decimal point. There are two forms, decimal form and exponential form.

Two forms of decimals: ① Decimal form such as 2.14 ② Exponential form such as 2.31e10 means 2.31✕e¹⁰

Two forms of decimals: ①Single precision ②Double precision Double precision has more effective digits than single precision.

The definition form of the constant is:

#define 常量名 常数值

For example:

#define PI 3.1415926

2), character constants 

Character constants are composed of characters, such as 'A', 'X' and 'a' and other common character constants. In addition to the familiar character constants, Turbo C also defines some special control characters.

\n new line
\t horizontal tab
\v vertical tab
\b backspace
\r carriage return
\f paper feed
\\ backslash character
\' single quote character
\? question mark character
\" double quote character
\0 null
\ddd Characters represented by 1~3 octal numbers
\xhh Characters represented by 1~2 hexadecimal numbers

3. Variables

Variables are quantities that can change while a program is running. Each variable has a name and occupies a certain storage space in memory to store data.

1), the definition of variables

Turbo C 2.0 stipulates that all variables must be declared before they can be used. A variable declaration statement consists of a data type followed by one or more variable names. The format of variable definition is as follows:

类型<变量名>;

For example: 

int a,b,c;           //说明三个变量a,b,c都是整型变量
unsigned long d;     //说明一个变量d是无符号长整型变量

2) Description of variables

(1) Like other languages, Turbo C variables must define their data types before using them, and undefined variables cannot be used.

(2) The name of the variable can only be composed of letters, numbers and underscores, and the number cannot start with a number.

(3) The writing format of the Turbo C program is very flexible without strict restrictions.

3.3 Basic data types

Turbo C provides the following basic data types: integer (int), real (float or double) and character (char).

1. Integer

Integer numbers consist of integer constants and integer variables.

1), the definition of integer variables

Integer variable definition format:

<整数类型><变量名>;

Integer types can be classified by unsigned and data size:

According to whether there is a sign or not: ① signed ② unsigned

According to the length of the data: ① long integer ② short integer

type abbreviation word length illustrate range of numbers
signed short int short or int 2 bytes signed short integer -32768~32767
signed long int long 4 bytes signed long integer -2147483648~2147483647
unsigned short int unsigned int 2 bytes unsigned short integer 0~65535
unsigned long int unsigned long 4 bytes unsigned long integer 0~4294967295

2) The storage form of integer numbers

When integers are stored in storage units, the difference between signed (signed) and unsigned (unsigned) integers lies in the definition of their highest bits. If the definition is a signed integer (signed int), the code generated by the C compiler will set the highest bit of the integer as the sign bit, and the remaining bits represent the value. If the highest bit is 0, the integer is positive; if the highest bit is 1, the integer is negative. For example, when expressed in 16-bit binary:

[+1] source code=0000000000000001

[-1] source code=1000000000000001

[+127] Source code=0000000001111111

[-127] Source code=1000000001111111

In the above numbers, the first bit of the left number is the sign bit.

Most computers - use two's complement when representing signed numbers. The complement code of a positive number, the inverse code and the source code are all equal to the source code. The complement of a negative number is the inverse code plus 1, and the inverse code is to invert each bit of the source code of the absolute value of the corresponding number, and then add 1 to the inverse code to obtain the complement code. When the above 4 numbers are expressed in 8-bit binary as signed numbers, the computer's inverse code, the complement value is:

[+1] Source code=0000000000000001=inverse code=complement code

[-1] source code=1000000000000001

[-1] Inverse code=111111111111110

[-1] Complement code = inverse code + 1 = 1111111111111111

Signed integers are important for many operations. But the absolute value of the largest number it can express is only half of the unsigned number. For example, the signed integer representation of 32767 is 011111111111111. Because it is signed, the highest bit represents the sign, so 32767 is the maximum number of int signed integers. If the highest bit is 1, the number will be treated as a negative number - 1 (because 1111111111111111 is the complement of -1). However, if the number is defined as an unsigned int, then when the highest bit is set to 1, it becomes 65 535.

3) Integer variable assignment and its description

(1) You can assign an initial value to a variable while defining an integer variable, which is called variable initialization.

For example

int a=3,b=100;    //a,b被定义为有符号短整型变量,并且a的初值为3,b的初值为100
unsigned long c=65535;    //c被定义为无符号长整型变量,并且c的初值为65535

(2) When assigning initial values, octal numbers and hexadecimal number integer constants need to be represented by specific symbols.

For example

int f=022;    //f的值是八进制数22,按十进制数输出时值为18

(3) Add a letter L or l after the integer constant to indicate that the number is a long integer, such as 22L, 773L, etc.

For example

#define G 22L      //G的值定义为长整型数22
#define H 0773L    //H的值定义为八进制的长整型数。

2. Solid type

1), the definition of real variables

The definition form of real variable is as follows:

<实型><变量列表>

Real data has different precisions due to the number of digits after the decimal point. In the C language, there are three types of real types: single-precision real numbers, double-precision real numbers, and long double-precision real numbers. These three data types are stored in the computer memory, and the number of units occupied is different, and the precision of the number expressed is also different. The specific content is shown in the table.

type word length illustrate Valid word count range of numbers
float 4 bytes single precision real number 6~7 -3.4✕10⁻³⁸ ~ 3.4✕10³⁸
double 8 bytes double precision real number 15~16 -1.7✕10⁻³⁰⁸~ 1.7✕10³⁰⁸
long double 16 bytes long double precision real number 18~19 -1.2✕10⁻⁴⁹³² ~ 1.2✕10⁴⁹³²

Real variables can be defined with the following statement:

float a,b;    //a,b被定义为单精度实型变量
double c;     //c被定义为双精度实型变量

2) The storage form of real data

float is a single-precision real number type, with a word length of 4 bytes and a total of 32 binary digits. The value range of the number is -3.4✕10⁻³⁸ ~ 3.4✕10³⁸. The memory format is to use the high 24 bits to represent the fractional part, the highest bit to represent the sign bit; the low 8 bits to represent the exponent part. The storage form of the real number type number 3.141 592 6 is shown in the figure.


For the double data type, it occupies a total of 64 bits. In some systems, 56 bits are used to store the decimal part, and the exponent part is 8 bits. In this way, the stored double-precision real number type has more effective digits, which can reduce numerical operation errors. One note: all real numbers are signed real numbers, and there are no unsigned real numbers.

 3), real variable assignment and its description

Initial value assignment to real variables can be assigned at the same time of definition. For example:

float a=23.562

The description of the real number type is as follows:
(1) The constants of the real number type only have decimal.
(2) All real number type constants are defaulted as double type
(3) For real type numbers whose absolute value is less than 1, the 0 before the decimal point can be omitted. For example, 0.22 can be written as .22,
and -0.0015E-3 can be written as -.0015E-3.
(4) When outputting real numbers in the default format of Turbo C, only 6 digits after the decimal point are reserved at most.

3. Character type

1), the definition of character variables

The definition form of a character variable is as follows:

<字符类型><变量列表>;

There are two types of characters: signed and unsigned;

For example:

char a;           //a被定义为有符号字符变量
unsigned char c;  //c被定义为无符号字符变量

 character data type

type word length illustrate range of numbers
char 1 byte signed char -128 ~ 127
unsigned char 1 byte unsigned char 0 ~ 255

2) Assignment of character variables and representation of character constants

Character variables can be assigned initial values ​​directly when they are defined.

char a='A';   //a被定义为有符号字符变量,并且将a变量赋初值为'A',这时a变量中存储A的 ASCII 码值为十进制数 65。

When assigning the initial value, characters can be directly enclosed in single quotation marks, such as 'a', 9' and 'Z', or can be expressed by the ASCII code value of the character.

char a='A';
char a= 65;

The above two initial value assignments are equivalent.

        The string constants allowed by the C language are a pair of character sequences enclosed in double quotation marks, such as "Hello TurboC 2.0". Note that 'a' and "a" are different, the former is a character and can be assigned to a variable; the latter is a string and cannot be assigned to a character variable. C stipulates to add a string ending symbol \0' at the end of each string, and there is no special string variable in C language.

3.4 Data input and output

        输入与输出的概念是相对计算机而言的,当数据从外部设备(如键盘)送给计算机时,称为“输人”;当数据从计算机中送出到外部设备(如显示器或打印机)时,称为“输出”。
        在 C 语言中,没有提供专门的输人输出语句,输人或输出操作是通过调用输入输出库函数来实现的,如 printf()就是库函数。C语言函数库中有一批标准输入输出函数,包括 printf() , scanf() ,  putchar() , getchar() , puts() 和 gets() 等。

1、字符输出函数

函数的格式:

putchar(ch)

函数的功能:将变量 ch 中的内容以一个字符形式输出到屏幕上。其中 ch 可以是字
符型变量,也可以是整型变量,还可以是字符型常量或整型常量。

putchar()函数,不仅可以输出普通可显示的字符,还可以输出转义控制字符。如 putchar(\n); 表示输出换行符,即控制输出位置换到下一行的开头。

【例1】putchar() 函数的使用。

#include<stdio.h>
int main()
{
	char x='A',y='B';
	int z=97;
	putchar(x);
	putchar(y);
	putchar('\n');
	putchar(z);
}
//运行结果为:
AB
a

2、字符输入函数

函数的调用格式:

c=getchar()

函数的功能:接收从标准输入设备(键盘)输人的一个字符,并将该字符作为函数的值赋给变量 c。
该函数的执行是等待从键盘输入一个字符,当用户输入一个字符后,函数的值就是所输入的字符的 ASCII 码值。所以常用赋值语句的形式,将键盘输人的字符,赋给一个变量。

使用getchar()函数应注意以下两点:

(1)getchar()函数可以作为 putchar()函数的参数,如语句 putchar(getchar()); 是正确的。其功能是显示键盘输人的字符。
(2)使用 getchar()函数时,回车键也会作为输入字符的一部分。尤其在连续使用
getchar()函数时要注意回车键将会作为换行符被下一个 getchar()所接受。

 【例2】getchar() 函数的使用。

#include<stdio.h>
int main()
{
	putchar(getchar());
}
//运行结果为:
A     //输入
A

3、格式输出函数

函数的调用格式:

printf("格式控制",输入项列表);

函数的功能:按某种格式,向输出设备输出若干指定类型的数据。

printf() 函数的参数包括格式控制和输出项列表两部分。格式控制部分又可分为普通字符和格式说明两部分。

(1)普通字符,将原样式输出。例如:

printf("China");

输出结构如下: 

China

(2)格式说明部分,以%开头,以”格式说明符“结束。例如:%d,%f 等。

在 printf("sum=%d",a); 中:

sum-->普通字符

%d-->格式说明

a-->输出项列表

输出格式说明符及其含义

格式说明符 含义
d 按十进制有符号整型数输出
o 按八进制无符号整型数输出
x 按十六进制无符号整型数输出
u 按十进制无符号整型数输出
c 以字符格式输出,只输出一个字符
s 输出字符串

f

以小数形式输出单,双精度数,输出6位小数
e 以标准指数形式输出单,双精度数
g 按 f 和 e 格式中较短的一种输出
l 用于输出 long 型数据,如 %ld , %lu等
m.n 指定输出域宽及精度, m 和 n 都是正整数
- 左对齐输出数据

1)、整型格式说明符

(1)十进制形式输出
① %md:控制输出项按十进制有符号整数形式输出。m 是一个整数,用于指定输出数据的最小占位宽度,若所输出数据的位数小于 m,左端(高位)将以空格占位;若输出数据的位数大于 m,则按数据的实际宽度输出;若省略 m 则按所要输出数据的实际长度
输出。如;

#include<stdio.h>
int main()
{
	int a=123,b=123456;
	printf("a=%5d\nb=%5d",a,b);
	
}
//运行结果为:
a=  123
b=123456

② % mld:格式中的 l 符号用于输出 long 型数据,m 指定输出数据的占位宽度,long 型数不能用%d 格式输出。如:

#include<stdio.h>
int main()
{
	long a=123456;
	printf("%ld\n",a);
	printf("%9ld",a);
}
//运行结果为:
123456
   123456

③  %一md 或%一mld:格式中的一修饰符表示输出数据左对齐。当输出数据的位
数小于 m 时,数据将左对齐,右边以空格占位,如:

#include<stdio.h>
int main()
{
	int a=123;
	printf("%5d+%5d=%6d\n",a,a,a+a);
	printf("%-5d+%-5d=%-6d\n",a,a,a+a);			
}
 //运行结果为:
  123+  123=   246
123  +123  =246

④ %mu 或 %mlu:控制输出项按十进制无符号整型数输出。其中 m 和 l 的含义如前所述。


(2)八进制形式
① %mo:控制输出项按八进制整数形式输出。
② %mlo:控制输出项按八进制整数形式输出。


(3)十六进制形式
① %mx:控制输出项按十六进制整数形式脸出。
② %mlx:控制输出项按十六进制整数形式输出。

2)、实型格式说明符

用于输出单精度或双精度数的格式说明符是相同的。实型格式说明符有以下两种。
(1)%f 或 %m.nf:以小数形式输出单、双精度数据。格式中的 m 表示输出的整个数据所占的列数(注意小数点占一列),含义如前所述;n 表示输出数据的小数部分的位数,如果不指定 n,则默认输出 6 位小数。
(2)%e 或 %m.ne:以标准指数形式输出单、双精度数。m 的含义如前所述,注意此格式中,实际输出的小数部分的位数为 n一 1 位。若不指定 n ,则默认输出 5 位小数。

3)、字符型格式说明符

该格式用于输出一个字符,字符型数据的格式说明符为%c或%mc。其中 m 表示输出的单个字符占的列数,即在输出的字符前要有m一1个空格占倍。

4)、字符串格式说明符

该格式用于输出一个字符串,字符串型数据的格式说明符为 %-m.ns。格式中的 m 指输出字符串占 m 列。若要输出的字符串长度大于 m,则不受 m 限制,全部输出;若要输出的字符串长度小于 m,则左边以空格占位补齐 m 位。n 用于指定输出字符串左端截取 n 个字符。一表示当输出的字符个数小于 m 时,输出左对齐,右边以空格占位补齐 m 位。
请看下面程序段的运行结果:

#include<stdio.h>
int main()
{
	printf("%3s\n%5.2s\n%.4s\n%-5.3s\n","china","china","china","china");	
}
//运行结果为:
china
   ch
chin
chi

4、格式输入函数

函数的调用格式:

scanf("格式控制",输出项列表);

函数的功能:接收键盘输人的信息,按指定格式转换后,存放到地址列表所对应的变量中。

1)、地址列表

地址列表由若干地址组成,它们可以是变量的地址也可以是字符串的首地址。变量的地址可以通过取地址运算符 & 得到。例如,变量 a的地址表示为&a。

2)、格式控制说明

与 printf类似,格式控制说明以%开头,以格式说明符结束,中间可加入附加说明
符。

输入格式说明符及其含义

格式说明符 含义
d 输入十进制整型数
o 输入八进制整型数
x 输入十六进制整型数
c 输入一个字符
s 输入字符串

f

输入十进制的小数形式或指数形式的实数
e 输入一个实数,与 f 相同
l 用于输入 long 型数据,如 %ld , %lu ,或输入double型数据,如%lf
h 用于输入short型数据,如 %hd
m 域宽说明,是一个正整数,指出输入数据所占列数
* 表示本输入项不赋给变量

使用格式输入函数时,需要注意以下几点:

(1)当输入一串数据时,要考虑分隔各个数据。

 scanf("%d %d %d",&a,&b,&c); 执行该输入时,要连续输入3个整数,可以用以下几种分隔方法。

① 可以用空格分隔各个数。

② 可以用 Enter 键分隔。

③  可以用Tab 键分隔

④ 根据格式控制说明中指定的域宽来分隔数据

#include<stdio.h>
int main()
{
	int a,b;
	scanf("%4d%3d",&a,&b);
	printf("a=%d b=%d",a,b);	
}
//运行结果为:
1234567    //输入
a=1234 b=567

⑤ 根据格式字符的含义来分隔数据。 

#include<stdio.h>
int main()
{
	int a;
	char b;
	float c;
	scanf("%d%c%f",&a,&b,&c);
	printf("a=%d b=%c c=%f",a,b,c);	
}
//运行结果为:
123D3.12    //输入
a=123 b=D c=3.120000

 ⑥ 使用自定义的分隔符

比如逗号分隔:

#include<stdio.h>
int main()
{
	int a,b,c;
	scanf("%d,%d,%d",&a,&b,&c);
	printf("a=%d b=%d c=%d",a,b,c);	
}
//运行结果为:
12,13,14    //输入
a=12 b=13 c=14

(2)附加说明符 * 号的作用是抑制输入,即用 * 说明的输入项不给任何变量。

#include<stdio.h>
int main()
{
	int a,b,c;
	scanf("%3d%*2d%2d",&a,&b,&c);
	printf("a=%d b=%d c=%d",a,b,c);	
}
//运行结果为:
1234567    //输入
a=123 b=67 c=0

Guess you like

Origin blog.csdn.net/m0_66411584/article/details/122782802