文章內(nèi)容來源于Programming Hub的學習記錄车柠,本人整理添加了中文翻譯颈嚼,如有侵權(quán)局骤,聯(lián)系本人刪除
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.
讓我們從第一個主題擴展我們的主函數(shù)呢堰。如果我們想打印5和3的和?
這將是我們的第一個邏輯程序屿聋。
The steps will look something like this:
- Ok computer:
- Store the values 5 and 3
- Add 5 and 3
- 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.打印結(jié)果。
當你運行程序時裙犹,計算機將存儲數(shù)值5和3,對其執(zhí)行加法運算衔憨,然后打印結(jié)果叶圃。
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’語言中,這些存儲箱/區(qū)域稱為變量践图。
我們的程序可以根據(jù)需要隨時訪問和更改這些存儲區(qū)域的值掺冠。
除‘C’語言中已保留的關(guān)鍵字外,您可以為變量指定任何名稱码党。
數(shù)字5將存儲在名為‘a(chǎn)’的變量中德崭,數(shù)字3將存儲在名為‘b’的變量中。所以我們可以寫成a=5和b=3揖盘。
您可以在這些變量中存儲不同類型的值眉厨,如數(shù)字、文本兽狭、帶小數(shù)的數(shù)字等憾股。
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?
正如我們以前學到的,變量可以存儲不同類型的值箕慧。
但是服球,在執(zhí)行操作之前,您認為計算機如何才能知道變量中存儲了什么樣的值呢颠焦?
Datatype C語言中的數(shù)據(jù)類型
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.
答案是通過讀取它的數(shù)據(jù)類型斩熊。
變量將保存的值的類型由其數(shù)據(jù)類型定義。
因此伐庭,有不同的數(shù)據(jù)類型可用于存儲不同類型的值粉渠,我們將在后面看到這一點。
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:
與我們的主程序類似似忧,用于存儲數(shù)字的數(shù)據(jù)類型是int渣叛。
因此,當我們想要存儲數(shù)字a=5和b=3時盯捌,我們必須向計算機提及它的數(shù)據(jù)類型淳衙。
并且我們的變量應該在編寫程序的過程中使用數(shù)據(jù)類型:
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:
前兩個語句稱為‘變量定義’,我們在其中定義變量名稱及其類型。
接下來的兩個語句稱為“變量初始化”箫攀,我們為變量賦值肠牲。
您可以同時執(zhí)行這兩個步驟:
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’語言中有不同的數(shù)據(jù)類型可用。一些常用的數(shù)據(jù)類型包括:
- int:用于存儲整數(shù)靴跛,例如:int a=10缀雳;
- char:用于存儲單個字符。示例:char a='J';值始終用單引號引起來梢睛。
- Float:用于存儲帶小數(shù)位的數(shù)字肥印,例如:Floata=10.78,最多存儲7位小數(shù)绝葡。
- DOUBLE:用于存儲精度較高的小數(shù)值深碱,例如:DOUBLEa=10.12345678,最多存儲15位小數(shù)藏畅。
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”語言打印一篇簡單的文本嗎?現(xiàn)在我們將學習如何打印變量愉阎。
正如您知道‘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環(huán)境中實際運行上述代碼的結(jié)果為:20.815454,并非是20.815454342
double應該是小數(shù)點后15位梁厉,不知道為什么出了這個問題?后面還要詳細查考后更新博客
Arithematic operations 算術(shù)運算
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.
讓我們回到前面將兩個數(shù)字5和3相加的例子。要將數(shù)學中的兩個數(shù)字相加踏兜,我們使用+運算符词顾。
因此,操作將類似于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.
同樣肉盹,在編程中也保留了運算符來執(zhí)行這類基本的算術(shù)運算。對于將兩個數(shù)相加疹尾,則使用‘+’運算符上忍。
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'.'
讓我們以前面將兩個數(shù)字5和3相加的例子為例骤肛,將其存儲在兩個變量‘a(chǎn)’和‘b’中,并使用+‘運算符對其執(zhí)行加法窍蓝。
加法后的結(jié)果將存儲在新變量‘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’中可用的其他運算符的列表。
- ‘-’:從第一個操作對象中減去第二個操作對象吓笙。
- '*':將兩個操作對象相乘淑玫。
- '/':用于除法運算。
- '%':整數(shù)除法后的模運算符和余數(shù)面睛。
++ && --
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.
- 等一下絮蒿,在我們結(jié)束這個主題之前,“C”中還有兩個運算符叁鉴。這些被稱為遞增運算符的‘++’和被稱為遞減運算符的‘--’歌径。
- ‘++’運算符將整數(shù)值增加1。
- ‘--’將整數(shù)值減1亲茅。