卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16947本站已运行3323

在C语言中,常量类型限定符用于指定一个变量是一个常量,即其值在初始化后不能被修改。常量类型限定符可以通过将关键字const放在变量声明前来实现。例如: const int x = 5; 在上述示例中

Type qualifiers add special attributes to existing datatypes in C programming language.

在C语言中,常量类型限定符用于指定一个变量是一个常量,即其值在初始化后不能被修改。常量类型限定符可以通过将关键字const放在变量声明前来实现。例如:

const int x = 5;
在上述示例中,变量x被声明为一个常量,其值被初始化为5,并且不能在后续的代码中被修改。常量类型限定符的使用可以提高代码的可读性和可维护性,因为它明确地指示了变量的用途和限制

There are three type qualifiers in C language and constant type qualifier is explained below −

Const

There are three types of constants, which are as follows −

  • Literal constants

  • Defined constants

  • Memory constants

Literal constants − These are the unnamed constants that are used to specify data.

For example,

'
a=b+7 //Here ‘7’ is literal constant.

Defined constants − These constants use the preprocessor command ‘define" with #

For example, #define PI 3.1415

Memory constants − These constants use ‘C’ qualifier ‘const’, which indicates that the data cannot be changed.

The syntax is as follows −

'
const type identifier = value

例如,

const float pi = 3.1415

如您所见,它只是给出一个字面名称。

示例

以下是常量类型限定符的C程序:

'
#include<stdio.h>
#define PI 3.1415
main ( ){
   const float cpi = 3.14
   printf ("literal constant = %f",3.14);
   printf ("defined constant = %f", PI);
   printf ("memory constant = %f",cpi);
}

输出

当上述程序被执行时,它产生以下结果 −

'
literal constant = 3.14
defined constant = 3.1415
memory constant = 3.14

上一篇: C++程序以找出通过交换行和列可以生成的唯一矩阵的数量
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏