【鹏哥C语言】第二讲:数据类型和变量

Artificial-Fool Lv3

操作符

操作符即运算符。(应该是operator);

有两个操作数的操作符叫双目操作符。(如33 + 42中的加号)。

int值进行相除时,结果为整数。;

1
2
3
4
5
6
7
int main()
{
int score = 5;
printf("%d\n", score / 2)//2
score = (score / 20) / 100;
printf("%d\n", score);//0
}

只有⼀个操作数的操作符叫单目操作符。

**a++**:先运算,再返回。

1
2
3
4
5
6
7
int main()
{
int a = 0;
int b = a++;//先返回 a 的当前值,再自增。
printf("a = %d\n",a);//a = 1
printf("b = %d\n",b);//b = 0
}

++a:先返回,再运算。(新学)

1
2
3
4
5
6
7
int main()
{
int a = 0;
int b = ++a;//先自增,再返回 a 的新值。
printf("a = %d\n",a);//a = 1
printf("b = %d\n",b);//b = 1
}

(type**)**value 强制类型转换。

1
2
3
4
5
6
7
int main()
{
float f = 3.1415f;
int a = (int)f;
printf("%d\n", a); //3
}

printf()

1.printf()允许限定占位符的最小宽度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
int main()
{
printf("%d\n",12345); // 输出为 "12345"

//将最小宽度改为5位:
printf("%5d\n", 123); // 输出为 " 123"
printf("%-5d\n", 123); // 输出为 "123 "

//显示超过最小宽度的字符串:
printf("%5d\n",123456);// 输出为 "123456"


//显示小数:
printf("%f\n", 123.45);// "123.450000"(默认6位)
printf("%12f\n", 123.45);// " 123.450000"

return 0;
}

2.%.2f限定显示小数位数为2(会四舍五入):

1
2
3
4
5
6
7
#include <stdio.h>
int main()
{
printf("%.2f\n", 0.5);//"0.50"
printf("%6.2f\n", 0.5);//" 0.50"
return 0;
}

3.%.2s限定显示字符串的前2个字符:

1
2
3
4
5
6
7
// 只输出 hello
#include <stdio.h>
int main()
{
printf("%.5s\n", "hello world");
return 0;
}

4.最⼩宽度和位数这两个限定值,都可以⽤ * 代替,通过 printf() 的参数传⼊。

1
2
3
4
5
6
7
#include <stdio.h>
int main()
{
printf("%*.*f\n", 6, 2, 0.5);
return 0;
}
// 等同于printf("%6.2f\n", 0.5);

上⾯⽰例中, %*.*f 的两个星号通过 printf() 的两个参数 62 传⼊。

5.使%d总是显示正负号:%+d.

1
2
3
4
5
6
7
8
#include <stdio.h>
int main()
{
printf("%+d\n", 12); // 输出 +12
printf("%+d\n", -12); // 输出 -12
return 0;
}

scanf()

scanf()相当于c语言console中的input(),可以获取用户输入的值。

当我们有了变量,我们需要给变量输⼊值就可以使⽤ scanf() 函数,如果需要将变量的值输出在屏幕上 的时候可以使⽤ prinf() 函数,下⾯看⼀个例⼦:

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
int main()
{
int score = 0;
printf("请输⼊成绩:");
scanf("%d", &score);//标准输入(stdin)
printf("成绩是:%d\n", score);//标准输出(stdout)
return 0;
}

scanf的返回值:

scanf() 的返回值是⼀个整数,表⽰成功读取的变量个数。 如果没有读取任何项,或者匹配失败,则返回 0 。 如果在成功读取任何数据之前,发⽣了读取错误或者遇到读取到⽂件结尾,则返回常量 EOF (-1)。

输入时按 ctrl+z ,可提前结束输⼊。

scanf中的各种读取规则:

scanf()通常会直接跳过起首的空白字符。

%d会跳过 100前的空格而读取100

%c不会跳过任何字符,总是返回当前第一个字符,输入 a会读取,而不是读取a。(如果需要跳过,需在其前面加上空格 %c

%s会跳过起首的空白字符再开始,并在遇到下一个空白字符时结束读取,最后会在字符串变量末尾存储⼀个空字符 \0 。如输入 hello world 会返回 hello,没有空格也没有world。也就是说,%s 只会读“单词”,不会读“句子”,也不会读多个单词。

scanf() 的风险性:

scanf() 将字符串读⼊字符数组时,不会检测字符串是否超过了数组⻓度。所以,储存字符串时, 很可能会超过数组的边界,导致预想不到的结果。为了防⽌这种情况,使⽤ %s 占位符时,应该指定 读⼊字符串的最⻓⻓度,即写成 %[m]s ,其中的 [m] 是⼀个整数,表⽰读取字符串的最⼤⻓度,后 ⾯的字符将被丢弃。

下面是一个scanf(“%[m]s”)的安全用法。

1
2
3
4
5
6
7
8
#include <stdio.h>
int main()
{
char name[11];//如果将name[11]改成name[10],那么字符串中的\0就会因为溢出而导致报错.
scanf("%10s", name);

return 0;
}
scanf中的赋值忽略符:

有时,用户的输入可能不符合预定的格式,这种情况下, scanf() 解析数据就会失败。

为了避免这种情况, scanf() 提供了⼀个赋值忽略符(assignment suppression character) * 。 只要把 * 加在任何占位符的百分号后⾯,该占位符就不会返回值,解析后将被丢弃。

1
2
3
4
5
6
7
8
9
#include <stdio.h>
int main()
{
int year = 0;
int month = 0;
int day = 0;
scanf("%d%*c%d%*c%d", &year, &month, &day);
return 0;
}

上⾯示例中, %*c 就是在占位符的百分号后面,加⼊了赋值忽略符 * ,表示这个占位符没有对应的变量,解读后不必返回。

  • Title: 【鹏哥C语言】第二讲:数据类型和变量
  • Author: Artificial-Fool
  • Created at : 2024-12-22 20:25:22
  • Updated at : 2024-12-24 01:59:14
  • Link: https://space.bilibili.com/636023121/2024/12/22/PenggeC-note2/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
【鹏哥C语言】第二讲:数据类型和变量