c語言語言教程0基礎
Hey, Folks here I am back with my second article on C language. Hope you are through with my previous article C language - History, Popularity reasons, Characteristics, Basic structure etc. In this one, I will cover some fundamental concepts of C Language namely Variables, Tokens, Operators in C language.
嘿,伙計們,我回來了第二篇有關C語言的文章。 希望您能讀完我以前的C語言文章-歷史,流行原因,特征,基本結構等 。 在這一篇中,我將介紹C語言的一些基本概念,即C語言中的變量,標記,運算符 。
Let’s get started...
讓我們開始吧...
1)變量 (1) Variables)
They are temporary memory locations allocated during execution of the program. As the name suggests it is an entity whose value may change during program execution.
它們是在程序執行期間分配的臨時內存位置。 顧名思義,它是一個在程序執行期間其值可能會更改的實體。
Rules for variable name
變量名規則
It does not have a space between characters.
字符之間沒有空格。
Collection of alphabets, digits, and underscore (_).
字母,數字和下劃線(_)的集合。
The first character should be either alphabet or underscore (_).
第一個字符應為字母或下劃線(_)。
No other special symbol except underscore (_).
除下劃線(_)外,沒有其他特殊符號。
Keywords cannot be used as variable name.
關鍵字不能用作變量名。
Declaration of variable
變量聲明
Syntax:
句法:
datatype variable_name;
Example:
例:
int a;
It tells the user what is the data type of a declared variable or what type of values it can hold.
它告訴用戶聲明的變量的數據類型是什么或它可以保存的值的類型。
Initialization of a variable
初始化變量
The process of assigning any value to any variable.
將任何值分配給任何變量的過程。
Example:
例:
int a = 5;
2)代幣 (2) Token)
The basic and smallest unit of C program is token.
C程序的基本最小單位是令牌。
Token includes :
令牌包括:
Keywords
關鍵詞
Identifier
識別碼
Constants
常數
String Constant
字符串常量
Operators
經營者
Special Symbols e.g: _ , @ , *
特殊符號,例如:_,@,*
1) Keywords or Reserve words
1)關鍵字或保留字
These are those words whose meaning is already explained to the compiler.
這些是已經向編譯器解釋其含義的單詞。
They can’t be used as a variable name because if we do so that means we are defining the new meaning to the compiler which the compiler does not allow.
它們不能用作變量名,因為如果這樣做,則意味著我們正在為編譯器定義編譯器不允許的新含義。
2) Identifier
2)識別碼
They are the name given to programming elements such as array, function, variable. Same rules as of variable name.
它們是諸如數組,函數,變量之類的編程元素的名稱。 與變量名相同的規則。
3) Constant
3)常數
Entities whose value does not during the execution of a program.
其值在程序執行期間不存在的實體。
4) String constant
4)字符串常量
Collection of character enclosed by a double inverted comma. e.g.: "abc"
用雙反逗號括起來的字符的集合。 例如: “ abc”
5) Operators
5)運營商
They are used to perform arithmetic and logical operations by ALU.
它們由ALU用于執行算術和邏輯運算。
Example:
例:
a+b
Here, a and b are operands and + is an operator.
在這里, a和b是操作數,而+是運算符。
C language has very rich operators. Many different types of operators are available in C language for different mathematical computations.
C語言具有非常豐富的運算符。 C語言提供了許多不同類型的運算符,用于不同的數學計算。
They are mainly of three types:
它們主要分為三種類型:
Operators itself is a separate topic which needs to be covered in detail.
運營商本身是一個單獨的主題,需要詳細介紹。
So, here we get started.
所以,我們開始。
Unary operators
一元運算符
They require only one operand for execution, it includes :
它們只需要一個操作數即可執行,其中包括:
Unary minus
一元減
Bitwise compliment
按位贊美
Logical Not
邏輯不
Increment / Decrement
增量/減量
sizeof() operator
sizeof()運算符
sizeof() Operator
sizeof()運算符
It is used to return the size of an operand. It can be applied on variable, constant, datatype.
它用于返回操作數的大小。 它可以應用于變量,常量,數據類型。
Syntax:
句法:
sizeof(operand);
Example:
例:
int a=2, b;
b = sizeof(a);
//or
b = sizeof(int);
//or
b = sizeof(5);
printf("%d\n",b);
//All of the 3 three statements will give same output.
Ternary operators
三元運算符
They are also called conditional operator. For these operators, we require 3 operands for execution. There is only and one ternary operator in C language.
它們也稱為條件運算符。 對于這些運算符,我們需要3個操作數來執行。 用C語言只有一個三元運算符。
Syntax:
句法:
expression_1 ? expression_2 : expression_3 ;
If expression_1 is true expression_2 gets executed, if false then expression_3.
如果expression_1為true,則執行expression_2 ,如果為false,則執行expression_3 。
Example:
例:
int a=4 , b=7 , c;
c = ( a<7 ? a:b );
printf("%d\n", c );
Output
輸出量
4
Since, a = 4, exp_1 is true and therefore exp_2 gets executed. So, c = a gets executed.
由于a = 4 , exp_1為true,因此exp_2被執行。 因此, c = a被執行。
Binary operators
二元運算符
i) Arithmetic operators
i)算術運算符
Operator name | Operator |
Addition | + |
Subtraction | - |
Multiplication | * |
Division | / |
Modulus | % |
操作員姓名 | 操作員 |
加成 | + |
減法 | -- |
乘法 | * |
師 | / |
模量 | % |
Modulus operator gives remainder and division operator gives quotient. All arithmetic operators can be used for integer and float values except modulus which is used for integers only.
模運算符給出余數,除法運算符給出商。 除模數僅用于整數外,所有算術運算符均可用于整數和浮點值。
b) Relational operators
b)關系運算符
They are used for comparison between two values. They return result as true or false.
它們用于兩個值之間的比較。 它們返回結果為true或false。
Operator name | Operator |
Less than | < |
Less than or equal to | <= |
Greater than | > |
Greater than or equal to | >= |
Equal to | == |
Not equal to | != |
操作員姓名 | 操作員 |
少于 | < |
小于或等于 | <= |
比...更棒 | > |
大于或等于 | > = |
等于 | == |
不等于 | != |
c) Logical operators
c)邏輯運算符
Used to combine two relational expression and they returns result as true or false.
用于組合兩個關系表達式,它們返回結果為true或false。
A | B | A && B | A || B | !A |
---|---|---|---|---|
0 | 0 | 0 | 0 | 1 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 0 |
1 | 1 | 1 | 1 | 0 |
一個 | 乙 | A && B | A || 乙 | !一個 |
---|---|---|---|---|
0 | 0 | 0 | 0 | 1個 |
0 | 1個 | 0 | 1個 | 1個 |
1個 | 0 | 0 | 1個 | 0 |
1個 | 1個 | 1個 | 1個 | 0 |
d) Bitwise operators
d)按位運算符
They are used to perform operation on individual bits. They can be applied on char and int.
它們用于對單個位執行操作。 它們可以應用于char和int 。
Operators | Symbol name | Meaning |
---|---|---|
& | Ampersand | Bitwise AND |
| | Pipe | Bitwise OR |
^ | Caret | Bitwise X OR |
~ | Tilde | Bitwise compliment |
<< | Double less than | Left shift operator |
>> | Double greater than | Right shift operator |
經營者 | 符號名稱 | 含義 |
---|---|---|
和 | &符 | 按位與 |
| | 管 | 按位或 |
^ | 插入符號 | 按位X OR |
? | 蒂爾德 | 按位贊美 |
<< | 少于兩倍 | 左移運算符 |
>> | 大于 | 右移運算符 |
C Operator Precedence Table:
C運算符優先級表:
This page lists C operators in order of precedence (highest to lowest). Their associativity indicates in what order operators of equal precedence in an expression are applied.
本頁按優先順序(從高到低)列出C運算符。 它們的關聯性指示在表達式中應用相同優先級的運算符的順序。

翻譯自: https://www.includehelp.com/c/basics-of-c-language.aspx
c語言語言教程0基礎