当前位置:网站首页>0 basic C language (1)

0 basic C language (1)

2022-06-26 20:20:00 the best b

Today we learn a simple change calculation .( In the future, there will be only main{} What's inside )

printf("100-23=%d\n",100-23);

The result is zero 77, So how can we enter that number while the program is running 23, Then calculate the output .

int ptince=0;

printf(" Please enter the amount ( element ):");
scanf("%d",&price);
int change =100 - price;
printf(" Looking for you %d element .\n",change);

The first line in the above line of code int price=0

This line defines a variable , The name of the variable is price, The type is int, The initial value is 0.

  Variables for c Language is a place to store data , When we want to record the entered price, we need a variable to save the data , Only in this way can we participate in the following calculation .

The components of variables are generally < Type the name >< Variable name >; these two items. , for example :

int price;

int amount;

int price,amount;

  The name of the variable is a kind of “ identifier ”, Identifier and python The string in is similar ,c Identifiers in languages can only consist of letters 、 Numbers and underscores , The number cannot appear in the first position , Of course c A keyword in a language cannot be used as an identifier .

c Reserved words in the language are :

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.

  When defining a variable, you can assign a value to a single variable in the definition, such as :

int price=0,amount=100;

 price=0 It's a formula , there “=” Is an assignment operator , It means that you will “=” The value on the right is assigned to the variable on the left

Unlike math ,a=b To express relationships in mathematics , namely a and b The value of is the same , And in the c In language ,a=b Yes, it will b The value is assigned to a.

 scanf Actually, it's equivalent to us python Medium input It is an output of content .

requirement scanf This function reads in the next integer , Then assign the read result to the variable price, When you run the program, you enter 23,scanf This data will be added to price in , What we see here & character , Is to represent the joining .

next  int change =100-price;, Defines the second variable change, And the calculation is made. In this formula, there is a number that should be fixed 100, Then we can make it a constant and write it directly into the program

Define a constant :

const int AMOUNT=100

Then in the program, we can directly change the formula to

int change=AMOUNT-price;

This const It's a modifier , Add to int Used to add an invariant attribute to this variable , Once you add const The value of this attribute variable cannot be changed .

原网站

版权声明
本文为[the best b]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206262009306850.html