变量
数据类型
I/O
Program = Input + Data + Operations + Output
Given a radius (say 10) of a circle,
to compute its circumference and area.
L=2πr S=πr2
int radius = 10;
radius.radius is int (integer).radius is initialized to 10.radius.radius refers to a location (&radius) in memory.int radius = 10;
radius is an identifier.
Warning: Do not start with _, which are reserved by C.
circumference = 2 * PI * radius
Given a radius (say 100) of a sphere,
to compute its surface area and volume.
A=4πr2V=34πr3
_______________ : surface_area_______________ : volume6 克氧气的分子数是多少?
Q=6/32×6.02×1023
两种格式输出, 结果均使用科学计数法表示
校名 (EN) / 校址
类型(综合型C/艺术类A/工科类T)
校庆日 (mm-dd-yyyy)
教学质量/研究质量/影响力
平均分/标准差/排名
\t 间隔char and <ctype.h>
A char is actually an int.
C string char first_name[] = “Tayu”;
A C string is a null-terminated (\0) sequence of characters.
String literal: ‘T’, ‘a’, ‘y’, ‘u’, ‘\0’
%[flags][width][.[precision]]specifier %d: decimal int%f: double%e %E: double (−d.ddde±dd)%c: char%s: C string%%: %
%[flags][width][.[precision]]specifier %[flags][width][.[precision]]specifier %[flags][width][.[precision]]specifier %d: minimum number of digits
%f, %e, %E: number of digits after .
%s: maximum number of characters
<stdio.h>, P225–230 
<stdio.h>, P257–262 
%[∗][width]specifier %d: skip white-spaces; match a decimal int%lf: skip white-spaces; match a double%c: match a char (do NOT skip white-spaces)%s: match a sequence of non-white-spaces%%: mathch a %%[∗][width]specifier %[∗][width]specifier 
<stdio.h>, P231–P237 
<stdio.h>, P263–P268 

scanf scanf. scanf? What should I use instead? 

大多数程序在产生输出之前往往需要执行一系列计算, 因此需要在程序执行过程中有一种 临时存储数据 的方法
变量: (Variable)程序执行过程中临时存储数据的单元
类型: (type)每一个变量都有一个类型, 类型用来说明变量所存储的数据的种类, int, float, double, char, char []
C 有多种类型, 包括int、float、double、char等类型的变量
int变量:
0, 1, 392或-2553#include <stdio.h>
#include <limits.h>
int main(void) {
printf("Max: %d, Min: %d", INT_MAX, INT_MIN);
return 0;
}
float/double 变量: 存储浮点数/双精度浮点数, 即可以带小数位, 如379.125
float/double 型变量算术运算通常比int型变量慢
float/double 在计算机中是近似存储的
需要注意:
char 变量: 存储字符, 如’A’
单引号
0000000 ~ 1111111, 0 ~ 127的整数
字符’a’的值是97, 'A’的值是65, '0’的值是48, ’ '的值是32
%c
#include <stdio.h>
#include <ctype.h>
int main(void) {
char gender = 'M';
printf("%c\n", toupper(gender));
printf("%c\n", gender + 32);
return 0;
}
声明: 变量使用前必须对其进行 声明(为编译器所做的描述)
指定变量的类型
说明变量的名字
int main(void)
{
声明
语句
}
在C99中, 声明可以不必出现在语句之前
int height;
float profit;
char gender;
char name[20];
int height, length, width, volume;
float profit, loss;
char gender;
char name[30], nation[20], address[60];
赋值: 通过赋值的方式获得值
height = 8;
lenght = 12;
width = 10;
gender = 'M';
8, 12, 10为常量
char name[30] = "Alex";
char nation[20] = "China";
char address[60] = "XianLin Avenue, Qixia District, Nanjing, China 210023";
变量在赋值或者以其他方式使用之前必须先声明
height = 9; /*** WRONG ***/
int height;
把包含小数点常量赋值给float型变量时, 最好在常量后加字母f:
float profit = 2150.48f;
double类型的变量赋值不要加字母f:
double pi = 3.14159;
混合类型赋值:
可以把int型的值赋给double型变量
也可以把double型值赋给int型变量, 但不一定安全
height = 8;
length = 12;
width = 10;
volume = height * length * width;
/* volume is now 960 */
printf 可用于打印变量的当前值
int height = 2;
char ch = 'a';
float profit = 1500f;
double pi = 3.14159;
printf("Height: %d\n", height);
printf("Character: %c\n", ch);
printf("Profit: %f\n", profit);
printf("Pi: %lf\n", pi);
%d: int型变量占位符, 用来指明变量height的值显示的位置
%c: char型变量占位符, 用来指明变量ch的值显示的位置
%f: float型变量占位符, 用来指明变量profit的值显示的位置
%lf: double型变量占位符, 用来指明变量pi的值显示的位置
浮点数
要显示float型变量, 需使用 %f 代替 %d, %f 默认会显示出小数点后6位
如果要强制 %f 显示小数点后 p 位数字, 可以把 .p 放置在 % 和 f 之间, 如:
printf("profit: %.2f", profit);
printf 打印的变量数量没有限制printf("Height: %d Length: %d", height, lenght);
当程序开始执行时, 某些变量会自动设零, 而大多数则不会
没有默认值并且尚未在程序中赋值的变量是 未初始化的(uninitialized)
若试图访问未初始化的变量, 可能会得到不可预知的结果
int height = 8;
这里, 数值8是一个初始化式.
同一个声明中可以对任意数量的变量进行初始化, 如:
int height = 8, length = 12, width = 10;
或
int height, length, width = 10;
printf 的功能不局限于显示变量中存储的数, 它还可以显示任意数值表达式的值, 如:
int volume = height * length * width;
printf("%d", volume);
可改写成:
printf("%d", height * length * width);
:fa-lightbulb-o: C 语言的一个通用原则: 在任何需要数值的地方, 都可使用具有相同类型的表达式
scanf 函数: C语言中对应于 printf 的库函数
scanf 与 printf 中的字母 f 含义相同, 都表示格式化
int i;
scanf("%d", &i);
/* reads an integer; stores into i */
& 符号通常(但不总是)在使用 scanf 时是必需的
读取浮点值的scanf调用:
scanf("%f", &x);
%f 告诉 scanf 查找浮点格式的输入值(数字可能包含小数点, 但不是必须的)