C语言学习笔记 第二章

2012年7月20日——22日
P15-31
函数:
main() 相当于子程序头,声明接受的参数与返回值数据类型
{ 函数体开始,相当于子程序开始
} 函数体结束,相当于子程序结束
int num 声明变量num短整数型
num = 1 赋值语句
* 乘号

概念:
1、#include不是C语句
2、分号是语句的一部分,不是两句中的分隔符
3、最好在开始位置声明所有变量
4、变量名称区分大小写
5、变量声明 int a,b; = int a; int b;

方法:
1、注释:/*注释1*/
//注释2

例子:

2.1
#include<stdio.h>
int main(void)
{
int num;
num = 1;
printf("I am a simple ");
printf("computer.\n");
printf("My favorite number is %d because it is first.\n",num);
return 0;
}

2.2
#include<stdio.h>
int main(void)
{
int feet, fathoms;
fathoms = 2;
feet = 6 * fathoms;
printf("There are %d feet in %d fathoms!\n", feet, fathoms);
printf("Yes, I said %d feet!\n",6 * fathoms);
return 0;
}

2.6
#include<stdio.h>
void butler(void);
int main(void)
{
printf("I will summon the butler function.\n");
butler();
printf("Yes. Bring me some tea and writeable CD-ROMS.\n");
return 0;
}
void butler (void)
{
printf("You rang, sir?\n");
}

2.7
#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)
return0;

正确应为
#include<stdio.h>
int main(void)
{
int n, n2, n3;
/*ok*/
n=5;
n2=n*n;
n3=n2*n2;
printf("n=%d,n squared = %d ,n cubed = %d\n",n,n2,n3);
return 0;
}

课后练习

2.12.1

#include<stdio.h>

int main(void)
{
printf(“Anton Bruckner\n”);
printf(“Anton\nBruckner\n”);
printf(“Anton “);
printf(“Bruckner”);
return 0;
}

2.12.2

#include<stdio.h>
int main(void)
{
printf("My name is yxzyxz001.\nAnd my Address is Dongguan, China!");
return 0;
}

2.12.3

#include<stdio.h>
int main(void)
{
printf("Now, I am 14 years old.\nI am also %d days old!",14*365);
return 0;
}

2.12.4

#include<stdio.h>
void good1(void);
void good2(void);
int main(void)
{
good1();
good1();
good1();
good2();
return 0;
}
void good1(void)
{
printf("For he's a jolly good fellow!\n");
}
void good2(void)
{
printf("Which nobody can deny!\n");
}

2.12.5

#include<stdio.h>
int main(void)
{
int toes;
toes=10;
printf("toes = %d, toes + toes = %d, toes * toes = %d.\n",toes,toes+toes,toes*toes);
return 0;
}

2.12.6

#include<stdio.h>
void smile(void);
int main(void)
{
smile();
smile();
smile();
printf("\n");
smile();
smile();
printf("\n");
smile();
printf("\n");
return 0;
}
void smile(void)
{
printf("Smile!");
}

2.12.7

#include <stdio.h>
void one_three(void);
void two(void);
int main(void)
{
printf("starting now:\n");
one_three();
printf("done!\n");
return 0;
}
void one_three(void)
{
printf("one\n");
two();
printf("three\n");
}
void two(void)
{
printf("two\n");
}

原创文章,转载请注明: 转载自YXZ’s Blog

本文链接地址: C语言学习笔记 第二章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>