当前位置:网站首页>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这个属性变量的值就不能更改了。
边栏推荐
- Bonne Recommandation: développer des outils de sécurité pour les terminaux mobiles
- 30. 串联所有单词的子串
- swagger:如何生成漂亮的静态文档说明页
- [serialization] how to master the core technology of opengauss database? Secret 5: master database security (6)
- What are the specific steps for opening a stock account? Is it safe to open an account online?
- The successfully resolved idea cannot use the log normally after referencing Lombok's @slf4j
- Convex hull problem
- Project practice 6: distributed transaction Seata
- C primer plus learning notes - 3. Character IO (input / output)
- Pinda general permission system (day 1~day 2)
猜你喜欢

Detailed explanation of shutter textfield

Flutter TextField详解

On the origin of the dispute between the tradition and the future of database -- AWS series column

超分之VRT

stm32和电机开发(直流有刷电机和步进电机)
MySQL中存储过程的详细详解

MySQL - table creation and management

好物推薦:移動端開發安全工具

On the escape of inequality value

论数据库的传统与未来之争之溯源溯本----AWS系列专栏
随机推荐
[recommended collection] these 8 common missing value filling skills must be mastered
find_ path、find_ Library memo
Tiktok practice ~ sharing module ~ short video download (save to photo album)
Preliminary analysis of serial port printing and stack for arm bare board debugging
Project practice 5: build elk log collection system
Three basic backup methods of mongodb
Separate save file for debug symbols after strip
【最详细】最新最全Redis面试大全(42道)
C primer plus learning notes - 3. Character IO (input / output)
Unity——Mathf. Similarities and differences between atan and atan2
阿里云个人镜像仓库日常基本使用
股票开户的具体步骤是什么?网上开户安全吗?
Kubernetes 资源拓扑感知调度优化
vue中缓存组件keep-alive
论数据库的传统与未来之争之溯源溯本----AWS系列专栏
515. find the maximum value in each tree row
On the escape of inequality value
C language file cursor fseek
Current limiting design and Implementation
Serial port application program based on gd32