zoukankan      html  css  js  c++  java
  • C++常量

    常量是固定值,在程序执行期间不会改变。这些固定的值,又叫做字面量。常量可以是任何的基本数据类型,可分为整型数字、浮点数字、字符、字符串和布尔值。常量就像是常规的变量,只不过常量的值在定义后不能进行修改。

    在 C++ 中,有两种简单的定义常量的方式:

    • 使用 #define 预处理器。
    • 使用 const 关键字。

    下面是使用 #define 预处理器定义常量的形式:

    #define identifier value

    代码:

    #include <iostream>
    using namespace std;
     
    #define LENGTH 10   
    #define WIDTH  5
    #define NEWLINE '
    '
     
    int main()
    {
     
       int area;  
       
       area = LENGTH * WIDTH;
       cout << area;
       cout << NEWLINE;
       return 0;
    }

    可以使用 const 前缀声明指定类型的常量,如下所示:

    const type variable = value;

    代码:

    #include <iostream>
    using namespace std;
     
    int main()
    {
       const int  LENGTH = 10;
       const int  WIDTH  = 5;
       const char NEWLINE = '
    ';
       int area;  
       
       area = LENGTH * WIDTH;
       cout << area;
       cout << NEWLINE;
       return 0;
    }
  • 相关阅读:
    归并排序
    快速排序
    冒泡排序
    排序算法复杂度
    [LeetCode] 20. Valid Parentheses ☆(括号匹配问题)
    makefile编写helloworld
    shell的通俗理解
    PID三种参数的理解
    PID的原理
    PID控制温度
  • 原文地址:https://www.cnblogs.com/ConnorShip/p/10273969.html
Copyright © 2011-2022 走看看