c语言 const常量

const is a keyword in C language, it is also known as type qualifier (which is to change the property of a variable). const is used to define a constant whose value may not be changed during the program execution. It prevents the accidental changes of the variable.

const是C语言中的关键字,也称为类型限定符 (用于更改变量的属性)。 const用于定义一个常量,其值在程序执行期间不得更改。 这样可以防止变量的意外更改。

Consider these two definitions,

考虑这两个定义,

int value1 = 10;
const int value2 = 20;

Here, value1 is an integer variable, the value of value1 can be changed any time during the run time. But, the value2 is an integer constant, and the value of value2 cannot be changed during the run time.

这里, value1是一个整数变量,在运行期间可以随时更改value1的值。 但是, value2是一个整数常量,并且在运行期间无法更改value2的值。

Here, value1 is an integer variable while value2 is an integer constant.

此处, value1是整数变量,而value2是整数常量。

定义一个常数 (Defining a constant)

The const keyword is used to define a constant, include the keyword const before the data type or after the data type, both are valid.

const关键字用于定义常量,在数据类型之前或之后的关键字const都有效。

const data_type constant_name = value;
or
data_type const constant_name = value;

Does a constant occupy memory?

常量会占用内存吗?

Yes, a constant always occupies memory at compile time. In the above statements, value2 will take sizeof(int) bytes (that maybe 2, 4, or 8 according to the system architecture) in the memory.

是的 ,一个常量总是在编译时占用内存。 在上面的语句中,value2将在内存中占用sizeof(int)个字节(根据系统体系结构可能是2、4或8个字节)。

C程序演示常量示例 (C program to demonstrate the example of constants)

#include <stdio.h>
int main()
{
const int a = 10; //integer constant
const float b = 12.3f; // float constant
const char c = 'X'; // character constant
const char str[] = "Hello, world!"; // string constant
// printing the values
printf("a = %d\n", a);
printf("b = %f\n", b);
printf("c = %c\n", c);
printf("str = %s\n", str);
return 0;
}

Output:

输出:

a = 10
b = 12.300000
c = X
str = Hello, world!

What does happen, if we try to change the value of a constant?

如果我们尝试更改常量的值,会发生什么?

If we try to change the value of a constant, the compiler produces an error that constant is read-only.

如果我们尝试更改常数的值,则编译器会产生一个错误,指出该常数是只读的。

Let's consider this example,

让我们考虑这个例子,

#include <stdio.h>
int main()
{
const int a = 10; //integer constant
// printing the value
printf("a = %d\n", a);
// changing the values
a = 20;
// again, printing the value
printf("a = %d\n", a);
return 0;
}

Output:

输出:

main.c: In function ‘main’:
main.c:11:7: error: assignment of read-only variable ‘a’
a = 20;
^

See the output – a is an integer constant here, and when we try to change it, the error is there.

看到输出- 一个是一个整型常量这里,当我们试图改变它,错误是存在的。

翻译自: https://www.includehelp.com/c/const-in-c-programming.aspx

c语言 const常量

c语言 const常量_C编程中的常量(const)相关推荐

  1. c语言编程字符串_C编程中的字符串

    c语言编程字符串 Strings in C programming are an array of characters with a NULL character ('\0') appended a ...

  2. 编程实现strcpy函数_C编程中的strcpy()

    编程实现strcpy函数 We earlier learned about strings in C programming in our Strings in C tutorial. Today, ...

  3. basic在c语言中是常量吗,QBASIC中的常量

    Basic语言是计算机高级语言的一种,它简单.易学.好用,被广大计算机用户所青睐.Basic语言的表达式与数学中的表达式相似,语句与自然语言相仿,极容易被初学者掌握,而Basic语言的最新发展Visu ...

  4. php中new运算符的作用,C++_C++编程中new运算符的使用学习教程,new运算符用作从自由存储为 typ - phpStudy...

    C++编程中new运算符的使用学习教程 new运算符用作从自由存储为 type-name 的对象或对象数组分配内存,并将已适当分类的非零指针返回到对象. [::] new [placement] ne ...

  5. c语言中值程序,编程c语言中,向上取整函数_C编程中的函数

    编程c语言中,向上取整函数 什么是功能? (What is a Function?) A Function is a block of statements that performs a speci ...

  6. python常量列表_Python中实现常量(Const)功能

    python语言本身没有提供const,但实际开发中经常会遇到需要使用const的情形,由于语言本身没有这种支出,因此需要使用一些技巧来实现这一功能 定义const类如下 复制代码 代码如下: imp ...

  7. mysql中无穷大如何表示_编程中无穷大常量的设置技巧

    如果问题中各数据的范围明确,那么无穷大的设定不是问题,在不明确的情况下,很多程序员都取0x7fffffff作为无穷大,因为这是32-bit int的最大值.如果这个无穷大只用于一般的比较(比如求最小值 ...

  8. 如何用c语言对51单片机进行编程,C语言在51单片机编程中的应用技巧

    随着单片机硬件性能的提高,编写应用程序更着重于程序本身的效率. Franklin或KEII.C51交叉编译器是专为51系列单片机设计的一种高效的C语言编译器,用其开发的应用程序易于维护,可移植性好,是 ...

  9. C/C++ 常量的定义与应用(编程中的常量)

    常量一般定义为全局变量,且大写: 1. 字符串常量 const string EXPAND_X = "X+YF"; const string EXPAND_Y = "FX ...

最新文章

  1. Java基础概念性的知识总结
  2. Java锁的种类以及辨析(二):自旋锁的其他种类
  3. 日志多租户架构下的Loki方案
  4. java 最小化 api_Java的API设计实践
  5. 在ListCtrl控件中插入图标
  6. mysql数据库表空间最大值_mysql 数据库取最大值
  7. [zz]WCF分布式开发步步为赢(0):WCF学习经验分享,如何更好地学习WCF?
  8. BigDecimal丢失精度的坑
  9. React开发(106):方法定义 不然弹出框报错
  10. GET和POST方式提交参数给web应用
  11. 故宫回应灯会票秒光:3500人约成功 没票别信黄牛
  12. 彻底删除linux mbr,如何删除mbr分区表(超详细介绍Linux管理分区技巧)
  13. 谷粒商城【商城系统】完整总结
  14. Microsoft Teams显示连接不上网
  15. CABasicAnimation,CAKeyframeAnimation,CATransition,CAAnimationGroup,UIBezierPath之间做动画的不同点和各自的使用范围。
  16. python pickle文件大小_Python Pandas to_pickle()压缩文件
  17. 创业之路 - 魏杰:下一个 10 年,将造就一批新富翁
  18. Altium Designer制作原理图库不显示Comment和Designator
  19. 【Spring系列04】自动装配(Qualifier,Autowired,Resource讲解)
  20. Windows 10(Office 2019)下安装mathtype 6.9/7.4以及相关冲突问题解决

热门文章

  1. 申请信息系统建设和能力评估CS1级对社保有要求吗?
  2. Java 实体转Json Json转实体 String转Json Sting转JSONObject Sting转JSONArray
  3. 区块链驱动供应链金融创新
  4. FastReport .NET 2023.2.7 FastReport Crack
  5. 使用Js将图片转换为base64格式-在线示例
  6. 异性相吸题目解决方法
  7. java onetomany_java – 如何使用@OneToMany集合进行分页
  8. 面对微信小程序的火爆,很多人却陷入了这8个误区
  9. Ubuntu下安装tacacs+服务器
  10. 1823196912