当前位置:网站首页>The first lesson of HNUMSC-C language
The first lesson of HNUMSC-C language
2022-08-09 02:08:00 【The bionic programmers will dream of electric sheep】
Clanguage first
简介
- C语言是一种底层语言
- C语言是一种小型语言
- C语言是一种包容性语言
hello world
#include <stdio.h>
int main(void)
{
printf("Hello World!");
return 0;
}
#include <stdio.h>
头文件,包含C语言标准输入/输出库的相关信息
C语言规定每条Statements need to end with a semicolon(当然,也有例外)
注释
文档说明
符号/标记注释的开始,符号/Marks the end of a comment
/* This is a comment */
Comments can appear almost anywhere in a program.
Comments can also span multiple lines
#include <stdio.h>
/* Author:Bio Sheep
website:https://algernon98.github.io/
*/
int main(void)
{
printf("Hello World!");
return 0;
}
General comments are placed before code sections,或者 同一行
int main(void)
{
printf("欲买桂花同载酒,终不似,少年游"); /*文本部分*/
return 0;
}
注意:大小写 “ ” "
句尾分号;
另一种注释:
int main(void)
{
printf("欲买桂花同载酒,终不似,少年游"); //文本部分
return 0;
}
Such comments are automatically terminated at the end of the line
变量
Every variable must have a type,Used to describe the type of data stored in the variable.
intType variables can store integers,However, the value range is limited;float型变量可以存储带小数位的数.
类型 | 举例 |
---|---|
int | 0、114、-255 |
float | 114.514、-0.11 |
声明
Dad's nephew is an archaeologist,This day he brought back a wonderful gadget,I want to let the well-informed old man slap his eyes.But even Dad needs time to study,So Dad wanted to customize a beautiful little box to hold this seemingly unusual treasure,You can design a program to help Dad determine this the size of the box?
首先,We need to know the length, width and height of the box.
长、宽、High is in the program变量
在C语言中,Variables must be used before they can be used声明
声明变量,First specify the variable type,Then specify the variable name,
For example, we need to introduce two variables,物体的高(整数)和重量(浮点数)
int height;
float weight;
赋值
从右往左赋值
//for a high8,长为12,宽为10的物体,Assign values to three variables accordingly:
height =8;
length =12;
width = 10;
但是,before assigning a value to a variable,Need to make sure the variable has been declared before this:
int height;
float length;
float width;
height =8;
length =12.14159;
width = 10;
让我们看看输出:
printf("%d",height);
printf("%f",length);
printf("%f",width);
812.14159010.000000
显然,这不是我们想要的结果
We will make some adjustments to the code:
int height;
float length;
float width;
height =8;
length =12.14159;
width = 10;
printf("height=%d\n",height);
printf("length=%f\n",length);
printf("width=%f",width);
输出:
height=8
length=12.141590
width=10.000000
可以看到,Without specifying the number of decimal places to retain,Floating point numbers default to six decimal places
声明的时候,We can also assign values together,This can be done on variables初始化
int height=8,length=12,width=10; //分别对高、Length and width are initialized
int height,length,width=10;//声明三个变量,But only for wide initialization
格式化输入输出
Dad has been very annoying lately!The last antique has not been researched yet,The worry-free nephew brought him several more gadgets,要知道,Boxes with magic are very difficult to customize,Different antiques also have different parameters,Can you improve the program,You can find the suitable box volume according to Dad's requirements?
In order to meet the requirements of the father,This program needs to allow users to enter their own dimensions.
The function that gets user input isscanf函数,And the output is usedprintf函数
Both require a format string to specify the form of input or output data.
读入一个int型值:
scanf("%d".&i); //Store the input value inint型变量i中
同样的,floattype value read:
scanf("%f".&j); //Store the input value infloat型变量j中
改进的程序:
#include <stdio.h>
/* Author:Bio Sheep
website:https://algernon98.github.io/
*/
int main(void)
{
int height,length,width,volume;
printf("Enter the height of the box:") ;
scanf("%d",&height);
printf("Enter the length of the box:") ;
scanf("%d",&length);
printf("Enter the width of the box:") ;
scanf("%d",&width);
volume=height*length*width;
printf("The volume of the box is :%d",volume);
return 0;
}
输出:
Enter the height of the box:1
Enter the length of the box:2
Enter the width of the box:3
The volume of the box is :6
What if we wish to keep three decimal places?
printf("The volume of the box is :%.3f",volume);
转换说明
我们可以用%.1fto display with one digit after the decimal pointfloat型值.
%m.pX形式
m是最小 字段宽度,p是精度,X是转换说明符
对于X:
- d表示十进制形式的整数
- e表示指数(科学计数法)形式的浮点数
- f是浮点数(没有指数)
%d
%5d
%-5d
%5.3d
%5.3f
%5.3e
转义序列
换行符 : \n
printf("欲买桂花同载酒,终不似,少年游");
printf("欲买桂花同载酒,\n终不似,\n少年游.");
输出:
欲买桂花同载酒,终不似,Juvenile You want to buy sweet-scented osmanthus with wine,
终不似,
少年游.
修改:
printf("欲买桂花同载酒,终不似,少年游\n");
printf("欲买桂花同载酒,\n终不似,\n少年游.");
欲买桂花同载酒,终不似,少年游
欲买桂花同载酒,
终不似,
少年游.
转义序列 " ,表示字符"
printf("老爹说:\"还有一件事!\"");
表达式
算数运算符
**一元运算符 **
+ | - |
---|
二元运算符
加法类 | 乘法类 |
---|---|
+加法运算符 | *乘法运算符 |
-减法运算符 | /除法运算符 |
%求余运算符 |
10%3的值是1,10/3的值是3
复合赋值
i=i+2;
可以简写为:
i+=2;
其他同上
v-=e表示v加上e,然后将结果存储到v中
自增、自减
i=i+1;
j=j-1;
You can use the increment operator(++)和自减运算符(–)缩短为:
i++;
j--;
思考:++i和i++有什么区别?
编程题,Two digits are printed in reverse order
选择语句
- 选择语句 if switch
- 重复语句 while do for 循环
- 跳转语句 break continue goto
逻辑表达式
关系运算符
在C语言中,诸如i<jThis comparison operation produces integers:
0(假)或1(真)
10<11的值为1(true)
11<10的值为0(false)
符号 | 含义 |
---|---|
< | 小于 |
> | 大于 |
<= | 小于等于 |
>= | 大于等于 |
判等运算符
符号 | 含义 |
---|---|
== | 等于 |
!= | 不等于 |
逻辑运算符
符号 | 含义 |
---|---|
! | 逻辑非 |
&& | 逻辑与 |
|| | 逻辑或 |
if 语句
if(表达式)语句
判定0<i<n是否成立:
if (0<i && i<n)
复合语句:
if(i>0){
i--;
j++;
}
else语句
if (i>j)
max=i;
else
max=j;
if嵌套
#include <stdio.h>
/* Author:Bio Sheep
website:https://algernon98.github.io/
*/
int main(void)
{
int i=5,j=4,max;
if(i>0){
if(i>j){
max=i;
}
else{
max=j;
}
}
else{
max=j;
}
printf("%d",max);
return 0;
}
级联式if语句
if (i>j)
printf("i is greater than j\n");
else if (i<j)
printf("i is less than j\n");
else
printf("i is equal to j\n")
switch语句
switch(grade){
case 3:
case 2:
case 1: printf("passing");
break;
case 0: printf("falling");
break;
}
break语句
breakStatements make programs“跳”出switch语句,继续执行switch后面的语句
边栏推荐
猜你喜欢
MT4/MQ4L入门到精通EA教程第二课-MQL语言常用函数(二)-账户信息常用功能函数
力扣刷题记录7.1-----707. 设计链表
多语种翻译-免费多语种翻译软件
力扣刷题记录8.1-----206. 反转链表
etcd实现大规模服务治理应用实战
JDBC technology (2) - set up common sql and configuration files
Mysql 5.7 into the pit
MT4 / MQ4L entry to the master of EA tutorial lesson two (2) - - MQL language commonly used function account information commonly used functions
JDBC技术(一)——一个简单的JDBC测试
LeetCode每日两题02:轮转数组 (均1200道)
随机推荐
The 7 taboos of time management summarized by the postgraduate students, how many have you won?
Simple example of .reduce()
How js implements array deduplication (7 kinds)
JDBC技术(三)——使用Druid数据库连接池测试
Grid布局介绍
数据恢复软件EasyRecovery支持恢复所有类型的文件
企业里Foxmail邮箱问题解决方法汇总
OpenMLDB + Jupyter Notebook:快速搭建机器学习应用
Phenomenon 1 during RF debugging
Educational Codeforces Round 132 (Rated for Div. 2)
[深入研究4G/5G/6G专题-55]: L3信令控制-4-软件功能与流程的切分-CU网元的信令
JDBC技术(二)——设置通用的sql和配置文件
力扣刷题记录4.1-----209. 长度最小的子数组
How SEMRush finds keywords for advertising
力扣刷题记录7.1-----707. 设计链表
史上最猛“员工”,疯狂吐槽亿万富翁老板小扎:那么有钱,还总穿着同样的衣服!
Use of torchversion.transforms
Go-12-结构体
年金险的安全性怎么样啊?可靠吗?
力扣刷题记录5.1-----59. 螺旋矩阵 II