zoukankan      html  css  js  c++  java
  • 04 Storage and Calculation C语言中的存储和计算

    文章内容来源于Programming Hub的学习记录,本人整理添加了中文翻译,如有侵权,联系本人删除

    Variables C语言中的变量

    image.png

    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. Ok computer:
    2. Store the values 5 and 3
    3. Add 5 and 3
    4. Print the result
      When you run the program, the computer will store the values 5 and 3, perform addition operation on it and then print the result.

    步骤如下所示:
    1.准备好计算机。
    2.存储值5和3。
    3.加5和3。
    4.打印结果。
    当你运行程序时,计算机将存储数值5和3,对其执行加法运算,然后打印结果。

    image.png

    • 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。

    • 您可以在这些变量中存储不同类型的值,如数字、文本、带小数的数字等。

    image.png

    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?
    正如我们以前学到的,变量可以存储不同类型的值。
    但是,在执行操作之前,您认为计算机如何才能知道变量中存储了什么样的值呢


    Datatype C语言中的数据类型

    image.png
    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.

    答案是通过读取它的数据类型
    变量将保存的值的类型由其数据类型定义。
    因此,有不同的数据类型可用于存储不同类型的值,我们将在后面看到这一点。

    image.png

    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;
    

    image.png

    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:

      • int: used to store a whole number.Example: int a = 10;
      • char: used to store a single Character. Example: char a='J';Value is always enclosed in single quotes.
      • float: used to store a number with decimal places.Example: float a =10.78; Store Upto 7 Decimal Places.
      • double : used to store decimal values with higher precision.Example: double a = 10.12345678;Stores up to 15 decimal places.
    • 在‘C’语言中有不同的数据类型可用。一些常用的数据类型包括:

      • int:用于存储整数,例如:int a=10;
      • char:用于存储单个字符。示例:char a='J';值始终用单引号引起来。
      • Float:用于存储带小数位的数字,例如:Floata=10.78,最多存储7位小数
      • DOUBLE:用于存储精度较高的小数值,例如:DOUBLEa=10.12345678,最多存储15位小数

    Printing variables 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’中有不同类型的变量可用一样,每种类型的变量的打印方式都不同。
    让我们看看如何做到这一点:

    Printing an int variable 打印一个int类型的变量

    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 is the newline character)
    当您想要打印一个int类型的变量时,我们使用%d并给出变量名称。下面是代码(记住, 换行符)

    printf ("%d
    ",a);
    printf("Value of b is : %d", b);
    

    Output 输出:

    5
    Value of b is : 20

    Printing an char variable 打印一个char (单个)字符类型的变量

    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

    Make a Test 做个愉快的小测试吧

    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

    Printing a double variable 打印一个double类型的变量

    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

    打印double类型的变量时,程序出了个暂时不能理解的小bug

    image.png

    如上图,本人在x86_64-w64-mingw32环境中实际运行上述代码的结果为:20.815454,并非是20.815454342
    double应该是小数点后15位,不知道为什么出了这个问题?后面还要详细查考后更新博客

    Arithematic operations 算术运算

    image.png

    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。
    image.png

    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

    image.png

    • Below is a list of other operators available in 'C

      • '-': Subtracts the second operand from the first.
      • '*': Multiplies both operands.
      • '/': use for division operation.
      • '%': Modulus Operator and remainder of after an integer division.
    • 下面是‘C’中可用的其他运算符的列表。

      • ‘-’:从第一个操作对象中减去第二个操作对象。
      • '*':将两个操作对象相乘。
      • '/':用于除法运算。
      • '%':整数除法后的模运算符和余数。

    ++ && --

    image.png

    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.

    • 等一下,在我们结束这个主题之前,“C”中还有两个运算符。这些被称为递增运算符的‘++’和被称为递减运算符的‘--’。
      • ‘++’运算符将整数值增加1。
      • ‘--’整数值减1。
  • 相关阅读:
    ubuntu输入法安装
    ffmpeg使用
    sourceforge无法登陆?没关系~~
    六大代码问题检验你的JAVA知识(转)
    关于Struts处理异常框架的小例子
    Spring Security连接数据库查询实例
    关于Struts的Token
    JAVA md5、SHA加密类
    利用commons upload+ffmpeg+mencoder完成视频的上传与转换
    初始化SSD1963
  • 原文地址:https://www.cnblogs.com/xlfcjx/p/13393910.html
Copyright © 2011-2022 走看看