sizeof()函数
sizeof(int),sizeof(char)等等……
整型,浮点的上溢出,下溢出?
#include
int main(){
int a = 0, b =0;
int pn1 = printf("ok!\n"); // pn1 = 4
int pn2 = printf("ok!,%d,%d\n",a, b); // pn2 = 8
int sn1 = scanf("%d", &a, &b); // sn1 = 1
int sn2 = scanf("%d%d", &a, &b); // sn2 = 2
printf("pn1=%d, sn1=%d, pn2=%d, sn2=%d\n", pn1, sn1, pn2, sn2);
return 0;
}
#include
int main(){
char a[27] = "abcdefghijklmnopqrstuvwxyz";
int i = 0;
while( i++ < 26){
printf("%c\n", a[i-1]); //前面+1 ,-1即为真实元素
}
return 0;
}
#include
int main(){
char lets[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i, j;
for (i = 0; i < 6; i++)
{
for (j = 0; j <= i; j++)
{
printf("%c", lets[i*(i+1)/2+j]);
}
printf("\n");
}
return 0;
}
#include
#include
#define MAX_LEN 255
int main(){
char a[MAX_LEN];
scanf("%s", a);
int len = strlen(a);
while(len-- > 0){
printf("%c", a[len]);
}
return 0;
}
void bb(int a[]){
int i, j;
for (i = 0; i < LEN -1; ++i)
{
for (j = 0; j < LEN - i - 1; ++j)
{
if(a[j] < a[j+1])
{
swap(&a[j], &a[j+1]);
}
}
}
}
if(n == 0)
s = 1;
else
s = -1;
ch = getchar(); //scanf("%c", &ch);
putchar(ch); //printf("%c", ch);
函数名 | 作用 |
---|---|
isalnum() | 字母或数组 |
isalpha() | 字母 |
isblank() | 空白符 |
isdigit() | 数字 |
islower() | 小写字母 |
isupper() | 大写字母 |
isxdigit() | 16进制字母 |
tolower() | 返回小写字母 |
toupper() | 返回大写字母 |
- 多重选择if …… (else if) …… else:在多条语句中选择,完全等价与if else多重嵌套模式
if (score <= 60)
score = 6;
else if(score <= 70 )
score = 7;
else if(score <= 80)
score = 8;
else
score = 10;
max = a > b ? a : b;
switch (整型表达式)
{
case 常量1:
语句
case 常量2:
语句
default:
语句
}
#include
int main(){
char ch, pre;
int n = 0;
while( (ch = getchar()) != '#' ){
if(pre == 'e' && ch == 'i') //切记
n++;
pre = ch;
}
printf("%d\n", n); //切记
return 0;
}
#include
int main(){
char choice;
float fweight = 0, sweight = 0, tweight = 0;
float fcharge = 0, scharge = 0, tcharge = 0;
float weight, price, charge, orderCharge, orderWeight, account, extCharge, pay;
printf("enter a, b, c chooce your goods, q is quit:\n");
while ( (choice = getchar()) != 'q' )
{
if('\n' == choice)
continue;
if( choice == 'a' || choice == 'b' || choice == 'c' )
{
switch (choice)
{
case 'a':
price = 2.05;
printf("please enter want to buy yangli weight:\n");
scanf("%f", &weight); ;
fweight += weight;
fcharge += fweight * price;
printf("%.2f\n", fweight);
break;
case 'b':
price = 1.15;
printf("please enter want to buy taincai weight:\n");
scanf("%f", &weight);
sweight += weight;
scharge += sweight * price;
printf("%.2f\n", sweight);
break;
case 'c':
price = 1.09;
printf("please enter want to buy hulubo weight:\n");
scanf("%f", &weight);
tweight += weight;
tcharge += tweight * price;
printf("%.2f\n", tweight);
break;
default: ;
}
}
else
{
printf("pleae enter 'a', 'b', 'c' !\n");
}
printf("enter a, b, c chooce your goods, q is quit:\n");
}
orderWeight = fweight + sweight + tweight;
orderCharge = fcharge + scharge + tcharge;
account = orderCharge > 100 ? orderCharge * 0.05 : 0;
if( orderWeight <= 5 )
extCharge = 6.5;
else if( orderWeight <= 20)
extCharge = 14;
else
extCharge = 14 + (orderWeight-14) * 0.5;
pay = orderCharge + extCharge - account;
printf("******************* order ******************\n");
printf("*name-------price------weight-------charge*\n");
printf("*yang-------$2.05--------%9.2f---------$%9.2f*\n", fweight, fcharge);
printf("*tian-------$1.15--------%9.2f---------$%9.2f*\n", sweight, scharge);
printf("*hulobo-----$1.09--------%9.2f---------$%9.2f*\n", tweight, tcharge);
printf("totalWeight:%.2f, orderCharge:$%.2f", orderWeight, orderCharge);
printf("account:$%.2f, extCharge:$%.2f, pay:$%.2f\n", account, extCharge, pay);
printf("*************************************************\n");
return 0;
}
缓冲区:字符被收集存储的临时存储区(是否有无缓冲输入取决于系统)正常的都是有缓冲输入
结束键盘输入:文件,流,键盘输入
#include
int main(){
char ch;
while( (ch =getchar()) != '#')
{
putchar(ch);
}
}
使用的#字符可能会被我们用到,使用#退出并不一定起作用
c语言把输入输出设备,视为文件,stdin流表示键盘输入,stdout流表示显示输出
使用文件的形式来结束键盘的输入
- 文件结尾:Ctrl+Z(曾经操作系统),存储文件大小信息,EOF(C语言)
#define EOF -1 //stdio.h定义的
while( (ch =getchar()) != EOF)
testFileIO < words // UNIX与DOS 输入重定向
testFileIO > mywords //输出重定向 DOS Ctrl+Z结束,UNIX Ctrl+D结束
testFileIO < words > mywords //组合重定向
不能读多个文件,也不能写多个文件,空格不是必须的,写入的会把之前的覆盖掉
- 友好的用户界面
丢弃换行符
while (getchar() != '\n')
continue;
if(2 != scanf("%d%d", &a, &b))
break;
while( scanf("%ld", &n) == 1 && n > 0 ) //验证正整数