C语言基础知识

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

C语言是以下哪种类型的编程语言?

  • 低级语言
  • 高级语言 (correct)
  • 汇编语言
  • 机器语言

以下哪个选项不是C语言的特点?

  • 语言简洁紧凑
  • 是面向对象的程序设计语言 (correct)
  • 可以直接对硬件进行操作
  • 数据类型丰富

以下关于C语言注释的描述,哪个是错误的?

  • 只有初学者才需要写注释,实际工作中不需要写注释 (correct)
  • 计算机不会编译运行注释的内容
  • /* */ 中间可以写多行注释
  • 单行注释 // 只能注释一行内容,不能跨行

在C语言中,一个完整的C程序有且仅有一个 ___函数,它是程序执行的入口。

<p>main</p> Signup and view all the answers

C程序的执行从哪里开始?

<p>名为main的函数 (B)</p> Signup and view all the answers

C语言程序的基本单位是 ___ 。

<p>函数</p> Signup and view all the answers

结构化程序由 ______, 选择结构、循环结构三种基本结构组成。

<p>顺序结构</p> Signup and view all the answers

C语言算法的特性包括有效性、有穷性、确定性、有零个或多个输入和______。

<p>有一个或多个输出</p> Signup and view all the answers

C语言程序执行顺序是:编译, 链接, ______。

<p>运行</p> Signup and view all the answers

以下叙述中,哪个是错误的?

<p>C语言可以不用编译就能被计算机识别执行 (B)</p> Signup and view all the answers

下列4种不同数制表示的数中,数值最小的一个是?

<p>十六进制数 A6 (A)</p> Signup and view all the answers

以下哪个可以作为C语言合法整数?

<p>0Xffa (B)</p> Signup and view all the answers

以下选项中哪个可以作为C语言合法常量?

<p>-80. (A)</p> Signup and view all the answers

在C语言程序中,标识符由什么组成?

<p>字母、数字和下划线 (D)</p> Signup and view all the answers

以下哪个是合法的C语言标识符?

<p>int_var (A)</p> Signup and view all the answers

下列用户定义标识符中,全部合法的一组是哪个?

<p>_total clu1 sum (D)</p> Signup and view all the answers

在C语言中,定义字符型变量的关键字是什么?

<p>char (A)</p> Signup and view all the answers

在C语言中,定义双精度浮点型变量的关键字是什么?

<p>double (C)</p> Signup and view all the answers

以下关于C语言数据类型的说法,哪个是正确的?

<p>char 类型只能存储单个字符,占用1个字节 (A)</p> Signup and view all the answers

英文小写字母 a 的 ASCII 码为 97,英文大写字母 A 的 ASCII 码为多少?

<p>65 (B)</p> Signup and view all the answers

字符 '0' 的 ASCII 码值为48。

<p>True (A)</p> Signup and view all the answers

若要使用 scanf 从键盘给 xyz 输入数据,正确的输入语句是(假设 x y z 已定义为 int 型变量)?

<p><code>scanf(&quot;%d %d %d&quot;,&amp;x,&amp;y,&amp;z);</code> (C)</p> Signup and view all the answers

以下程序执行时,将1、2、3分别赋给 a、b、c, 正确的输入是什么? main() { int a,b,c,d; scanf("a=%d,b=%d,c=%d",&a,&b, &c, &d); ...}

<p>a=1,b=2,c=3</p> Signup and view all the answers

给定以下代码,假定从键盘输入 23456 (回车),程序的输出结果是? void main (){ int m,n; scanf("%2d%3d",&m,&n); printf("m=%d n=%d\n",m,n);}

<p><code>m=23 n=456</code> (A)</p> Signup and view all the answers

将以下转义字符与其用途进行匹配:

<p><code>\n</code> = 回车换行 <code>\t</code> = 制表符 (Tab) <code>\\</code> = 反斜杠 <br /> <code>\&quot;</code> = 双引号</p> Signup and view all the answers

以下代码的输出结果是什么? #include <stdio.h> int main() { int a = 5, b = 3; int result1 = a + b + 2; int result2 = (a + b) * 2; printf("result1 = %d\n", result1); printf("result2 = %d\n", result2); return 0; }

<p>result1 = 10 result2 = 16</p> Signup and view all the answers

以下代码的输出结果是什么? #include <stdio.h> int main() { int a = 3, b = 5, c = 7; int result = a += b -= c * 2; printf ("Result = %d",result); return 0; }

<p>Result = -6</p> Signup and view all the answers

Int $a = 3, b = 4, c = 5$; 求表达式(a++ * 2 + --b)/(c-- -1)的值

<p>2</p> Signup and view all the answers

Int $a = 6, b = 4, c = 3$; 则表达式 $a && b + c || b - 4$ 的值是

<p>1</p> Signup and view all the answers

在C语言中,要正确表达数学关系 x>y>z,以下哪个是正确的?

<p>x &gt; y &amp;&amp; y &gt; z (B)</p> Signup and view all the answers

若运行时,给变量x输入12,则以下程序的运行结果是? #include <stdio.h> int main() { int x,y; scanf("%d",&x); y= x < 10 ? x + 10 : x - 12; printf ("%d\n",y); return 0; }

<p>0</p> Signup and view all the answers

已知int x = 5, y = 3;, 求表达式 x > 4?y<5?x+y:x-y:y>2?x*y:x/y 的值

<p>8</p> Signup and view all the answers

什么函数可以从键盘得到你输入的一个字符给变量a?

<p>getchar()</p> Signup and view all the answers

下列关于 if 语句说确的是?

<p>if (a &gt; b) {printf(&quot;a is greater&quot;);} else {printf(&quot;b is greater&quot;);} (A)</p> Signup and view all the answers

以下代码中 for 循环的执行次数是? for(i = 0; i < 10; i += 2) { //循环体内容 }

<p>5 (C)</p> Signup and view all the answers

以下代码片段中,输出结果为 5 4 3 2 1 的是?

<p>for (int i = 1; i &lt;= 5; i++) printf(&quot;%d &quot;, 6 - i); (D)</p> Signup and view all the answers

以下代码的输出结果是什么?

#include <stdio.h>
int main() {
int i;
for(i = 1; i <= 5; i++) {
if(i == 3) {
 continue;
}
printf("%d ", i);
}
return 0;
}

<p>1 2 4 5</p> Signup and view all the answers

关于以下while 代码的说法正确的是? int k = 5; while(k > 0 && k < 10) { printf("%d ", k); k += 2; }

<p>输出5 7 9 (C)</p> Signup and view all the answers

以下C语言代码的输出结果是什么?

#include <stdio.h>
int main() {
int num = 1;
while (1) {
if (num == 4) {
break;
}
printf("%d ", num);
num++;
}
return 0;
}

<p>1 2 3</p> Signup and view all the answers

以下代码的输出结果是什么?

#include <stdio.h>
int main() {
int i = 6;
do {
printf("%d ", i);
i++;
} while (i <= 5);
return 0;
}

<p>6</p> Signup and view all the answers

下列循环语句中,哪个会无限循环?

<p>以上都是 (A)</p> Signup and view all the answers

定义一个包含 5 个整数的数组,数组名为arr,正确的定义语句是什么?

<p>int arr[5]</p> Signup and view all the answers

以下关于C语言数组索引的描述,正确的是?

<p>数组索引用于访问数组中的特定元素,其值必须是整数类型 (A)</p> Signup and view all the answers

下面程序的输出结果是?

#include <stdio.h>
int main(){
int i, a[10]={10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
printf("%d%d%d\n", a[2], a[5], a[8]);
return 0;
}

<p>852 (B)</p> Signup and view all the answers

以下哪个能对二维数组 a 进行正确初始化的语句?

<p>int a[][3]={1,2,3,4,5,6} (A)</p> Signup and view all the answers

定义一个字符数组 char arr[5] = {'a', 'b'}arr 的完整内容是什么?

<p>{'a', 'b', '\0', '\0', '\0'}</p> Signup and view all the answers

在C语言中,函数定义的正确顺序是?

<p>返回值类型、函数名、参数列表、函数体 (A)</p> Signup and view all the answers

在C语言中,函数的参数可以是?

<p>以上都是 (B)</p> Signup and view all the answers

C 语言函数既可以嵌套定义又可递归调用

<p>False (B)</p> Signup and view all the answers

以下关于C语言函数的说法,正确的是?

<p>函数可以没有参数 (C)</p> Signup and view all the answers

C语言中, 函数可以没有返回值, 此时函数的返回类型应声明为?

<p>void (A)</p> Signup and view all the answers

在C语言中,函数参数传递默认是按什么方式传递的?

<p>传值 (C)</p> Signup and view all the answers

用于比较两个字符串的函数是?

<p>strcmp(str1, str2)</p> Signup and view all the answers

下面哪个是正确的结构体定义的方式?

<p>struct Person { char name; int age; }; (C)</p> Signup and view all the answers

下面哪个是正确的打开文件操作的方式?

<p>FILE *fp = fopen(&quot;data.txt&quot;, &quot;r&quot;); (A)</p> Signup and view all the answers

在C语言中,用于关闭文件的函数是?

<p>fclose (D)</p> Signup and view all the answers

Flashcards

基础知识

特点、算法、进制转换

顺序结构

标识符、关键字、数据类型、输入输出语句、运算符与表达式

选择结构

if语句、swich语句

循环结构

for语句、while语句、do while语句

Signup and view all the flashcards

数组

一维数组、二维数组、字符数组

Signup and view all the flashcards

函数

基础知识、参数传递、函数返回值

Signup and view all the flashcards

指针

指针变量、指针应用

Signup and view all the flashcards

其他

字符串、结构体、文件、宏定义、枚挙

Signup and view all the flashcards

C语言的类型

C语言是一种(C)

Signup and view all the flashcards

单行注释

// 只能注释一行内容,不能跨行

Signup and view all the flashcards

多行注释

/* */ 中间可以写多行注释

Signup and view all the flashcards

程序基本结构

顺序结构、选择结构、循环结构

Signup and view all the flashcards

C语言程序执行顺序

编译,链接,运行

Signup and view all the flashcards

八进制规定

0开头、且没有8

Signup and view all the flashcards

十六进制规定

0x开头

Signup and view all the flashcards

小数的合法写法

C语言小数点两边有一个是零

Signup and view all the flashcards

C语言程序标识符

字母、数字和下划线组成

Signup and view all the flashcards

常见数据类型

char、int、float、double

Signup and view all the flashcards

char 类型

存储单个字符,占用1个字节

Signup and view all the flashcards

a 的 ASCII 码

英文小写字母 a 的 ASCII 码为 97

Signup and view all the flashcards

A 的 ASCII 码

英文大写字母 A 的 ASCII 码为 65

Signup and view all the flashcards

scanf

1、2

Signup and view all the flashcards

\n

回车换行

Signup and view all the flashcards

if 大括号省略

判断语句大括号里只包含一条语句时,大括号可以省略

Signup and view all the flashcards

continue:

跳过本次循环的剩余部分

Signup and view all the flashcards

指针

八叉鱼

Signup and view all the flashcards

1 = 1

0

Signup and view all the flashcards

数组

1, 2, 3, 4, 5

Signup and view all the flashcards

字符串赋值

strcpy(str1, str2)

Signup and view all the flashcards

sizeof(s)

float

Signup and view all the flashcards

字符串反转函数

strrev()

Signup and view all the flashcards

scanf用法

scanf()

Signup and view all the flashcards

动态分配内存

malloc()

Signup and view all the flashcards

比较⼤⼩

strcmp()

Signup and view all the flashcards

Study Notes

  • C语言是面向对象程序设计语言
  • 认真看一遍视频,例题都会做了
  • 要去题库刷题,哪块不熟看哪里
  • 题目如果哪里不懂,可以问AI

章节知识点总结

  • 基础知识包括特点、算法和进制转换
  • 顺序结构包含标识符、关键字、数据类型、输入输出语句、运算符与表达式
  • 选择结构学习 if 和 swich 语句
  • 循环结构学习 for、while 和 do while 语句
  • 数组包括一维数组、二维数组和字符数组
  • 函数部分学习基础知识、参数传递和函数返回值
  • 指针部分学习指针变量和指针应用
  • 其他部分学习字符串、结构体、文件、宏定义和枚举

C语言的特点

  • #include 是编译预处理指令
  • int main() { ... } 定义主函数
  • printf("Hello, World!\n"); 是输出内容
  • return 0; 表示主函数执行完毕

题1 C语言的类型

  • C 语言是一种高级语言。

题2 C语言的特点

  • C 语言特点是语言简洁紧凑,可以直接对硬件进行操作,数据类型丰富
  • C语言 不是面向对象的程序设计语言

题3 C语言的注释及描述

  • 计算机不会编译运行注释内容
  • 单行注释 // 只能注释一行,不能跨行
  • /* */ 中间可以写多行注释
  • 写注释初学者和实际工作都需要

题4 程序执行的入口

  • 在 C 语言中,一个完整的 C 程序有且仅有一个 main 函数,
  • main 函数 是程序执行的入口。

题5 C程序的执行

  • C 程序的执行从名为 main 的函数开始。

题6 C语言程序的基本单位

  • C 语言程序的基本单位是函数。

题7 程序基本结构

  • 结构化程序由顺序结构、选择结构和循环结构三种基本结构组成。

题8 C语言算法的特性:

  • 有效性
  • 有穷性
  • 确定性
  • 有零个或多个输入,有一个或多个输出。

题9 C语言程序执行顺序

  • 执行顺序:编译、链接、运行。
  • 后台生成对应的三种后缀名的文件.c.obj.exe
  • 注意:.exe 文件才能运行!

题10 关于C 语言的描述

  • C 语言运算功能丰富
  • C语言 允许直接访问物理地址,直接对硬件进行操作
  • C可以属于结构化的程序设计语言
  • C不可以不用编译就能被计算机识别执行

基础知识——进制转换

  • 掌握十进制与其他进制的互相转换
  • C 语言只有八、十、十六进制,没有二进制
  • 运行时候,所有的进制都要转换成二进制来进行处理
  • 八进制规定要以 0 开头、且没有 8,逢 81,如:017是合法的,018 是非法的
  • 十六进制规定要以 0x 开头,如:0x15a是合法的
  • 小数的合法写法:C 语言小数点两边有一个是零的话,可以不用写,如:1.0 可写成 1.0.1 可写成 .1
  • 科学计数法的合法形式:2.333e-1 就是合法的,且数据是 2.333×10^-1
  • e前e后必有数,e后必为整数.

题11 四种不同数制表示的数中,数值最小

  • 在四种数制的对比中,十六进制数 A6 数值最小。

题12 作为 C 语言合法整数的是

  • 0Xffa 可以作为 C 语言合法整数

题13 作为 C 语言合法常量的是

  • -80.可以作为 C 语言合法常量

顺序结构——代码示例

#include <stdio.h>  // 编译预处理指令
int main() {    // 定义主函数
    int num1, num2;      // 定义变量,用于存储用户输入的两个整数
    int sum;             // 定义变量,用于存储两个整数的和
    printf("请输入两个整数:");   // 提示用户同时输入两个整数
    scanf("%d %d", &num1, &num2);    // 同时读取两个整数
    sum = num1 + num2;    // 计算两个整数的和
    printf("两个整数的和是:%d\n", sum);  // 输出两个整数的和
    return 0;   // 函数执行完毕,返回函数值0
}

顺序结构——标识符

  • 在 C 语言程序中,标识符由字母、数字和下划线组成。
  • 第一个字符必须是字母或下划线
  • 标识符不能用关键字。

顺序结构——关键字

类别 | 关键字

  • --|--- 数据类型 | char、int、float、double、void、short、long、signed、unsigned 存储类型 | auto、register、static、extern 控制语句 | if、else、switch、while、do、for、case、default、break、continue、goto、return 其他 | typedef、struct、union、enum、sizeof、const、volatile、inline、restrict

题2 以下哪个是合法的 C 语言标识符

  • int_var 合法的 C 语言标识符

题3 下列用户定义标识符中,全部合法的一组是

  • _total clu1 sum 是合法标识符的组合。

顺序结构——数据类型

数据类型 | 关键字| 占用字节数 | 占位符 | 用途示例

  • --|---|---|---|--- 整型 | int | 4 字节 | %d | 存储整数,是最常用的整数类型 长整型 |long int| 4 字节 | %ld | 存储较大范围的整数 单精度浮点型 | float| 4 字节 | %f | 存储小数,精度相对较低 双精度浮点型 | double| 8 字节 | %lf | 存储小数,精度相对较高 字符型 | char | 1 字节 | %c | 存储单个字符,如 'a''5' 等,也可用于存储较小的整数值

题4 在 C 语言中,定义字符型变量的关键字是

  • char 定义字符型变量的关键字

题5 在 C 语言中,定义双精度浮点型变量的关键字是

  • double 定义双精度浮点型变量的关键字

题6 关于 C 语言数据类型的说法,正确的是

  • char 类型只能存储单个字符,占用 1 个字节.

顺序结构——ASCII码

a A 0
97 65 48

题7 字母ASCII码

  • 英文小写字母 a 的 ASCII 码为 97
  • 英文大写字母 A 的 ASCII 码为 65

题8 字符的ASCII码

  • 字符 '0' 的ASCII 码值为 48。

顺序结构——输入及输出语句

int num1, num2;
scanf("%d %d",&num1,&num2); // 输入的内容要用空格分开
scanf("%d,%d",&num1,&num2); // 输入的内容需要用逗号隔开
scanf("第一个数:%d,第二个数:%d",&num1,&num2); // 必须输入: "第一个数:1,第二个数:2"

题9 正确的输入语句是

  • 如果xyz 被定义为 int 型变量,使用键盘给 xyz 输入数据的正确语句是:
scanf("%d %d %d",&x,&y,&z);
  • 要记住scanf 第二部分的 & 别忘了

题10 在 C 语言中用正确的scanf

int a,b,c,d;
scanf("a=%d,b=%d,c=%d",&a,&b,&c,&d);
  • 输入数据要像a=1,b=2,c=3
int num1 = 1,num2 = 2
printf("请输入两个整数:"); // 会直接打印在屏幕
printf("两个整数的和是:%d\n", 3); // 打印结果: 两个整数的和是:3
printf("整数1:%d,整数2:%d\n",num1,num2); // 打印结果:整数1:1,整数2:2
printf("小数:%f,字符:%c\n",0.1,a); // 打印结果: 小数:0.1,字符:a (如果定义了a='a';)
printf("两个整数的和是:%d\n",num1+num2); // 打印结果: 两个整数的和是:3

题11 假定从键盘输入的结果

  • 要细心看, scanf("%2d%3d",&m,&n);会截取数字;

顺序结构——输出语句

占位符 用途 占位符 用途
%d 输出十进制整数 %ld long类型的十进制整数
%x 十六进制整数 %#x 带0x的十六进制整数
%o 八进制整数 %#o 带前导0的八进制数
%f 单精度浮点数 %lf 双精度浮点数
%c 字符 %s 字符串
%% 百分号 %p 指针地址

顺序结构——转义

转义字符 含义 转义字符 含义
\n 换行 \f 换页
\t tab \ 反斜杠
\b 删除键 ' 单引号
\r 换行 " 双引号

顺序结构——运算符与表达式

  • 要注意运算符优先级
  • 赋值运算符的结合性是从右向左
  • A && B - 两个都为真, 结果才为真
  • A || B - 只要有一个是真, 结果就为真
int a = 5, b = 3;
int result1 = a + b * 2; //11
int result2 = (a + b) * 2; //16

题14 求结果表达式

int result = a += b -= c * 2;

+=, -= 这种赋值是从右往左结合 - 先 b -= c * 2相当于 b=b-(c*2) 所以 b = 5 - 14 = -9, a += b 相当于 a = a + b 所以a = 3 + (-9) = -6

题15 求表达式

a++ * 2 + --b)/(c-- -1)

要注意++--的位置, 可以手动算一下

题17 正确表达数学关系

  • 要表达x>y>z,需要使用:
x > y && y > z

题18 条件表达式:int x = (a > b) ? a : b;

  • 意思是如果a>b, 这个表达式的结果就是a, 否则就是b
getchar
  • 意思是 从键盘得到你输入的一个字符给变量a;
char a = getchar()
putchar
  • 意思是把字符y输出到屏幕中;
putchar('y')
C语言中
x、y数值互换
要用中间变量
int a =x;
x = y;
y = a;

选择结构相关笔记

if 语句大括号里只包含一条语句时, 大括号可以省略

题1 if语句语法

if (a > b) {
    printf("a is greater");
} else {
    printf("b is greater");
}

Swich的模板

switch (choice) {
    case 1:
        printf("你选择了数字 1。\n");
        break;
    case 2:
        printf("你选择了数字 2。\n");
        break;
    default:
        printf("你输入的数字不在范围内。\n");
        break;
}

题2 swich语句

  • 要注意每一个case 里的break

条件表达式

要记得使用==, 如果不等于!=

选择结构的经验

  • 判断连续区域, 使用if
  • 判断离散值, 使用swich

for循环模板

for (int i = 1; i <= 10; i++) {
    printf("下次一定\n");
}

题2 注意for循环括号里

  • 是分号;, 不是,

###题3 for循环

continue 跳过本次循环的, 直接进行下一次循环

###While循环的模板

int i
while (i <= 5) {
    printf("%d\n", i);
    i++;
}

###Do..while循环的模板

int i = 6;
do {
    printf("%d ", i);
    i++;
} while (i <= 5);
  • do while()语句的分号;一定不能够丢

各个模式及实现的功能

int factorial(int n) {
    int result
    for (int i = 1; i <= n; i++) {
        result *= i;
    }

    return result;
}

判断是不是质数

int isPrime(int a) {
    if (a <= 1) {
        return 0; //小于等于1的数不是质数
    }
    for (i=2;i <= a/2; i++) { //从2到a/2遍历
        if (a % i == 0) {
            return 0; //如能被整除则不是质数
        }
    }
    return 1; //是质数
}

常用函数

  • 使用时候要把对应的头文件添加到代码最前面!

数学相关

函数名 | 头文件 | 功能描述 | 返回值类型

  • --|---|---|---| abs() | <stdlib.h>| 计算整数的绝对值 | int sqrt() | <math.h> | 计算浮点数的平方根 | double fabs() | <math.h> | 计算浮点数的绝对值 | double pow() | <math.h>| 计算 x 的 y 次幂 | double sin() | <math.h> | 计算角度(弧度制)的正弦值 | double cos() | <math.h> | 计算角度(弧度制)的余弦值 | double tan() | <math.h> | 计算角度(弧度制)的正切值 | double log() | <math.h> | 计算自然对数(以e为底) | double exp() | <math.h> | 计算e的x 次幂 | double ceil() | <math.h>| 向上取整 | double floor() | <math.h> | 向下取整 | double round() | <math.h>| 四舍五入 | double

  • 指针变量用来放地址

  • 一般变量用来放数值

int a = 10;
int *p = &a;
  • p存的是a的地址
  • *p 存的是a的值
scanf` 第二部分的`&不要遗漏

字符串相关

函数名 | 实现的功能

  • --|--- strcmp(str1, str2)| 比较字符串str1和str2 strcat(str1, str2)| 连接str1和str2 strlen(str) | 求str的长度 strcpy(str1, str2) | 将str2复制到str1 strrev(str) | 将str反转, 记住, string里没有这个函数
struct stu {
    int age;
    char name[20];
    float score;
};
  • 记住 struct 大体格式及变量结构

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

Use Quizgecko on...
Browser
Browser