标题
for 循环先判断条件,然后输出,然后再自增。所以最后不符合条件的自增值不符合条件不会输出。
输入输出
#include<stdio.h>
int main(void)
{
char ch;
scanf("%c",&ch);
while (ch != 'g')
{printf("%c",ch);
scanf("%c",&ch);}
return 0;
}
//Go west,young man!
//Go west,youn
输入两个数求四则运算
#include <stdio.h>
#include <math.h>
int main(void) {
int A,B;
printf("A = ");
scanf("%d",&A);
printf("B = ");
scanf("%d",&B);
printf("%d+%d=%d\n",A,B,A+B);
printf("%d-%d=%d\n",A,B,A-B);
printf("%d*%d=%d\n",A,B,A*B);
printf("%d/%d=%d\n",A,B,A/B);
return 0;
}
分段函数
#include <stdio.h>
#include <math.h>
int main(void) {
double x;
printf("x = ");
scanf("%lf",&x);
if (x==0) printf("f(0)=0\n");
else printf("f(%.1f)=%.1f\n",x,1/x);
return 0;
}
把鞋码转换成英寸
//
// 把鞋码转换成英寸.c
// c 语言
//
// Created by 华为matebook x on 2020/9/9.
//
#include "把鞋码转换成英寸.h"
#define ADJUST 7.31
int main(void)
{
const double SCALE = 0.333;//const 变量
double shoe,foot;
shoe = 9.0;
foot = SCALE*shoe+ADJUST;
printf("shoe size(men's) foot length\n");
printf("%10.1f %15.2f inches\n",shoe,foot);
return 0;
}
//
// 把鞋码转换成英寸.h
// c 语言
//
// Created by 华为matebook x on 2020/9/9.
//
#ifndef _________h
#define _________h
#include <stdio.h>
#endif /* _________h */
带参数的函数
//
// 带参数的函数.c
// c 语言
//
// Created by 华为matebook x on 2020/9/14.
//
#include "带参数的函数.h"
void pound(int n);
int main(void)
{
int times = 5;
char ch = '!';
float f = 6.0f;
pound(times);
pound(ch);
pound(f);
return 0;
}
void pound (int n)
{
while(n-->0)
printf ("#");
printf("\n");
}
//
// 带参数的函数.h
// c 语言
//
// Created by 华为matebook x on 2020/9/14.
//
#ifndef _______h
#define _______h
#include <stdio.h>
#endif /* _______h */
递减运算符
//
// 递减运算符.c
// c 语言
//
// Created by 华为matebook x on 2020/9/11.
//
#include "递减运算符.h"
#define MAX 10
int main(void)
{
int count = MAX + 1;
while(--count>0)
{
printf("宝哥有%d个小jj\n",count);
printf("Take one down and pass it around,\n");
printf("%d jjs of 宝哥!\n\n",count-1);
}
return 0;
}
//
// 递减运算符.h
// c 语言
//
// Created by 华为matebook x on 2020/9/11.
//
#ifndef ______h
#define ______h
#include <stdio.h>
#endif /* ______h */
浮点数比较
//
// 浮点数比较.c
// c 语言
//
// Created by 华为matebook x on 2020/9/16.
//
#include "浮点数比较.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.0001)
{
printf("Try again!\n");
scanf("%lf",&response);
}
printf("Close enough!\n");
return 0;
}
//除非输入的值与正确值之间相差 0.0001
//
// 浮点数比较.h
// c 语言
//
// Created by 华为matebook x on 2020/9/16.
//
#ifndef ______h
#define ______h
#include <stdio.h>
#endif /* ______h */
根据用户键入的整数求和
//
// 根据用户键入的整数求和.c
// c 语言
//
// Created by 华为matebook x on 2020/9/16.
//
#include "根据用户键入的整数求和.h"
int main(void)
{
long num;
long sum = 0L;//把 sum 初始化为 0
int status;//用户输入的值储存在 num 中,scanf 成功读取一个值返回 1 给 status,如果输入非数字则返回 0 给 status,循环终止
printf("Please enter an integer to be summed");
printf("(q to quit): ");
status = scanf("%ld",&num);
while(status == 1)
{
sum = sum + num;
printf("Please enter next integer (q to quit): ");
status = scanf("%ld",&num);
}
printf("Those integers sum to %ld.\n",sum);
return 0;
}
//
// 根据用户键入的整数求和.h
// c 语言
//
// Created by 华为matebook x on 2020/9/16.
//
#ifndef ____________h
#define ____________h
#include <stdio.h>
#endif /* ____________h */
计算 1 到 20 的平方
//
// 计算 1 到 20 的平方.c
// c 语言
//
// Created by 华为matebook x on 2020/9/9.
//
#include "计算 1 到 20 的平方.h"
int main(void)
{
int num = 1;
while (num < 21)
{
printf("%4d %6d\n",num,num*num);
num = num +1;
}
return 0;
}
//
// 计算 1 到 20 的平方.h
// c 语言
//
// Created by 华为matebook x on 2020/9/9.
//
#ifndef ___1___20_____h
#define ___1___20_____h
#include <stdio.h>
#endif /* ___1___20_____h */
求模运算符(取余)的应用
//
// 求模运算符(取余)的应用.c
// c 语言
//
// Created by 华为matebook x on 2020/9/11.
//
#include "求模运算符(取余)的应用.h"
#define SEC_PER_MIN 60
int main(void)
{
int sec,min,left;
printf("Convert seconds to minutes and seconds!\n");
printf("Enter the number of seconds(<=0 to quit):\n");
scanf("%d",&sec);//读取秒数
while(sec>0)
{
min = sec/SEC_PER_MIN;
left = sec%SEC_PER_MIN;
printf("%d seconds is %d minutes,%d seconds.\n",sec,min,left);
printf("Enter next value(<= to quit):\n");
scanf("%d",&sec);
}
printf("Done!\n");
return 0;
}
//
// 求模运算符(取余)的应用.h
// c 语言
//
// Created by 华为matebook x on 2020/9/11.
//
#ifndef _____________h
#define _____________h
#include <stdio.h>
#endif /* _____________h */
使用变宽输出字段
//
// 使用变宽输出字段.c
// c 语言
//
// Created by 华为matebook x on 2020/9/6.
//
#include "使用变宽输出字段.h"
int main(void)
{
unsigned width,precision;
int number = 256;
double weight = 242.5;
printf("Enter a field width:\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");
return 0;
}
//
// 使用变宽输出字段.h
// c 语言
//
// Created by 华为matebook x on 2020/9/6.
//
#ifndef _________h
#define _________h
#include <stdio.h>
#endif /* _________h */
题 5-7
//
// 题 5-7.c
// c 语言
//
// Created by 华为matebook x on 2020/9/14.
//
#include "题 5-7.h"
int main(void)
{
char c1;
int num = 0;
c1 = 'a';
while(num<26)
{
printf("%c ",c1);
num++;
c1 = c1+1;
}
printf(" \n");
return 0;
}
//
// 题 5-7.h
// c 语言
//
// Created by 华为matebook x on 2020/9/14.
//
#ifndef __5_7_h
#define __5_7_h
#include <stdio.h>
#endif /* __5_7_h */
题
//
// 题.c
// c 语言
//
// Created by 华为matebook x on 2020/9/14.
//
#include "题.h"
#define FORMAT "%s! C is cool!\n"
int main(void)
{
int num = 10;
printf(FORMAT,FORMAT);
//宏定义的作用是在编bai译前将所有du代码中的宏名称替换为宏定义中的代码。
//因此printf(FORMAT,FORMAT)在编译前将被替换为printf("%s!dao C is cool!\n","%s! C is cool!\n")
//然后编译器进行解析,第一个字符串是输出格式参数,其中有个控制符%s代表输出字符串,因此编译器会认为第二个参数是一个字符串
//因此将%s! C is cool!\n作为字符串输出到%s的位置,
//输出结果就是%s! C is cool! 回车 ! C is cool!
printf("%d\n",++num);
printf("%d\n",num++);
printf("%d\n",num--);
printf("%d\n",num);
return 0;
}
//
// 题.h
// c 语言
//
// Created by 华为matebook x on 2020/9/14.
//
#ifndef __h
#define __h
#include <stdio.h>
#endif /* __h */
跳过 scanf 输入的前两个数
//
// 跳过 scanf 输入的前两个数.c
// c 语言
//
// Created by 华为matebook x on 2020/9/6.
//
#include "跳过 scanf 输入的前两个数.h"
int main(void)
{
int n;
printf("Please enter three integers:\n");
scanf("%*d %*d %d",&n);
printf("The last integer was %d\n",n);
return 0;
}
//
// 跳过 scanf 输入的前两个数.h
// c 语言
//
// Created by 华为matebook x on 2020/9/6.
//
#ifndef ___scanf_________h
#define ___scanf_________h
#include <stdio.h>
#endif /* ___scanf_________h */
无限 bug
//
// 6.c
// c 语言
//
// Created by 华为matebook x on 2020/9/16.
//
#include "无限 bug.h"
int main(void)
{
int n = 0;
while(n<3)
printf("n is %d\n",n);
n++;
printf("That's all this program does\n");
return 0;
}
//
// 6.h
// c 语言
//
// Created by 华为matebook x on 2020/9/16.
//
#ifndef __h
#define __h
#include <stdio.h>
#endif /* __h */
指数
//
// 指数.c
// c 语言
//
// Created by 华为matebook x on 2020/9/9.
//
#include "指数.h"
#define SQUARES 64
int main(void)
{
const double CROP = 2E16;
double current,total;
int count = 1;
printf("square grains total ");
printf("fraction of \n");
printf(" added grains ");
printf("world total\n");
total = current = 1.0;//从第一粒谷粒开始
printf("%4d %13.2e %12.2e %12.2e\n",count,current,total,total/CROP);
while (count<SQUARES)
{
count = count + 1;
current = 2.0 * current;//下一个方格谷粒翻倍
total = total + current;//更新总数
printf("%4d %13.2e %12.2e %12.2e\n",count,current,total,total/CROP);
}
printf("That's all.\n");
return 0;
}
//
// 指数.h
// c 语言
//
// Created by 华为matebook x on 2020/9/9.
//
#ifndef ___h
#define ___h
#include <stdio.h>
#endif /* ___h */
自动类型转换
//
// 自动类型转换.c
// c 语言
//
// Created by 华为matebook x on 2020/9/14.
//
#include "自动类型转换.h"
int main(void)
{
char ch;
int i;
float fl;
fl = i = ch = 'C';
printf("ch = %c,i = %d,fl = %2.2f\n",ch,i,fl);
ch = ch+1;
i = fl + 2*ch;
fl = 2.0 * ch + i;
printf("ch = %c,i = %d,fl = %2.2f\n",ch,i,fl);
ch = 1107;
printf("Now ch = %c\n",ch);
ch = 80.89;
printf("Now ch = %c\n",ch);
return 0;
}
bool 类型
//
// bool 类型.c
// c 语言
//
// Created by 华为matebook x on 2020/10/5.
//
#include <stdio.h>
int main(void)
{
long num;
long sum = 0L;
_Bool input_is_good;
printf("Please enter an integer to be summed ");
printf("(q to quit): ");
input_is_good = (scanf("%ld", &num) == 1);//判断输入是不是数字,是赋值给 num,否直接退出 while
while (input_is_good)
{
sum = sum + num;
printf("Please enter next integer (q to quit): ");
input_is_good = (scanf("%ld", &num) == 1);
}
printf("Those integers sum to %ld.\n",sum);
return 0;
}
define预处理器
//
// define预处理器.c
// c 语言
//
// Created by 华为matebook x on 2020/9/5.
//
#include "define预处理器.h"
#define PI 3.14159
int main(void)
{
float area,circum,radius;
printf("what is the radius of your pizza?\n");
scanf("%f",&radius);
area = PI * radius * radius;
circum = 2.0 * PI * radius;
printf("circumference = %1.2f,area = %1.2f\n",circum,area);//%1.2结果被四舍五入为两位小数
return 0;
}
running
//
// running.c
// c 语言
//
// Created by 华为matebook x on 2020/9/14.
//
#include "running.h"
const int S_PER_M = 60;//一分钟的秒数
const int S_PER_H = 3600;//一小时的分钟数
const double M_PER_K = 0.62137;//一公里的英里数
int main(void)
{
double distk,distm;//跑过的距离
double rate;//平均速度
int min,sec;//跑步用时
int time;
double mtime;
int mmin,msec;
printf("This program converts your time for a metric race \n");
printf ("to a time for running a mile and to your average\n");
printf ("speed in miles per hour.\n");
printf ("Please enter, in kilometers, the distance run.\n");
scanf("%lf",&distk);
printf("Next enter the time in minutes and seconds.\n");
printf ("Begin by entering the minutes.\n");
scanf ("%d",&min);
printf("Now enter the seconds.\n");
scanf("%d",&sec);
time = S_PER_M*min +sec;
distm = M_PER_K*distk;
rate = distm/time *S_PER_H;
mtime = (double)time/distm;
mmin = (int)mtime/S_PER_M;
msec = (int)mtime%S_PER_M;
printf("You ran %1.2f km (%1.2f miles) in %d min,%d sec.\n",distk,distm,min,sec);
printf("That pace corresponds to running a mile in %d min,",mmin);
printf("%d sec.\nYour average speed was %1.2f mph.\n",msec,rate);
return 0;
}
//
// running.h
// c 语言
//
// Created by 华为matebook x on 2020/9/14.
//
#ifndef running_h
#define running_h
#include <stdio.h>
#endif /* running_h */
标题
//
// strlen.c
// c 语言
//
// Created by 华为matebook x on 2020/9/5.
//
#include "strlen.h"
#include<string.h>
#define PRAISE "You are an extraodinary being."
int main(void)
{
char name[40];
printf("what's your name?");
scanf("%s",name);
printf("hello,%s.%s\n",name,PRAISE);
printf("Your name of %zd letters occupies %zd memory cells.\n",strlen(name),sizeof name);
printf("The phrase of praise has %zd letters",strlen(PRAISE));
printf(" and occupies %zd memory cells.\n",sizeof PRAISE);
return 0;
}
//
// strlen.h
// c 语言
//
// Created by 华为matebook x on 2020/9/5.
//
#ifndef strlen_h
#define strlen_h
#include <stdio.h>
#endif /* strlen_h */
打印乘法表
//
// 打印乘法表.c
// 1111
//
// Created by 华为matebook x on 2020/3/7.
// Copyright © 2020 华为matebook x. All rights reserved.
//
#include "stdio.h"
int main()
{
int i,j;
for(i=1;i<=9;i++)
{
for(j=1;j<=i;j++)
{
printf("%d*%d=%-2d ",i,j,i*j);
}
putchar('\n');/*/每一次循环就换行*/
}
return 0;
}
// 打印乘法表.h
// 1111
//
// Created by 华为matebook x on 2020/3/7.
// Copyright © 2020 华为matebook x. All rights reserved.
//
#ifndef ______h
#define ______h
#include <stdio.h>
#endif /* ______h */
求三角形面积
//
// 求三角形面积.c
// 1111
//
// Created by 华为matebook x on 2020/3/9.
// Copyright © 2020 华为matebook x. All rights reserved.
//
#include <stdio.h>
#include<math.h>
int main()
{
float a,b,c,s,area;
scanf("%f%f%f",&a,&b,&c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));//sqrt开平方根
printf("a=%.2f,b=%.2f,c=%.2f\n",a,b,c);
printf("area=%.3f\n",area);
return 0;
}
交换变量
//
// 交换变量.c
// 1111
//
// Created by 华为matebook x on 2020/3/9.
// Copyright © 2020 华为matebook x. All rights reserved.
//
#include <stdio.h>
int main()
{
int x,y,t;
x=5,y=6;
t=x,x=y,y=t;
printf("%d,%d,%d\n",x,y,t);
return 0;
}
判断素数
//
// 判断素数.c
// 1111
//
// Created by 华为matebook x on 2020/3/7.
// Copyright © 2020 华为matebook x. All rights reserved.
//
#include <stdio.h>
int main()
{
int i,num;
_Bool flag=1;
printf("请输入一个整数: ");
scanf("%d",&num);
for(i=2;i<num;i++)
{
if (num%i==0)
{
flag=0;
}
}
if(flag)
{
printf("%d是一个素数\n",num);
}
else
{
printf("%d不是素数\n",num);
}
return 0;
}
字符输入输出
//
// 字符输入输出.c
// 1111
//
// Created by 华为matebook x on 2020/3/9.
// Copyright © 2020 华为matebook x. All rights reserved.
//
#include <stdio.h>//从键盘输入一个大写字母,要求以小写字母输出
int main()
{
char c1,c2;
c1=getchar();//从键盘输入字符型常量时不用单引号,只能接收一个字符
putchar(c1);
putchar('\n');
c2=c1+32;
putchar(c2);//想再加个换行怎么处理?
return 0;
}
//printf中 %x输出十六进制整数。 %c输出一个字符。 %s输出字符串
continue语句
//
// continue语句.c
// 1111
//
// Created by 华为matebook x on 2020/3/7.
// Copyright © 2020 华为matebook x. All rights reserved.
//
#include <stdio.h>//带有C的字符会被消去
int main()
{
int ch;
while((ch=getchar())!='\n')
{
if(ch=='C')//如果相等不执行下面putchar
{
continue;
}
putchar(ch);
}
putchar('\n');
return 0;
}
printf百分号错误点
//
// printf百分号错误点.c
// 1111
//
// Created by 华为matebook x on 2020/3/9.
// Copyright © 2020 华为matebook x. All rights reserved.
//
#include<stdio.h>
int main()
{
int a,b,c,t;
scanf("%d,%d,%d\n",&a,&b,&c);
t=(a+b+c)/3;
printf("a=%d,b=%d,c=%d,t=%d\n",a,b,c,t);
return 0;
}