本文主要是介绍初学C语言(记录学习过程,慎重参考),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. C语言格式
#include<stdio.h>
int main(){return 0;}
不管写什么代码,格式要先敲上,防止遗忘。
练习1 身高换算标准体重
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{int H, M;printf("请输入您的身高:");scanf("%d", &H);M = (H-100)*0.9 * 2;printf("M=%d", M);return 0;
}
最上面的代码是因为VS上不能使用scanf为防止报错
3
scanf型输入
int a,b; //定义整数a,b
scanf("%d %d",a,b); //在输入中应该应该输入两个整数并空格// 若"%d,%d"同样输入的两个整数中间应用逗号// 否则将报错
4
用scanf语句输入int,float,double,char型数据
*用int定义整数
int a,b;
scanf("%d %d",&a,&b);
*float,double定义浮点数(小数)
float a,b;
scanf("%f %f",&a,&b);
printf输出
使用double型输出时%m.nf
m指输出的数字占m格,n指小数点后保留n位小数
double a=17.2143,b=263.9826;
printf("%5.3,%6.4",a,b);
则输出的a=17.214,b=263.982
double a,b;
scanf("%lf %lf",&a,&b);
*char定义字符(字母)
char a,b;
scanf("%c %c",a,b);
(1) getchar与putchar
#include<stdio.h>
int main()
{char a, b, c;a = getchar();b = getchar();c = getchar();a = a + 32;b = b + 32;c = c + 32;putchar(a);putchar(b);putchar(c);putchar('\n');return 0;}
getchar后的括号中输入字母
ps:大写字母+32=对应小写字母
putchar的括号后能输出转换好的字母
$$$$
%d输出整数 %f输出小数 %c输出字符 %e输出的值以科学计数法表示
%e
#include<stdio.h>
int main()
{
printf("你的身高为%e厘米\n",175.123);
return 0;
}
输出的结果是:你的身高为1.751230e+002厘米
(一般来说e前面的部分保留6位小数,同时与%m.nf类似:%m.ne m指m个格,n指小数点后保留n位小数)
1.751230e+002=1.751230*10^2
10^55=e+055 10^7=e+007
5 数学运算,各种函数的运用
在需要运用函数做题时,提前写好
#include<stdio.h>
#include<math.h>
其中#include<math.h>可以让你使用下列函数
(1)用公式计算面积
#include<stdio.h>
#include<math.h>
int main()
{double a, b, c, area,s;scanf("%lf %lf %lf", &a, &b, &c);s = (a + b + c) / 2;area = sqrt(s * (s - a) * (s - b) * (s - c));printf("%.2f", area);return 0;
}
这篇关于初学C语言(记录学习过程,慎重参考)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!