接上:
二、C语言概述
6. 多个函数
/* two_fun.c -- 一个文件中包含两个函数*/
#include <stdio.h>
void butler(void); /*ANSI/ISO C函数原型*/
int main(void)
{
printf("I will summon the buttler function.\n");
butler();
printf("Yess.Bring me some tea and writeable DVDs.\n");
return 0;
}
void butler(void) /*函数定义开始*/
{
printf("You rang, sir?\n");
}
//I will summon the buttler function.
//You rang, sir?
//Yess.Bring me some tea and writeable DVDs.
burler()函数出现了3次,第一次是函数原型,告知编译器在程序中要使用该函数;
第二次以函数调用的形式出现在main中;
最后一次出现在函数定义中函数定义即为函数本身的源代码。
7. 调试程序
程序错误叫bug,找出并修正错误过程叫作调试(debug)。
/* nogood.c -- 有错误的程序*/
#include <stdio.h>
int main(void)
(
int n, int n2 ,int n3;
/* 该程序有多处错误
n = 5;
n2 = n * n;
n3 = n2 * n2;
printf("n = %d, n squared = %d, n cubed = %d\n", n, n2, n3);
return 0;
)
错误:1.main()函数体用圆括号来代替花括号。
2.变量声明应该这样写:
int n, n2, n3;
或者这样写:
int n;
int n2;
int n3;
3.注释末尾漏掉了*/(另一种方法是,用//替换/*)
4.语意错误:那原本想计算你的三次方结果算成了四次方
改正:
/* nogood.c -- 有错误的程序*/
#include <stdio.h>
int main(void)
{
int n, n2, n3;
n = 5;
n2 = n * n;
n3 = n2 * n2;
printf("n = %d, n squared = %d, n cubed = %d\n", n, n2, n3);
return 0;
}
8. 关键字和保留标识符
粗体表示的是C90标准新增的关键字,
斜体表示的C99标准新增的关键字,
粗斜体表示的是C11标准新增的关键字。
----ISO C关键字----
| auto | extern | short | while |
| break | float | signed | _Alignas |
| case | for | sizeof | _Alignof |
| char | goto | static | _Atomic |
| const | if | struct | _Bool |
| continue | inline | switch | _Complex |
| default | int | typedef | _Generic |
| do | long | union | _Imaginary |
| double | register | unsigned | _Noreturn |
| else | restrict | void | _Static_assert |
| enum | return | volatile | _Thread_local |
9.复习题
9.1 C语言的基本模块是什么?
他们都叫作函数。
9.2 什么是语法错误?写一个英文例子和C语言例子。
语法错误违反了组成语句或程序的规则。就是一个有语法错误的英文例子:Me speak English good.。这是一个有语法错误的C语言例子:printf"Where are the parentheses?";
…<后面简单的编程题省略>
三、数据和C
1. 示例程序
/* platinum.c -- your weight in platinum */
#include <stdio.h>
int main(void)
{
float weight; /* 你的重量 */
float value; /* 相等重量的白金价值 */
printf("Are you worth your weight in platinum?\n");
printf("Let't check it out.\n");
printf("Please enter your weight in pounds: ");
/* 获取用户的输入 */
scanf("%f",&weight);
/* 假设白金的价格是每盎司 */
/* 14.5833 用于把英镑常衡盎司转换为金衡盎司 */
value = 1700.0 * weight * 14.5833;
printf("Your weight in platinum is worth $%.2f.\n",value);
printf("You are easily worth that! If platinum prices drop,\n");
printf("eat more to maintain your value.\n");
return 0;
}
晚上分别做 了awcing周赛和leetcode双周赛,今天就学这么多吧,明天继续
本文介绍了C语言中的函数使用,包括函数原型、调用和定义。通过示例展示了如何调试含有错误的程序,并解释了常见的语法错误。此外,还列举了C语言的关键字和保留标识符,帮助读者理解C语言的基础概念。
282





