数据 Variables/Constants 和类型 int 常量 (C Primer Plus)
Data Variables/Constants and Type int Constants (C Primer Plus)
世界。我是CS领域的新手,正在通过一本名为'C Primer Plus'的书学习C语言。
我有一个关于 C 数据类型的问题(本书第 3 章)。
书上是这么写的
Data Variable and Constants
[...] Some types of data are preset before a program is used and keep their values unchanged throughout the life of the programs. These are constants. Other types of data may change or be assigned values as the program runs; these are variables. In the sample program, weight is a variable and 14.5833 is a constant. [...] The difference between a variable and a constant is that a variable can have its value assigned or changed while the program is running, and a constant can't.
下面是示例程序。
/* platinum.c -- your weight in platinum */
#include <stdio.h>
int main(void)
{
float weight;
float value;
printf("Are you worth your weight in platinum?\n");
printf("Let's check it out.\n);
printf("Please enter your weight in pounds: ");
scanf("%f", &weight);
value = 1700.0 * weight * 14.5833;
printf("Your weight in platinum is worth $%.2f.\n", value);
printf("You are easily worth that! If platinum prices drop,\n");
printf("eat more to maintain your value.\n);
return 0;
}
和下一个,
Data: Data-type Keywords
Beyond the distinction between variable and constant is the distinction between different types of data. [...] If a datum is a constant, the compiler can usually tell its type just by the way it looks. [...] A variable , however, needs to have its type announced in a declaration statement. [...]
在我阅读下面的内容之前,一切对我来说都是有意义的。
Initializing a Variable
To initilize a variable means to assign it a starting, or initial, value. [...] Here are some examples:
int hogs = 21;
int cows = 32, goats = 14
int dogs, cats = 94; /* valid, but poor, form */
下面是,
Type int Constants
The various integers (21, 32, 14 and 94) in the last example are integer constants, also called integer literals.
这让我感到困惑,因为我的理解是常量和变量是不同的。其中一个区别是是否声明其类型。但声明语句中初始化变量的值称为整数常量。
现在我的问题是
1.why数据常量和整数有区别吗constant/literal?
2.How它们有什么不同吗?
3.What我错过了吗?
感谢阅读。
why is there a difference between data constant and integer constant/literal?
没有。整型常量是类型的数据常量。这些声明语句 初始化 具有常量值的变量 - 给定
int hogs = 21;
变量 hogs
将包含值 21
- 我们已将常量的值复制到变量中。
这里还有一些例子:
double d = 1.234; // copy the value 1.234 into d
char c = 'a'; // copy the character value 'a' into c
char str[] = "foo"; // copy the contents of the string "foo" into the array str - size of the array is taken from the size of the initializer.
整数、浮点数和字符串常量可以有后缀,告诉编译器使用特定类型而不是假定 int
或 double
或其他。 1234U
表示 "treat 1234
as an unsigned integer",3.1415f
表示 "treat 3.1415
as a float
, not a double
",等等。不过您可能还不需要担心这一点。
那么,为什么键入 很重要 ?
不同的类型在内存中有不同的表示 - 整数值1234
的位模式看起来没有像位浮点值 1234.0
的模式,看起来与 字符串 "1234"
的位模式完全不同。如果我们尝试将字符串值存储在整数变量中并尝试将其用作整数,我们的程序将不会按预期运行。所以编译器有规则阻止我们将 incompatible 类型的值赋给变量。
让我们回到我们的变量初始化:
int hog = 21;
我们告诉编译器变量 hog
的类型为 int
,并且我们用常量表达式 初始化 它。为了让编译器接受这一点,常量表达式 also 需要具有类型 int
(或类型 compatible with int
).如果我们写
int hog = "21";
编译器会抱怨我们试图用错误类型的值初始化 hog
。
世界。我是CS领域的新手,正在通过一本名为'C Primer Plus'的书学习C语言。 我有一个关于 C 数据类型的问题(本书第 3 章)。
书上是这么写的
Data Variable and Constants
[...] Some types of data are preset before a program is used and keep their values unchanged throughout the life of the programs. These are constants. Other types of data may change or be assigned values as the program runs; these are variables. In the sample program, weight is a variable and 14.5833 is a constant. [...] The difference between a variable and a constant is that a variable can have its value assigned or changed while the program is running, and a constant can't.
下面是示例程序。
/* platinum.c -- your weight in platinum */
#include <stdio.h>
int main(void)
{
float weight;
float value;
printf("Are you worth your weight in platinum?\n");
printf("Let's check it out.\n);
printf("Please enter your weight in pounds: ");
scanf("%f", &weight);
value = 1700.0 * weight * 14.5833;
printf("Your weight in platinum is worth $%.2f.\n", value);
printf("You are easily worth that! If platinum prices drop,\n");
printf("eat more to maintain your value.\n);
return 0;
}
和下一个,
Data: Data-type Keywords
Beyond the distinction between variable and constant is the distinction between different types of data. [...] If a datum is a constant, the compiler can usually tell its type just by the way it looks. [...] A variable , however, needs to have its type announced in a declaration statement. [...]
在我阅读下面的内容之前,一切对我来说都是有意义的。
Initializing a Variable
To initilize a variable means to assign it a starting, or initial, value. [...] Here are some examples:
int hogs = 21;
int cows = 32, goats = 14
int dogs, cats = 94; /* valid, but poor, form */
下面是,
Type int Constants
The various integers (21, 32, 14 and 94) in the last example are integer constants, also called integer literals.
这让我感到困惑,因为我的理解是常量和变量是不同的。其中一个区别是是否声明其类型。但声明语句中初始化变量的值称为整数常量。
现在我的问题是
1.why数据常量和整数有区别吗constant/literal?
2.How它们有什么不同吗?
3.What我错过了吗?
感谢阅读。
why is there a difference between data constant and integer constant/literal?
没有。整型常量是类型的数据常量。这些声明语句 初始化 具有常量值的变量 - 给定
int hogs = 21;
变量 hogs
将包含值 21
- 我们已将常量的值复制到变量中。
这里还有一些例子:
double d = 1.234; // copy the value 1.234 into d
char c = 'a'; // copy the character value 'a' into c
char str[] = "foo"; // copy the contents of the string "foo" into the array str - size of the array is taken from the size of the initializer.
整数、浮点数和字符串常量可以有后缀,告诉编译器使用特定类型而不是假定 int
或 double
或其他。 1234U
表示 "treat 1234
as an unsigned integer",3.1415f
表示 "treat 3.1415
as a float
, not a double
",等等。不过您可能还不需要担心这一点。
那么,为什么键入 很重要 ?
不同的类型在内存中有不同的表示 - 整数值1234
的位模式看起来没有像位浮点值 1234.0
的模式,看起来与 字符串 "1234"
的位模式完全不同。如果我们尝试将字符串值存储在整数变量中并尝试将其用作整数,我们的程序将不会按预期运行。所以编译器有规则阻止我们将 incompatible 类型的值赋给变量。
让我们回到我们的变量初始化:
int hog = 21;
我们告诉编译器变量 hog
的类型为 int
,并且我们用常量表达式 初始化 它。为了让编译器接受这一点,常量表达式 also 需要具有类型 int
(或类型 compatible with int
).如果我们写
int hog = "21";
编译器会抱怨我们试图用错误类型的值初始化 hog
。