1.字符型常量
单个字符,一对单引号及其所括起来的字符。定义时用类型名char。
例如'A'、'9'......每个字符有唯一的ASCII码。'A'=65
函数调用的格式控制说明为%c。
2.字符输入函数getchar()
只能读入一个字符
3.字符输出函数putchar()
输出字符型变量或字符型常量。只能输出一个字符。
4.逻辑运算
!逻辑非 &&逻辑与 ||逻辑或 ——逻辑运算符(优先级低于关系运算符 )
例3-7
#include<stdio.h>
int main()
{
int digit,letter,other;
char ch;
int i;
digit=letter=other=0;
printf("Enter 10 characters: ");
for(i=1;i<=10;i++)
{
ch=getchar();
if((ch>='a'&&ch<='z') ||( ch>='A'&&ch<='Z'))
letter++;
else if(ch>='0'&&ch<='9')
digit++;
else
other++;
}
printf("letter=% d,digit=% d,other=%d\n",letter,digit,other);
return 0;
}
Enter 10 characters: ed245@EW^$
letter= 4,digit= 3,other=3
例3-4
#include<stdio.h>
int main()
{
int n1,n2,n3,n4,n5,i;
char ch;
n1=n2=n3=n4=n5=0;
printf("enter 10 ch: ");
for(i=1;i<=10;i++)
{
ch=getchar();
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
{
n1++;
}
else if(ch==' ')
{
n2++;
}
else if(ch=='\n')
{
n3++;
}
else if(ch>='0'&&ch<='9')
{
n4++;
}
else
n5++;
}
printf("n1=%d,n2=%d,n3=%d,n4=%d,n5=%d\n",n1,n2,n3,n4,n5);
return 0;
}
enter 10 ch: 567#¥% hh
n1=2,n2=1,n3=0,n4=3,n5=4
练习3-5
#include<stdio.h>
int main()
{
int year,i,n;
printf("enter n: ");
scanf("%d",&n);
for(i=2000;i<=n;i++)
{
if(i%400==0||((i%4==0)&&(i%100!=0)))
{
printf("%d ",i);
}
}
return 0;
}
enter n: 2467
2000 2004 2008 2012 2016 2020 2024 2028 2032 2036 2040 2044 2048 2052 2056 2060 2064 2068 2072 2076 2080 2084 2088 2092 2096 2104 2108 2112 2116 2120 2124 2128 2132 2136 2140 2144 2148 2152 2156 2160 2164 2168 2172 2176 2180 2184 2188 2192 2196 2204 2208 2212 2216 2220 2224 2228 2232 2236 2240 2244 2248 2252 2256 2260 2264 2268 2272 2276 2280 2284 2288 2292 2296 2304 2308 2312 2316 2320 2324 2328 2332 2336 2340 2344 2348 2352 2356 2360 2364 2368 2372 2376 2380 2384 2388 2392 2396 2400 2404 2408 2412 2416 2420 2424 2428 2432 2436 2440 2444 2448 2452 2456 2460 2464