当前位置:网站首页>0基础学c语言(1)
0基础学c语言(1)
2022-06-26 20:09:00 【the best b】
今天我们学习一个简单的找零计算。(今后的代码中都只有main{}里面的东西)
printf("100-23=%d\n",100-23);得到的结果为77,那么我们如何能在程序运行的时候输入那个数字23,然后计算输出结果呢。
int ptince=0;
printf("请输入金额(元):");
scanf("%d",&price);
int change =100 - price;
printf("找您%d元。\n",change);在上面这行代码中第一行int price=0
这一行定义了一个变量,变量的名称是price,类型是int,初始值是0。
变量对于c语言来说是一个保存数据的地方,当我们要记录输入的价格就需要一个变量来保存数据,这样才能参加到后面的计算中。
变量的组成部分一般是<类型名称><变量名称>;这两个,例如:
int price;
int amount;
int price,amount;
变量的名称是一种“标识符”,标识符和python中的字符串差不多,c语言中的标识符只能由字母、数字和下划线组成,数字不可能出现在第一个位置上,当然c语言中的关键字也不可以用做标识符。
c语言中的保留字有:
auto,break,case,char,const, continue,default,do,double, else,enum,extern,float,for, goto,if,int,long,register,return,short,signed,sizeof,static, struct,switch,typedef,union, unsigned,void,volatile,while, inline,restrict。
在变量定义的时候可以在定义中单独给单个变量赋值如:
int price=0,amount=100;
price=0是一个式子,这里的“=”是一个赋值运算符,表示将“=”右边的值赋给左边的变量
和数学不同,a=b在数学中表示关系,即a和b的值是一样的,而在c语言中,a=b是将b的值赋给a。
scanf其实相当于我们python中的input就是一个对内容的输出。
要求scanf这个函数读入下一个整数,然后把读到的结果赋值给变量price,当你在运行程序的时候输入了23,scanf将会把这个数据增加到price中,其中我们所看到的&字符,就是代表了加入的。
接着 int change =100-price;,定义了第二个变量change,并且做了计算在这个式子中有一个应该固定不变的数100,这时我们可以让他变成常量并直接写进程序里面
定义一个常量:
const int AMOUNT=100
然后在程序中我们可以直接将式子改为
int change=AMOUNT-price;
这个const是一个修饰符,加在int前面用来给这个变量加上一个不变的属性,一旦加上const这个属性变量的值就不能更改了。
边栏推荐
猜你喜欢

Tiktok practice ~ sharing module ~ generate short video QR code

Six necessary threat tracking tools for threat hunters

Pinda general permission system (day 3~day 4)

Flutter TextField详解

(几何) 凸包问题

Tiktok practice ~ homepage video ~ pull-down refresh

MySQL - subquery usage

Detailed explanation of retrospective thinking

Bonne Recommandation: développer des outils de sécurité pour les terminaux mobiles

抖音实战~分享模块~复制短视频链接
随机推荐
Guomingyu: Apple's AR / MR head mounted display is the most complicated product in its history and will be released in January 2023
Garbage collection mechanism of browser
Boot的单元测试
Analysis on development technology of NFT meta universe chain game system
西瓜书重温(七): 贝叶斯分类器(手推+代码demo)
IK分词器
Dynamic planning 111
【推荐收藏】这8个常用缺失值填充技巧一定要掌握
Redis Basics
2022/02/14 line generation
String string is converted to jsonarray and parsed
Arduino uno + DS1302 uses 31 byte static RAM to store data and print through serial port
Microservice architecture
find_ path、find_ Library memo
30. 串联所有单词的子串
Project practice 6: distributed transaction Seata
ImageView, glide load long picture (glide load picture)
On the escape of inequality value
問題解决:虛擬機無法複制粘貼文件
Minimum spanning tree, shortest path, topology sorting, critical path