1. 程式人生 > 實用技巧 >04 Storage and Calculation C語言中的儲存和計算

04 Storage and Calculation C語言中的儲存和計算

文章內容來源於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。