“欢迎回到C语言的世界”
这是我时隔好几个月再次打开VS Code后,脑中出现的低沉你而又智慧的陌生声音。这个声音带有驱动和鼓励,又带有一丝不满与勉励。这个声音推动人继续敲击键盘,用指尖点击字母方块,编织出自己奇妙的王国。
话不多说,继续在这里记录吧。此篇权当是回归篇,简要复习一下基础,找回手感。
字符串和格式化输入输出
eg.1:
//演示与用户交互
#include<stdio.h>
#include<string.h> //提供strlen()函数
#include<windows.h>
#define DENSITY 62.4 //人体密度
int main(void)
{
float weight,volume;
int size,letters;
char name[40];
printf("Hi,what's your first name?\n");
scanf("%s",&name);
printf("%s,what's your weight in pounds?\n",name);
scanf("%f",&weight);
size = sizeof(name);
letters = strlen(name);
volume = weight/DENSITY;
printf("Well,%S,your volume is %2.2f cubic feet.\n",name,volume);
printf("Also,your first name has %d letters,\n",letters);
printf("and we have %d bytes to store it.\n",size);
system("pause");
return 0;
}
eg.2:
//一些不匹配的整形转换
#include<stdio.h>
#include<windows.h>
#define PAGES 336
#define WORDS 65618
int main(void)
{
short num = PAGES;
short mnum = -PAGES;
printf("num as short and unsigned short: %hd %hu\n",num,num);
printf("-num as short and unsigned short:%hd %hu\n",num,num);
printf("num as int and char:%d %c\n",num,num);
printf("WORDS as int,short,and char:%d %hd %c\n",WORDS,WORDS,WORDS);
system("pause");
return 0;
}
//short 类型与 int 类型的区别:
1)字节数不同
2)数据范围不同
eg.3:
//不匹配的浮点类型转换
#include<stdio.h>
int main(void)
{
float n1 = 3.0;
double n2 = 3.0;
long n3 = 2000000000;
long n4 = 1234567890;
printf("%.le %.le %.le %.le\n",n1,n2,n3,n4);
printf("%ld %ld\n",n3,n4);
printf("%ld %ld %ld %ld\n",n1,n2,n3,n4);
getchar();
return 0;
}
//关于参数传递与类型转换
eg.4:
//input.c---何时使用&
#include<stdio.h>
#include<windows.h>
int main(void)
{
int age;
float assets;
char pet[30];
printf("Enter your age,assets,and favorite pet:\n");
scanf("%d %f",&age,&assets);//scanf()读取基本变量类型的值,在变量名前加上一个&
scanf("%s",pet);//scanf()把字符串读入字符数组中,不要使用&
printf("%d $%2.2f %s\n",age,assets,pet);
system("pause");
return 0;
}
eg.5:
//varwid.c---使用变宽输出字段
#include<stdio.h>
#include<windows.h>
int main(void)
unsigned width,precision;
int number = 256;
double weight = 242.5;
printf("Enter a field wigth:\n");
scanf("%d",&width);
printf("The number is :%*d.\n",width,number);
printf("Now enter a width and a precision:\n");
scanf("%d %d",&width,&precision);
printf("weight = %*.*f\n",width,precision,weight);
printf("Done!\n");
system("pause");
return 0;
}
/*printf()中可以用*修饰符代替字符宽度
而scanf()中,*则表示跳过相应输入项*/
运算符、表达式和语句
eg.1:
//shoes.c---计算多个不同鞋码对应的脚长
#include<stdio.h>
#define ADJUST 7.31
int main(void)
{
const double SCALE = 0.333;
double shoe,foot;
printf("Shoe size(men's) foot length\n");
shoe = 3.0;
while(shoe<18.5)
{
foot = SCALE*shoe+ADJUST;
printf("%10.lf %15.2f inches\n",shoe,foot);
shoe = shoe+1.0;
}
printf("If the shoe fits,wear it.\n");
getchar();
return 0;
}
eg.2:
//sizeof.c---sizeof()的使用
#include<stdio.h>
int main(void)
{
int n = 0;
size_t intsize;
intsize = sizeof(int);
printf("n = %d,n has %zd bytes;all ints have %zd bytes.\n",n,sizeof n,intsize);
getchar();
return 0;
}
/*sizeof返回size_t类型的值
typedef机制允许为现有类型创建别名
'typedef double real'
这样,real就是double的别名*/
eg.3:
//pound.c---定义一个带参数的函数
#include<stdio.h>
void pound(int n);//n 为int类型变量
int main(void)
{
int times = 5;
char ch = '!';
float f = 6.0;
pound(times);
pound(ch);
pound(f);
getchar();
return 0;
}
void pound(int n)
{
while(n-->0)
printf("#");
printf("\n");
}
循环控制语句
eg.1:
//suming.c--根据用户键入的整数求和
#include<stdio.h>
int main(void)
{
long num;
long sum =0L;//long 类型后要加L
int status;
printf("please enter an integer to be summed");
printf("(q to quit):");
status = scanf("%ld",&num);
while(status == 1)
{
sum += num;
printf("Please enter next integer(q to quit):");
status = scanf("%ld",&num);
}
printf("Those integer sum to %ld.\n",sum);
getchar();
return 0;
}
//注意,scanf()返回值赋给status,如果scanf读取一个整数,则返回1;若读取的不是数字,则返回0
eg.2:
//cmpflt.c---浮点数比较
#include<stdio.h>
#include<math.h>
int main(void)
{
const double ANSWER = 3.14159;
double response;
printf("What is the value of pi?\n");
scanf("%lf",&response);
while(fabs(response-ANSWER)>0.01)//fabs()取精度更高的double、float类型的绝对值,在math头文件里
{
printf("Try again!\n");
scanf("%lf",&response);
}
printf("Close enough!\n");
getchar();
return 0;
}
eg.3:
//truth.c---哪些值是为真
#include<stdio.h>
int main(void)
{
int n=3;
while(n)
printf("%2d is true.\n",n--);
printf("%2d is false.\n",n);
n = -3;
while(n)
{
printf("%2d is true.\n",n++);
}
printf("%2d is false.\n",n);
getchar();
return 0;
}
Ps:关于for语句:
for语句使用三个表达式控制循环过程
for(initialize;test;update)
statement
initialize表达式在执行for语句之前执行一次,然后对test表达式求值,接着对update求值。
eg.4:
//entry.c---入口条件循环
#include<stdio.h>
int main(void)
{
const int secret_code =13;
int code_entered;
printf("To enter the triskaidekaphobia therapy club,\n");
printf("please enter the secret code number:");
scanf("%d",&code_entered);
while(code_entered != secret_code)
{
printf("To enter the ttc,\n");
printf("please enter the secret code number:");
scanf("%d",&code_entered);
}
printf("Congratulations!You a cured!\n");
return 0;
}
eg.5:
//power.c---计算数的整数幂
#include<stdio.h>
double power(double n,int p);
int main(void)
{
double x,xpow;
int exp;
printf("Enter a number and the positive integer power\n");
printf("to which the number will be raised.Enter q to quit.");
while(scanf("%lf %d",&x,&exp)==2)
{
xpow = power(x,exp);
printf("%.3g to the power %d is %.5g\n",x,exp,xpow);
printf("enter next pair of numbers or q to quit.\n");
}
printf("hope you enjoyed this power trip--byebye!\n");
getchar();
return 0;
}
double power(double n,int p)
{
double pow=1;
int i;
for(i = 1;i<=p;i++)
pow*=n;
return pow;
}
分支和跳转控制语句
eg.1:
//colddays---统计寒冷天气百分比
#include<stdio.h>
#include<windows.h>
int main(void)
{
const int FREEZING = 0;
float temperature;
int cold_days = 0;
int all_days = 0;
printf("Enter the list of daily low temperatures.\n");
printf("Use Celsius,and enter q to quit.\n");
while(scanf("%f",&temperature)==1)
{
all_days++;
if(temperature<FREEZING)
{
cold_days++;
}
}
if(all_days!=0)
printf("%d days total:%.lf%% were below freezing.\n",all_days,100.0 * (float) cold_days/all_days);
if(all_days == 0)
printf("No data entered!\nplz try again!");
system("pause");
return 0;
}
eg.2:
//divisors.c---使用嵌套if语句显示一个数的约数
#include<stdio.h>
#include<stdbool.h>
#include<windows.h>
int main(void)
{
unsigned long num;//待测试的数
unsigned long div;//可能的约束
bool isPrime;
printf("Please enter an integer for analysis;");
printf("Enter q to quit.\n");
while(scanf("%lu",&num)==1)
{
for(div = 2,isPrime = true;(div*div)<=num;div++)
{
if(num%div==0)
{
if((div*div)!=num)
printf("%lu is divsible by %lu and %lu.\n",num,div,num/div);
else
printf("%lu is divisible by %lu and %lu.\n",num,div);
isPrime = false;//该数不是素数
}
}
if(isPrime)
printf("%lu is is prime.\n",num);
printf("Please enter another integer for analysis;");
printf("Enter q to quit.\n");
}
printf("byebye!\n");
system("pause");
return 0;
}
此外关于C语言中分支与跳转还有许多方法,例如if语句,switch语句,break、continue、goto语句等跳转语句,用法都类似,贯彻循环重复执行任务的思想。
函数
//lethead1.c
#include<stdio.h>
#define NAME "GALATHINK,INC."
#define ADDRESS "101 MEGABUCK PLAZA"
#define PLACE "MEGApolis,CA 94904"
#define WIDTH 40
void starbar(void);
int main(void)
{
starbar();
printf("%s\n",NAME);
printf("%s\n",ADDRESS);
printf("%s\n",PLACE);
starbar();
return 0;
}
void starbar(void)//简单地用这个函数做个边框
{
int count;
for(count = 1;count<=WIDTH;count++)
putchar('*');
putchar('\n');
}