第二章 C语言概述

目录

2.1 零碎基础知识

2.2 课后复习题

2.3编程题


2.1 零碎基础知识

实例代码

#include <stdio.h>

int main(void)					/*一个简单的C程序*/
{
	int num;					/*定义一个名为num的整性变量*/
	num = 1;					/*为num赋一个值*/

	printf("I am a simple ");	/*使用printf()函数*/
	printf("computer.\n");
	printf("My favorite number is %d because it is first.\n", num);

	return 0;
}

运行结果:

#include 指令和头文件

#include <stdio.h>(C预处理器指令)

作用相当于把stdio.h文件中的所有内容都输入该行所在的位置。实际上,这是一种“拷贝-粘贴”的操作。

int main(void)

标准形式,防止程序从一个编译器移至到另一个编译器出现问题。

注释/*一个简单的程序*/

当注释用于把一段代码注释掉,更好的办法是使用#if指令

#if 0
       statements
#endif

检测程序状态的方法

模拟计算机逐步执行程序

在程序中的关键点插入额外的printf()语句

使用调试器

关键字和保留标识符

关键字是C语言的词汇。它们对C而言比较特殊,不能用它们作为标识

符(如,变量名)。

在表 2.2 中所列的C语言关键字中,粗体表示的是C90标准新增的关键字,斜体表示的C99标准新增的关键字,粗斜体表示的是C11标准新增的关键字。

aute  Break  case  char  defeule  double  extern  goto  ini2ne  reqlster  ISO  short  sL2ecf  static  tch  uns  uni le

保留标识符包括那些以下划线字符开头的标识符和标准库函数名,如printf()。

2.2 课后复习题

1.C语言的基本模块是什么?

函数

2.什么是语法错误?写出一个英语例子和C语言例子。

语法错误违反了组成语句或程序的规则

3.什么是语义错误?写出一个英语例子和C语言例子。

语义错误是指含义错误

4.Indiana Sloth编写了下面的程序,并征求你的意见。请帮助他评定

include studio.h

int main{void} /* 该程序打印一年有多少周 /*

( int s

s := 56;

print(There are s weeks in a year.);

return 0;

--------------------------------------------------

改正程序

#include <stdio.h>

int main(void)

{

        int s;

        s = 56;

        printf("There are %d weeks in year.", s);

        return 0;

}

运行结果:

5.假设下面的4个例子都是完整程序中的一部分,它们都输出什么结果?

    //A.

        printf("Baa Baa Black Sheep.");

        printf("Have you any wool?\n");

        //B.

        printf("Begone!\nO creature of lard!\n");

        //C.

        printf("What?\nNo/nfish?\n");

        //D.

        int num;

        num = 2;

        printf("%d + %d = %d", num, num, num + num);

运行结果:

6.mainintfunctionchar=中,哪些是C语言的关键字?

关键字:int char

main函数名; function函数; =运算符;

7.如何以下面的格式输出变量wordslines的值(这里,3020350代表两个变量的值)? There were 3020 words and 350 lines.

int words, lines;

words = 3020; lines = 350;

printf("There were %d words and %d lines.\n",words,lines);

运行结果:

8.考虑下面的程序:

#include<stdio.h>

int main(void)

{

    int a,b;

    a=5; b = 2; /* 7 */

    b = a;      /* 8 */

    a = b;      /* 9 */

    printf("%d %d\n",b,a);

    return 0;

}

9.考虑下面的程序:

#include<stdio.h>

int main(void)

{

    int x,y;

    x=10;

    y = 5;     /*7*/

    y = x + y; /*8*/

    x = x*y;   /*9*/

    printf("%d %d\n",x,y);

    return 0;

}

2.3编程题

1.编写一个程序,调用一次 printf()函数,把你的姓名打印在一行。再调用一次 printf()函数,把你的姓名分别打印在两行。然后,再调用两次printf()函数,把你的姓名打印在一行。输出应如下所示(当然要把示例的内容换成你的姓名):

#include<stdio.h> 

int main(void)
{
    printf("xiao zhou\n");
    printf("xiao\nzhou\n");
    printf("xiao ");
    printf("zhou\n");

    return 0;
}

运行结果:

2.编写一个程序,打印你的姓名和地址。

#include<stdio.h> 

int main(void)
{
    printf("xiao zhou\naddress:China\n");

    return 0;
}

运行结果:

 

3.编写一个程序把你的年龄转换成天数,并显示这两个值。这里不用考虑闰年的问题。

#include <stdio.h>

int main(void)
{
    printf("%d years is %d days", 20, 20 * 365);

    return 0;
}

运行结果:

 

4.编写一个程序,生成以下输出:

除了 main()函数以外,该程序还要调用两个自定义函数:一个名为jolly(),用于打印前 3 条消息,调用一次打印一条;另一个函数名为deny(),打印最后一条消息。

#include <stdio.h>

int jolly(void);
int deny(void);

int main(void)
{
    int i = 0;
    while (i < 3)
    {
        i++;
        jolly();
    }
    deny();

    return 0;
}

int jolly(void)
{
    printf("For he's a jolly good fellow!\n");

    return 0;
}

int deny(void)
{
    printf("Which nobody can deny!\n");

    return 0;
}

运行结果:

 

5.编写一个程序,生成以下输出:

除了main()以外,该程序还要调用两个自定义函数:一个名为br(),调用一次打印一次“Brazil, Russia”;另一个名为ic(),调用一次打印一次“India,China”。其他内容在main()函数中完成。

#include <stdio.h>

int br(void);
int ic(void);

int main(void)
{
    br();
    ic();
    ic();
    br();

    return 0;
}

int br(void)
{
    printf("Brazil, Russia.");

    return 0;
}

int ic(void)
{
    printf("India, China.\n");

    return 0;
}

运行结果:

 

6.编写一个程序,创建一个整型变量toes,并将toes设置为10。程序中还要计算toes的两倍和toes的平方。该程序应打印3个值,并分别描述以示区分

#include <stdio.h>

int main(void)
{
    int toes = 10;

    printf("toes is %d\n", toes);           /*toes的值*/
    printf("toes*2 is %d\n", toes*2);       /*toes的二倍*/
    printf("toes^2 is %d\n", toes*toes);    /*toes的平方*/

    return 0;
}

运行结果:

 

7.许多研究表明,微笑益处多多。编写一个程序,生成以下格式的输出:

该程序要定义一个函数,该函数被调用一次打印一次“Smile!”,根据程序的需要使用该函数。

#include <stdio.h>

int smile(void);

int main(void)
{
    int i = 0, j;

    for (i = 0; i < 3; i++)
    {
        for (j = i; j < 3; j++)
            smile();

        printf("\n");
    }

    return 0;
}

int smile(void)
{
    printf("Smile!");

    return 0;
}

运行结果:

8.在C语言中,函数可以调用另一个函数。编写一个程序,调用一个名为one_three()的函数。该函数在一行打印单词“one”,再调用第2个函数two(),然后在另一行打印单词“three”。two()函数在一行显示单词“two”。main()函数在调用 one_three()函数前要打印短语“starting now:”,并在调用完毕后显示短语“done!”。因此,该程序的输出应如下所示:

#include <stdio.h>

int one_three(void);
int two(void);

int main(void)
{
    printf("starting now\n");
    one_three();
    printf("done!\n");

    return 0;
}

int one_three(void)
{
    printf("one\n");
    two();
    printf("three\n");

    return 0;
}

int two(void)
{
    printf("two\n");

    return 0;
}

运行结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值