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

Variables C语言中的变量

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,对其执行加法运算,然后打印结果。

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

正如我们以前学到的,变量可以存储不同类型的值。

但是,在执行操作之前,您认为计算机如何才能知道变量中存储了什么样的值呢


Datatype C语言中的数据类型



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:

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

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

如上图,本人在x86_64-w64-mingw32环境中实际运行上述代码的结果为:20.815454,并非是20.815454342

double应该是小数点后15位,不知道为什么出了这个问题?后面还要详细查考后更新博客

Arithematic operations 算术运算

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

    • '-': Subtracts the second operand from the first.
    • '*': Multiplies both operands.
    • '/': use for division operation.
    • '%': Modulus Operator and remainder of after an integer division.
  • 下面是‘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.

  • 等一下,在我们结束这个主题之前,“C”中还有两个运算符。这些被称为递增运算符的‘++’和被称为递减运算符的‘--’。

    • ‘++’运算符将整数值增加1。
    • ‘--’整数值减1。

04 Storage and Calculation C语言中的存储和计算的更多相关文章

  1. C语言中字符串存储方法

    众所周知,C语言中没有数据类型能够存储字符串, char数据类型仅仅能够存储一个字符的数据,那么在C语言中关于存储字符串这一难题我们改何去何从呢? 下面将详述相关的字符串存储方法; 1,使用字符数组存 ...

  2. 关于C语言中结构体大小计算

    结构体大小的计算,.网上说法一大堆还都不一样分什么对齐不对齐,偏移量什么的.. 在此稍微举例简单总结下: 对齐原则:每一成员的结束偏移量需对齐为后一成员类型的倍数  补齐原则:最终大小补齐为成员中最大 ...

  3. C语言中结构体大小计算

    1.普通结构体 struct student { char sex; char a; char b; int age; char name[100]; }; 该结构体大小为108 解答:1.先算str ...

  4. C语言中的除法的计算

    不用除号,计算除法运算.思路是使用减法运算!思路1:循环采用减法每次减去n,直到做完减法之后结果小于0为止 但是这样次数较大  如求100/3,需要次数为34次. 思路2:循环采用减法每次减去k,K的 ...

  5. 关于c语言中的字符串的问题

      静态数组,动态数组,链表是c语言中处理存储数据最基本的三种方式. 1.静态数组,你先定好大小,直接赋值即可,不要超过定义的长度. 2.动态分配数组,在执行的时候,输入要分的内存大小,然后p=(vo ...

  6. 【C语言学习】存储类型

    C语言中的存储类型主要有四种:auto.static.extern.register ★auto存储类型 默认的存储类型.在C语言中,假设忽略了变量的存储类型,那么编译器就会自己主动默认为auto型 ...

  7. C语言中结构体赋值问题的讨论

    今天帮师姐调一个程序的BUG,师姐的程序中有个结构体直接赋值的语句,在我印象中结构体好像是不能直接赋值的,正如数组不能直接赋值那样,我怀疑这个地方有问题,但最后证明并不是这个问题.那么就总结一下C语言 ...

  8. (待续)C#语言中的动态数组(ArrayList)模拟常用页面置换算法(FIFO、LRU、Optimal)

    目录 00 简介 01 算法概述 02 公用方法与变量解释 03 先进先出置换算法(FIFO) 04 最近最久未使用(LRU)算法 05 最佳置换算法(OPT) 00 简介 页面置换算法主要是记录内存 ...

  9. C语言中的宏定义

    目录(?)[-] 简单宏定义 带参数的宏 运算符 运算符 宏的通用属性 宏定义中圆括号 创建较长的宏 较长的宏中的逗号运算符 宏定义中的do-while循环do 空操作的定义 预定义宏 C语言中常用的 ...

随机推荐

  1. 构建docker私有仓库+k8s-pod应用

    环境版本系统:centos7.4docker-compose version 1.26.2docker-py version: 4.3.0CPython version: 2.7.5docker-ve ...

  2. AndroidStudio修改程序的包名,可以修改com.example.xxx之类的详解

    转载请说明出处.原创作品. 首先说明一下,当时公司需要修改androidStudio 项目的包名 于是上网查了一下,只看到了修改后面的包名,而不可以修改 前缀的com.example.xxx.所以很无 ...

  3. 入门的艰难——关于LR的使用

    这年头做一件事真是TM不容易啊.做测试也很纠结,不是都说商业工具很强大么,我去,这个不支持那个不支持的,这还有什么搞头,还非要按照你说的这个版本的才行,高一点的就crash了,结果连最初级的录制脚本都 ...

  4. Fragment的跳转

    1. 设置主Fragment 其它fragment得到它就可以了. 1 val ft = fragmentManager?.beginTransaction() 2 val maiFrgmt = Ma ...

  5. python简介以及简单代码——python学习笔记(一)

    学习来源:https://www.liaoxuefeng.com/wiki/1016959663602400 了解python 简单编写并实现python代码 命令行模式和python交互模式 了解p ...

  6. .NET实现可交互的WINDOWS服务(转载自CSDN"烈火蜓蜻")

    Windows 服务应用程序在不同于登录用户的交互区域的窗口区域中运行.窗口区域是包含剪贴板.一组全局原子和一组桌面对象的安全对象.由于 Windows 服务的区域不是交互区域,因此 Windows ...

  7. leetcode刷题-91解码方法

    题目 一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1'B' -> 2...'Z' -> 26给定一个只包含数字的非空字符串,请计算解码方法的总数. 示例 1: ...

  8. 零基础一分钟入门Python

    这篇文章面向所有想学python的小伙伴(甚至你从没听过编程),这篇文章将会带你以最快的速度入门python.赶快上车,时间来不及了... 一,下载和安装python 1.下载: 1.1 python ...

  9. Oracle数据库之表与表数据操作

    一.SQL语言 SQL语言分为四种,分别是:数据定义语言(DDL).数据操纵语言(DCL).事务控制语言(TCL).数据控制语言(DML). 1.1 数据定义语言(DDL) 建立.修改.删除数据库对象 ...

  10. WinDbg排查CPU高的问题

    一.概述 在Window服务器部署程序后,可能因为代码的不合理或者其他各种各样的问题,会导致CPU暴增,甚至达到100%等情况,严重危及到服务器的稳定以及系统稳定,但是一般来说对于已发布的程序,没法即 ...