c语言变量类型和范围
C is a statically typed language.
C是一种静态类型的语言。
This means that any variable has an associated type, and this type is known at compilation time.
这意味着任何变量都具有关联的类型,并且该类型在编译时是已知的。
This is very different than how you work with variables in Python, JavaScript, PHP and other interpreted languages.
这与处理Python,JavaScript,PHP和其他解释语言中的变量的方式非常不同。
When you create a variable in C, you have to specify the type of a variable at the declaration.
在C语言中创建变量时,必须在声明中指定变量的类型。
In this example we initialize a variable age
with type int
:
在此示例中,我们使用int
类型初始化变量age
:
int age;
A variable name can contain any uppercase or lowercase letter, can contain digits and the underscore character, but it can’t start with a digit. AGE
and Age10
are valid variable names, 1age
is not.
变量名可以包含任何大写或小写字母,可以包含数字和下划线字符,但不能以数字开头。 AGE
和Age10
是有效的变量名,不是1age
。
You can also initialize a variable at declaration, specifying the initial value:
您还可以在声明时初始化变量,并指定初始值:
int age = 37;
Once you declare a variable, you are then able to use it in your program code, and you can change its value at any time, using the =
operator for example, like in age = 100;
, provided the new value is of the same type.
声明变量后,便可以在程序代码中使用它,并且可以随时使用=
运算符来更改其值,例如age = 100;
,前提是新值的类型相同。
In this case:
在这种情况下:
#include <stdio.h>
int main(void) {
int age = 0;
age = 37.2;
printf("%u", age);
}
the compiler will raise a warning at compile time, and will convert the decimal number to an integer value.
编译器将在编译时发出警告,并将十进制数转换为整数值。
The C built-in data types are int
, char
, short
, long
, float
, double
, long double
. Let’s find out more about those.
C内置数据类型为int
, char
, short
, long
, float
, double
, long double
。 让我们进一步了解这些。