C 变量、数据类型、常量
⚡ 智能摘要
Variables, data types, and constants form the foundation of every C program, where variables store changeable values, data types define the size and range of that data, and constants hold fixed values that never change.

什么是变量?
变量是用于存储某个值的标识符。常量在执行时永远不会改变。变量可以在程序执行期间发生变化并更新存储在其中的值。
一个变量可以在程序中的多个位置使用。变量名必须有意义。它应该代表变量的用途。
Example: Height, age, are the meaningful variables that represent the purpose it is being used for. Height variable can be used to store a height value. Age variable can be used to store the age of a person
在程序中使用变量之前,必须先声明变量。变量名由字符、数字和下划线组成。
以下是创建变量时必须遵循的规则:
- 变量名应该只由字符、数字和下划线组成。
- 变量名不应该以数字开头。
- 变量名不应包含空格。
- 变量名不应该由关键字组成。
- “C” 是一种区分大小写的语言,这意味着名为“age”和“AGE”的变量是不同的。
以下是“C”程序中有效变量名的示例:
height or HEIGHT
_height
_height1
My_name
以下是“C”程序中无效变量名的示例:
1height Hei$ght My name
For example, we declare an integer variable my_variable and assign it the value 48:
int my_variable;
my_variable = 48;
顺便说一下,我们可以在一条语句中声明和初始化(分配初始值)一个变量:
int my_variable = 48;
数据类型
“C” 提供了各种数据类型,使程序员可以轻松地根据应用程序的要求选择合适的数据类型。以下是三种数据类型:
- 原始数据类型
- 派生数据类型
- 用户定义的数据类型
有五种主要的基本数据类型,
- int 表示整数数据
- char 表示字符数据
- float 表示浮点数
- double 表示双精度浮点数
- 无效
Array, functions, pointers, structures are derived data types. ‘C’ language provides more extended versions of the above mentioned primary data types. Each data type differs from one another in size and range. Following table displays the size and range of each data type.
| 数据类型 | 大小(以字节为单位) | 范围 |
| 字符或有符号字符 | 1 | 128到127 |
| 无符号字符 | 1 | 0到255 |
| int 或有符号 int | 2 | 32768到32767 |
| 无符号整数 | 2 | 0到65535 |
| 短整型或无符号短整型 | 2 | 0到255 |
| 有符号短整型 | 2 | 128到127 |
| 长整型或有符号长整型 | 4 | 2147483648到2147483647 |
| 无符号长整型 | 4 | 0到4294967295 |
| 浮动 | 4 | 3.4E-38 至 3.4E+38 |
| 翻番 | 8 | 1.7E-308 至 1.7E+308 |
| 长双 | 10 | 3.4E-4932 至 1.1E+4932 |
Note: In C, there is no Boolean data type.
整数数据类型
整数就是一个整数。整数数据类型的范围因机器而异。整数数据类型的标准范围是 -32768 到 32767。
整数通常为 2 个字节,这意味着它在内存中总共占用 16 位。单个整数值占用 2 个字节的内存。整数数据类型进一步分为其他数据类型,例如短整型、整型和长整型。
尽管属于整数数据类型系列,但每种数据类型的范围都不同。整数系列的每个数据类型的大小可能不会改变。
短整型 (short int) 主要用于存储较小的数字,整型 (int) 用于存储平均大小的整数值,长整型 (long int) 用于存储较大的整数值。
Whenever we want to use an integer data type, we have to place int before the identifier such as,
int age;
这里,age 是一个整数数据类型的变量,可用于存储整数值。
浮点数据类型
与整数一样,在“C”程序中我们也可以使用浮点数据类型。“float”关键字用于表示浮点数据类型。它可以保存浮点值,这意味着数字有分数和小数部分。浮点值是包含小数点的实数。整数数据类型不存储小数部分,因此我们可以使用浮点数来存储值的小数部分。
通常,浮点数最多可以容纳 6 个精度值。如果浮点数不够,那么我们可以使用其他可以容纳大浮点值的数据类型。数据类型 double 和 long double 分别用于存储精度高达 14 位和 80 位的实数。
使用浮点数时,必须在标识符前放置关键字 float/double/long double。有效示例为:
float division; double BankBalance;
字符数据类型
字符数据类型用于存储用单引号括起来的单个字符值。
A character data type takes up to 1 byte of memory space.
例,
Char letter;
Void 数据类型
void 数据类型不包含或返回任何值。它主要用于在“C”中定义函数。
例,
void displayData()
变量的类型声明
int main() { int x, y; float salary = 13.48; char letter = 'K'; x = 25; y = 34; int z = x+y; printf("%d \n", z); printf("%f \n", salary); printf("%c \n", letter); return 0;}
输出:
59 13.480000 K
We can declare multiple variables with the same data type on a single line by separating them with a comma. Also, notice the use of format specifiers in printf output function float (%f) and char (%c) and int (%d).
常量
常量是程序执行过程中不会改变的固定值。以下是各种类型的常量:
整数常量
整数常量只不过是一个由数字或数值组成的值。这些值在程序执行期间永远不会改变。整数常量可以是八进制、十进制和十六进制。
十进制常数包含 0-9 的数字,例如,
Example, 111, 1234
以上是有效的十进制常数。
八进制常数包含0-7的数字,并且该类型的常数前面始终带有0。
Example, 012, 065
以上是有效的八进制常数。
十六进制常数包含 0-9 之间的数字以及 AF 之间的字符。十六进制常数始终以 0X 开头。
Example, 0X2, 0Xbcd
以上是有效的十六进制常量。
八进制和十六进制整数常数在使用“C”编程时很少使用。
字符常量
A character constant contains only a single character enclosed within a single quote (’). We can also represent character constant by providing ASCII value of it.
Example, 'A', '9'
以上是有效字符常量的示例。
字符串常量
字符串常量包含用双引号(“”)括起来的一系列字符。
Example, "Hello", "Programming"
这些是有效字符串常量的示例。
实常数
就像始终包含整数值的整数常量一样。 “C”还提供包含小数点或分数值的实常数。 实常数也称为浮点常数。 实常数包含一个小数点和一个小数值。
Example, 202.15, 300.00
这些是 ' 中的有效实常数C“。
实数常数也可以写成,
Mantissa e Exponent
例如,要声明一个不会改变的值(如经典的圆周常数 PI),有两种方法可以声明此常数
By using the const keyword in a variable declaration which will reserve a storage memory
#include <stdio.h> int main() { const double PI = 3.14; printf("%f", PI); //PI++; // This will generate an error as constants cannot be changed return 0;}
By using the #define pre-processor directive which doesn’t use memory for storage and without putting a semicolon character at the end of that statement
#include <stdio.h> #define PI 3.14 int main() { printf("%f", PI); return 0;}
