目录
1.2 编写一个程序,从键盘输入两个整数,输出这两个数之和。
1.3 调用库函数,求以下函数值。(1)cos3.5678(角度单位为弧度)(2) log 90(3) e2.567
一、习题
1.1 请编写一个程序,显示以下两行文字。
#include<stdio.h>
int main(){
printf("I am a student.\nI love China.");
return 0;
}
1.2 编写一个程序,从键盘输入两个整数,输出这两个数之和。
#include<stdio.h>
int main(){
int a, b;
scanf("%d %d", &a, &b);
printf("%d", a + b);
return 0;
}
1.3 调用库函数,求以下函数值。
(1)cos3.5678(角度单位为弧度)
(2) log 90
(3) e2.567
#include<stdio.h>
#include<math.h>
int main(){
printf("%lf\n", cos(3.5678));
printf("%lf\n", log10(90.0));
printf("%lf\n", exp(2.567));
return 0;
}
1.4 编写一个程序,求3个整数之和。
#include<stdio.h>
int main(){
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
printf("%d", a + b + c);
return 0;
}
1.5 编写一个求a+|b|的程序,其中a、b为两个整数。
#include<stdio.h>
#include<math.h>
int main(){
int a, b;
scanf("%d %d", &a, &b);
printf("%d", a + abs(b));
return 0;
}
1.6 编写一个求0~100之间全部偶数之和的程序。
#include<stdio.h>
int main(){
int sum = 0;
int i;
for(i = 0; i <= 100; i+=2){
sum += i;
}
printf("%d", sum);
return 0;
}
1.7 编写一个程序,从5个整数中找出最小的数。
#include<stdio.h>
int main(){
int min, a;
int i;
scanf("%d", &min);
for(i = 1; i < 5; i++){
scanf("%d", &a);
if(a < min){
min = a;
}
}
printf("%d", min);
return 0;
}
二、注意
2.1 C语言关键字
数据类型关键字(12个):
char、short、int、long、signed、unsigned、float、double、
struct、union、enum、void
控制语句关键字 (12 个):
1、循环控制(5个)
for、do、while、break、continue
2、条件语句(3个)
if、else、goto
3、开关语句(3个)
switch、case、default
4、返回语句(1个)
return
存储类型关键字( 5 个)
auto、extern、register、static、typedef
其他关键字( 3 个)
const、sizeof、volatile
2.2
#include<stdio.h>
int main(){
int i = 8, j = 10, m = 0, n = 0;
printf("%d %d %d %d\n", i, j, m = ++i, n = j++); //9 11 9 10
return 0;
}
2.3
#include<stdio.h>
int main(){
int x, y, z;
x=y=2;z=3;
y=x + +-1; printf("%d\t %d\t\n", x, y); //2 1
y=+ + x-1; printf("%d\t %d\t\n", x, y); //2 1
y=z - -+1; printf("%d\t %d\t\n", z, y); //3 4
y=- -z+1; printf("%d\t %d\t\n", z, y); //3 4
return 0;
}
2.4
(1)单目运算符、三目运算符和赋值运算符的结合性是从右到左。
(2)0111 1111 1111 1111 1111 1111 1111 1111 等于 2147483647 (最大值)
1000 0000 0000 0000 0000 0000 0000 0000 等于 -2147483648 (最小值)
1000 0000 0000 0000 0000 0000 0000 0001 等于 -2147483647 (第二小的值)
1111 1111 1111 1111 1111 1111 1111 1111 等于 -1
(3)C语言源程序的最小单位是字符,最小执行单元是函数。
2.5 &,|,^的妙用
x &(x - 1)的作用是对一个数中二进制1的个数进行统计
x |(x + 1)的作用是对一个数中二进制0的个数进行统计