Cprogramming 简明教程
C - Misc Operators
除了主要的操作符类别(算术、逻辑、赋值等),C 使用了以下同样重要的操作符。我们来讨论在此类别下分类的操作符。
Besides the main categories of operators (arithmetic, logical, assignment, etc.), C uses the following operators that are equally important. Let us discuss the operators classified under this category.
该符号“&”,已经在 C 中被定义为 Binary AND Operator ,如果它存在于两个运算对象中,则它会将位复制到结果中。该符号“&”也被定义为 address−of operator.
The "&" symbol, already defined in C as the Binary AND Operator copies a bit to the result if it exists in both operands. The "&" symbol is also defined as the address−of operator.
" symbol − A well−known arithmetic operator for multiplication, it can also be used as a *dereference operator.
The "" symbol − A well−known arithmetic operator for multiplication, it can also be used as a *dereference operator.
C 使用“>”符号,定义为 ternary operator, 用于计算条件表达式。
C uses the ">" symbol, defined as a ternary operator, used to evaluate a conditional expression.
在 C 中,点符号“.”用作与结构或联合类型有关的成员访问运算符。
In C, the dot "." symbol is used as the member access operator in connection with a struct or union type.
C 还使用箭头“→”符号作为间接寻址运算符,特别用于指向结构变量的指针。
C also uses the arrow "→" symbol as an indirection operator, used especially with the pointer to the struct variable.
Operator |
Description |
Example |
sizeof() |
Returns the size of a variable. |
sizeof(a), where a is integer, will return 4. |
& |
Returns the address of a variable. |
&a; returns the actual address of the variable. |
* |
Pointer to a variable. |
*a; |
?: |
Conditional Expression. |
If Condition is true ? then value X, else value Y |
. |
Member access operator |
var.member |
−> |
Access members of a struct variable with pointer |
ptr −> member; |
The sizeof Operator in C
sizeof 运算符是编译时一元运算符。它用于计算其操作数的大小,操作数可能为数据类型或变量。它以字节数返回大小。它可以应用于任何数据类型、浮点类型或指针类型变量。
The sizeof operator is a compile−time unary operator. It is used to compute the size of its operand, which may be a data type or a variable. It returns the size in number of bytes. It can be applied to any data type, float type, or pointer type variables.
sizeof(type or var);
当把 sizeof() 与数据类型一起使用时,它仅仅返回分配给该数据类型的内存量。输出在不同的机器上可能不同,比如 32 位系统可能显示不同输出,而 64 位系统可能显示相同数据类型的不同输出。
When sizeof() is used with the data types, it simply returns the amount of memory allocated to that data type. The output can be different on different machines like a 32−bit system can show different output while a 64−bit system can show different of the same data types.
Example
以下是 C 语言中的示例
Here is an example in C language
#include <stdio.h>
int main(){
int a = 16;
printf("Size of variable a : %d\n",sizeof(a));
printf("Size of int data type : %d\n",sizeof(int));
printf("Size of char data type : %d\n",sizeof(char));
printf("Size of float data type : %d\n",sizeof(float));
printf("Size of double data type : %d\n",sizeof(double));
return 0;
}
当你运行这段代码时,它将产生以下输出:
When you run this code, it will produce the following output −
Size of variable a: 4
Size of int data type: 4
Size of char data type: 1
Size of float data type: 4
Size of double data type: 8
Address-of Operator in C
“&”运算符返回现有变量的地址。我们可以将其分配给一个指针变量
The "&" operator returns the address of an existing variable. We can assign it to a pointer variable −
int a;
假设编译器在地址 1000 创建变量并创建 x 在地址 2000,那么 a 的地址存储在 x 中。
Assuming that the compiler creates the variable at the address 1000 and "x" at the address 2000, then the address of "a" is stored in "x".
Example
让我们通过一个示例来理解这一点。在这里,我们声明了一个 int 变量。然后,我们打印其值和地址
Let us understand this with the help of an example. Here, we have declared an int variable. Then, we print its value and address −
#include <stdio.h>
int main(){
int var = 100;
printf("var: %d address: %d", var, &var);
return 0;
}
运行代码并检查其输出:
Run the code and check its output −
var: 100 address: 931055924
The Dereference Operator in C
要声明一个指针变量,要使用以下语法
To declare a pointer variable, the following syntax is to be used −
type *var;
该变量的名称必须以星号 (*) 为前缀。数据类型表明它可以存储哪种数据类型的地址。例如
The name of the variable must be prefixed with an asterisk (*). The data type indicates it can store the address of which data type. For example −
int *x;
在这种情况下,变量 x 用于存储另一个 int 变量的地址。
In this case, the variable x is meant to store the address of another int variable.
float *y;
"y" 变量是一个指针,它存储一个 float 变量的内存位置。
The "y" variable is a pointer that stores the memory location of a float variable.
"&" 运算符返回一个现有变量的地址。我们可以把它分配给指针变量 −
The "&" operator returns the address of an existing variable. We can assign it to the pointer variable −
int a;
int *x = &a;
我们可以看到,这个变量的地址(对于任何类型的变量而言)是一个整数。所以,如果我们尝试把它存储在一个 int 类型指针变量中,看会发生什么 −
We can see that the address of this variable (any type of variable for that matter) is an integer. So, if we try to store it in a pointer variable of int type, see what happens −
float var1 = 10.55;
int *intptr = &var1;
编译器不接受这个,并报告以下错误 −
The compiler doesn’t accept this, and reports the following error −
initialization of 'int *' from incompatible pointer type 'float *' [-Wincompatible-pointer-types]
它表示变量的类型和它指针的类型必须相同。
It indicates that the type of a variable and the type of its pointer must be the same.
在 C 中,变量有特定的数据类型,数据类型定义了变量的大小和它们如何存储值。使用匹配的类型声明一个指针(例如, "float *" )强制指针和它指向的数据之间的类型兼容性。
In C, variables have specific data types that define their size and how they store values. Declaring a pointer with a matching type (e.g., "float *") enforces type compatibility between the pointer and the data it points to.
不同的数据类型在 C 中占用不同的内存空间。比如,一个 int 通常需要 4 个字节,而一个 float 可能需要 4 或 8 个字节,取决于系统。
Different data types occupy different amounts of memory in C. For example, an int typically takes 4 bytes, while a float might take 4 or 8 bytes depending on the system.
在指针中添加或减去整数根据这些指针所指向数据的大小,在内存中移动这些指针。
Adding or subtracting integers from pointers moves them in memory based on the size of the data they point to.
因此,我们声明 floatptr 变量为 float * 类型。
Hence, we declare the floatptr variable of float * type.
Example 1
请看以下示例:
Take a look at the following example −
#include <stdio.h>
int main(){
float var1 = 10.55;
float *floatptr = &var1;
printf("var1: %f \naddress of var1: %d \n\nfloatptr: %d \naddress of floatptr: %d", var1, &var1, floatptr, &floatptr);
return 0;
}
var1: 10.550000
address of var1: 6422044
floatptr: 6422044
address of floatptr: 6422032
Example 2
-
运算符被称为解引用运算符。它返回存储在指针中地址中的值,即它所指向的变量的值。看一看以下这个例子 −
The * operator is called the Dereference operator. It returns the value stored in the address which is stored in the pointer, i.e., the value of the variable it is pointing to. Take a look at the following example −
#include <stdio.h>
int main(){
float var1 = 10.55;
float *floatptr = &var1;
printf("var1: %f address of var1: %d\n",var1, &var1);
printf("floatptr: %d address of floatptr: %d\n", floatptr, &floatptr);
printf("var1: %f value at floatptr: %f", var1, *floatptr);
return 0;
}
运行此代码,您将获得以下输出−
On running this code, you will get the following output −
var1: 10.550000 address of var1: 6422044
floatptr: 6422044 address of floatptr: 6422032
var1: 10.550000 value at floatptr: 10.550000
The Ternary Operator in C
在 C 语言中,?字符用作三元运算符。它也被称为 conditional operator.
In C language, the "?" character is used as the ternary operator. It is also known as a conditional operator.
“三元”一词表示运算符有三个操作数。三元运算符经常用于以紧凑的方式表达条件(if-else)语句。
The term "ternary" implies that the operator has three operands. The ternary operator is often used to put conditional (if−else) statements in a compact way.
?运算符与以下 syntax 搭配使用 −
The ? operator is used with the following syntax −
exp1 ? exp2 : exp3
它具有以下 three operands −
It has the following three operands −
-
exp1 − a Boolean expression that evaluates to True or False
-
exp2 − returned by the ? operator when exp1 is true
-
exp3 − returned by the ? operator when exp1 is false
Example
以下 C 程序使用 ? 运算符来检查 a 的值是偶数还是奇数。
The following C program uses the ? operator to check if the value of a is even or odd.
#include <stdio.h>
int main(){
int a = 10;
(a % 2==0) ? printf("%d is Even\n", a) : printf("%d is Odd\n", a);
return 0;
}
10 is Even
将 “a” 的值改为 15,然后再次运行代码。现在你将获得以下输出 −
Change the value of "a" to 15 and run the code again. Now you will get the following output −
15 is Odd
条件运算符是对if−else构造的紧凑表示法。
The conditional operator is a compact representation of if − else construct.
The Dot (.) Operator in C
在C语言中,可以通过struct和union关键字定义一个派生数据类型。将不同类型成员元素分组在一起的派生或用户自定义数据类型。
In C language, you can define a derived data type with struct and union keywords. A derived or user−defined data type that groups together member elements of different types.
与struct或union变量一起使用时,点操作符是一个 member selection operator, 。点(.)运算符具有 highest operator precedence in C 语言,其结合性是从左到右。
The dot operator is a member selection operator, when used with the struct or union variable. The dot (.) operator has the highest operator precedence in C Language and its associativity is from left to right.
看看它的 syntax −
Take a look at its syntax −
var.member;
此处,var是某种struct或union类型的一个变量,member是在创建结构或联合时定义的一个元素。
Here, var is a variable of a certain struct or a union type, and member is one of the elements defined while creating structure or union.
使用 struct 关键字定义了新的派生数据类型,语法如下:
A new derived data type is defined with struct keyword as following syntax −
struct newtype {
type elem1;
type elem2;
type elem3;
. . .
. . .
};
然后,你可以声明这种派生数据类型的变量如下 −
You can then declare a variable of this derived data type as −
struct newtype var;
访问某个成员,
To access a certain member,
var.elem1;
Example
让我们声明一个名为book的struct类型,声明一个struct变量。以下示例显示了使用“.”运算符来访问book结构中的成员。
Let us declare a struct type named book, declare a struct variable. The following example shows the use of "." operator to access the members in the book structure.
#include <stdio.h>
struct book{
char title[10];
double price;
int pages;
};
int main(){
struct book b1 = {"Learn C", 675.50, 325};
printf("Title: %s\n", b1.title);
printf("Price: %lf\n", b1.price);
printf("No of Pages: %d\n", b1.pages);
printf("size of book struct: %d", sizeof(struct book));
return 0;
}
运行此代码,您将获得以下输出−
On running this code, you will get the following output −
Title: Learn C
Price: 675.500000
No of Pages: 325
size of book struct: 32
The Indirection Operator in C
结构是C语言中的一种派生数据类型。在C语言中,struct关键字已提供用于定义自定义数据类型。
A structure is a derived data type in C. In C, the struct keyword has been provided to define a custom data type.
使用 struct 关键字定义新的派生数据类型,如下 syntax −
A new derived data type is defined with a struct keyword as the following syntax −
struct type {
type var1;
type var2;
type var3;
. . .
. . .
};
然后,你可以声明这种派生数据类型的变量如下 −
You can then declare a variable of this derived data type as −
struct type = var;
通常情况下,结构在程序中定义第一个函数之前,在include语句之后声明。这样,可以在任何函数内声明其变量,以用于派生类型。
Usually, a struct is declared before the first function is defined in the program, after the include statements. That way, the derived type can be used for declaring its variable inside any function.
让我们声明一个名为book的struct类型,如下所示:−
Let us declare a struct type named book as follows −
struct book {
char title[10];
double price;
int pages;
};
若要声明此类型的变量,请使用以下语法:−
To declare a variable of this type, use the following syntax −
struct book b1;
通过将每个元素的值放在大括号内来完成struct变量的初始化。
The initialization of a struct variable is done by placing value of each element inside curly brackets.
struct book b1 = {"Learn C", 675.50, 325};
您还可以将struct变量的地址存储在struct指针变量中。
You can also store the address of a struct variable in the struct pointer variable.
struct book *strptr;
若要存储地址,请使用“&”运算符。
To store the address, use the "&" operator.
strptr = &b1;
C定义了箭头(→)符号与struct指针一起用作间接操作符(也称为struct取消引用操作符)。它有助于访问指针引用到的struct变量的元素。
C defines the arrow (→) symbol to be used with struct pointer as indirection operator (also called struct dereference operator). It helps to access the elements of the struct variable to which the pointer reference to.
Example
在此示例中,strptr是struct book b1变量的指针。因此,strrptr−>title返回标题,类似于b1.title所做的操作。
In this example, strptr is a pointer to struct book b1 variable. Hence, strrptr−>title returns the title, similar to b1.title does.
#include <stdio.h>
#include <string.h>
struct book {
char title[10];
double price;
int pages;
};
int main() {
struct book b1 = {"Learn C", 675.50, 325};
struct book *strptr;
strptr = &b1;
printf("Title: %s\n", strptr->title);
printf("Price: %lf\n", strptr->price);
printf("No of Pages: %d\n", strptr->pages);
return 0;
}
运行代码并检查其输出:
Run the code and check its output −
Title: Learn C
Price: 675.500000
No of Pages: 325