标签:保存 after 基本 eth let remember 定义变量 lang 通过
文章内容来源于Programming Hub的学习记录,本人整理添加了中文翻译,如有侵权,联系本人删除
Let‘s extend our mainfunction from the first topic. What if we want to print the sum of 5 and 3?
This will be our first logical program.
让我们从第一个主题扩展我们的主函数。如果我们想打印5和3的和?
这将是我们的第一个逻辑程序。
The steps will look something like this:
步骤如下所示:
1.准备好计算机。
2.存储值5和3。
3.加5和3。
4.打印结果。
当你运行程序时,计算机将存储数值5和3,对其执行加法运算,然后打印结果。
In ‘C‘ lanquage these storage boxes/areas are called Variables.
Our **program **can access and change the values of these storage boxes whenever required.
You can give any name to a variable except keywords which are already reserved in ‘C‘ language.
The number 5 will be stored in a variable named ‘a‘ and the number 3 we will be stored in a variable named ‘b‘. So we can write like a=5 and b=3.
You can store different types of values like numbers,text,numbers with decimals,etc.in these variables.
在‘C’语言中,这些存储箱/区域称为变量。
我们的程序可以根据需要随时访问和更改这些存储区域的值。
除‘C’语言中已保留的关键字外,您可以为变量指定任何名称。
数字5将存储在名为‘a’的变量中,数字3将存储在名为‘b’的变量中。所以我们可以写成a=5和b=3。
您可以在这些变量中存储不同类型的值,如数字、文本、带小数的数字等。
Like we learned before, variables can store differenttypes of values.
But how do you think the computer will come to know what kind of value is stored in a variable before performing an operation?
正如我们以前学到的,变量可以存储不同类型的值。
但是,在执行操作之前,您认为计算机如何才能知道变量中存储了什么样的值呢?
00)
The answer is by reading its datatype.
The kind of value a variable will hold is defined by its datatype.
So there are different datatypes available for storing different kind of values, which we will see further.
答案是通过读取它的数据类型。
变量将保存的值的类型由其数据类型定义。
因此,有不同的数据类型可用于存储不同类型的值,我们将在后面看到这一点。
Similar to our main program, the datatype for storing numbers is int.
So when we want to store our numbers a = 5 and b = 3 we will have to mention its datatype to the computer.
And our variables should be written with datatypes in the program as:
与我们的主程序类似,用于存储数字的数据类型是int。
因此,当我们想要存储数字a=5和b=3时,我们必须向计算机提及它的数据类型。
并且我们的变量应该在编写程序的过程中使用数据类型:
int a = 5;
int b = 3;
The previous two commands can be divided as,
前两个命令可以分为:
int a;
int b;
a = 5;
b = 3;
First two statements are called as ‘variable definition‘, where we define the variable name and its type.
Next two statements are called as ‘variable initialization‘, where we assign values to the variables.
You can do both steps at the same time:
前两个语句称为‘变量定义’,我们在其中定义变量名称及其类型。
接下来的两个语句称为“变量初始化”,我们为变量赋值。
您可以同时执行这两个步骤:
int a = 5;
int b = 3;
There are different datatypes available in ‘C‘ language. Some frequently used datatypes are:
在‘C’语言中有不同的数据类型可用。一些常用的数据类型包括:
Do you remember in the previous topic we learned how to print a simple text in ‘C‘ language? Now we will learn how to print a Variable.
As you know there are different types of variables available in ‘C‘, each type of variable is printed differently.
Let‘s see how to do it:
你还记得在上一个主题中,我们学习了如何用“C”语言打印一篇简单的文本吗?现在我们将学习如何打印变量。
正如您知道‘C’中有不同类型的变量可用一样,每种类型的变量的打印方式都不同。
让我们看看如何做到这一点:
Example,例子:
int a = 5;
int b = 20;
When you want to print an int variable, we use ‘%d‘ and give the variable name. Below is the code (remember that \n is the newline character)
当您想要打印一个int类型的变量时,我们使用%d并给出变量名称。下面是代码(记住,\n是换行符)
printf ("%d\n",a);
printf("Value of b is : %d", b);
Output 输出:
5
Value of b is : 20
Example,例子:
char varname = ‘A‘;
When you want to print a char variable, we use ‘%c‘ and give the variable name. Below is the code:
当您想要打印一个char类型的变量时,我们使用%c并给出变量名称。下面是代码:
printf("%c", varname );
Output 输出:
A
Complete the below command to print int and char value.
Fill in the blanks
完成以下命令以打印int和char值。
填空:
int rollNumber = 23;
char firstLetter = ‘M‘;
printf("my rollnumber is:__",rollNumber);
printf("First letter of my name is:__",firstLetter);
return 0;
int rollNumber = 23;
char firstLetter = ‘M‘;
printf("my rollnumber is:%d",rollNumber);
printf("First letter of my name is:%c",firstLetter);
return 0;
Output 输出:
my rollnumber is:23 First letter of my name is:M
Example 例子:
double marks = 20.815454342;
When you want to print a double variable, we have to use ‘%lf‘ and give the variable name. Below is the code
当您想要打印双变量时,我们必须使用%lf并给出变量名称。以下是代码:
printf ("%lf",marks);
Output 输出:
20.815454342
Let‘s go back to our previous example of adding two numbers 5 and 3. To add two numbers in maths we use + operator.
So the operation will be like 5+ 3.
让我们回到前面将两个数字5和3相加的例子。要将数学中的两个数字相加,我们使用+运算符。
因此,操作将类似于5+3。
Similarly in programming as well there are operators reserved to perform such kind of basic arithmetic operations.For adding two numbers ‘+‘ operator is used.
同样,在编程中也保留了运算符来执行这类基本的算术运算。对于将两个数相加,则使用‘+’运算符。
Let‘s take our previous example of adding two numbers 5 and 3, store it in two variables ‘a‘ and ‘b‘ and perform addition on it using +‘ operator.
The result which comes after addition will be stored in a new variable ‘c‘.‘
让我们以前面将两个数字5和3相加的例子为例,将其存储在两个变量‘a’和‘b’中,并使用+‘运算符对其执行加法。
加法后的结果将存储在新变量‘c’中。
#include <stdio.h>
int main() {
int a = 5;
int b = 3;
int c;
c = a+b;
printf("Result is %d", c);
}
Output 输出:
Result is 8
Below is a list of other operators available in ‘C
下面是‘C’中可用的其他运算符的列表。
Wait a minute before we finish this topic, there are two more operators in ‘C‘.
These are ‘++‘ called as increment operators and ‘--‘ called as decrement operator.
‘++‘ operator increases the integer value by one.
‘--‘ decreases the integer value by one.
04 Storage and Calculation C语言中的存储和计算
标签:保存 after 基本 eth let remember 定义变量 lang 通过
原文地址:https://www.cnblogs.com/xlfcjx/p/13393910.html