文字常数的目的是什么 [英] What is the purpose of literal constant
本文介绍了文字常数的目的是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以给出一个简单的例子。使用方法是什么。我可以在功能中使用它们吗?
我尝试了什么:
看一下文字的一些定义,但我找不到任何好用法
解决方案
使用它们很好编程样式并避免在必须更改值时编辑多个源代码行(这通常包括在忘记更换某个地方时发现错误)。
一个简单的例子:
int myArray [ 5 ] [< span class =code-digit> 5 ];
for ( int i = 0 ; i< 5 ; i ++)
{
for ( int j = 0 ; j< 5 ; j ++)
myArray [i] [j] ++;
}
// myArray稍后再次使用类似的循环
更好的解决方案:
// 大小可以在这里简单地更改
const int mySize = 5 ;
int myArray [mySize] [mySize];
for ( int i = 0 ; i< mySize; i ++)
{
for ( int j = 0 ; j< mySize; j ++)
myArray [i] [j] ++;
}
// 使用mySize后再次使用时,不关心myArray大小< /跨度>
Can someone give a simple example.what is the usage.how can i use them in function
What I have tried:
Look at some definition of literal but i couldnt find any good usage
解决方案
Using them is good programming style and avoids editing multiple source lines when a value has to be changed (which often includes finding the bug when forgetting the replacement somewhere).
A simple example:
int myArray[5][5]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) myArray[i][j]++; } // myArray used later again with similar loops
The better solution:
// size can be simply changed here const int mySize = 5; int myArray[mySize][mySize]; for (int i = 0; i < mySize; i++) { for (int j = 0; j < mySize; j++) myArray[i][j]++; } // Don't care about myArray size when used later again when using mySize
这篇关于文字常数的目的是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文