C语言学习笔记[第2天]2022.11.12

接上:

二、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关键字----

autoexternshortwhile
breakfloatsigned_Alignas
caseforsizeof_Alignof
chargotostatic_Atomic
constifstruct_Bool
continueinlineswitch_Complex
defaultinttypedef_Generic
dolongunion_Imaginary
doubleregisterunsigned_Noreturn
elserestrictvoid_Static_assert
enumreturnvolatile_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双周赛,今天就学这么多吧,明天继续

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值