• 【BOOST C++】教程4:常量和宏


    目录

    一、提要

    二、常量和宏的区别

    三、关于 const char *p, char * const p and const char * const p的区别

    3.1. const char *ptr :

    3.2. char *const ptr :

    3.3. const char * const ptr : 

    四、 “char a” 和 “char a[1]”的不同?

    五、char datatype和 char array  长度


    一、提要

            C语言恐怕永远不会没落,本人曾经试图摆脱C语言的纠缠,最后发现,在现实中发现实际项目中,C忽隐忽现,从没有原理。既然这样,那就需要好好掌握。

           本章提要:常量和宏到底有啥不同,什么时候可以相互替代;本文即将告诉你,在BOOST C++如是说。

    二、常量和宏的区别

            #define 是一个预处理器指令。 #define 宏定义定义的数据经过预处理,以便您的整个代码可以使用它。这可以释放空间并增加编译时间。

            const 变量被视为变量,而不是宏定义。

            长话短说:CONST 由编译器处理,而#DEFINE 由预处理器处理。

            const 相对于#define 的最大优势是类型检查。 #defines 无法进行类型检查,因此在尝试确定数据类型时可能会出现问题。相反,如果变量是常量,那么我们可以获取存储在该常量变量中的数据类型。

            由于 const 被认为是变量,我们可以在它们上使用指针。这意味着除了更改数据本身之外,我们还可以对常规变量进行类型转换、移动地址以及您可以使用的所有其他操作,因为分配给该变量的数据是恒定的。

            一般来说,如果我们可以选择 const 并且它可以成功地应用于代码,那么 const 是一个更好的选择。在某些情况下,#define 不能被 const 替换。例如,#define 可以接受参数(例如,请参阅this)。 #define 还可用于将程序中的某些文本替换为另一个文本。

    让我们以表格形式查看差异 -:

    #defineconst
    1.#define is a preprocessor directive.Constants are used to make variables constant such that never change during execution once defined.
    2.is used to define micro substitutionConstants are also called literals.
    3.

    Its syntax is -:

    #define token value  

    Its syntax is -:

    const type constant_name;

    4.It should not be enclosed with a (;) semicolonIt should be enclosed with a (;) semicolon

            ​​​ 本文由 Abhay Rathi 供稿,Nolan Taft 编辑。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论

    三、关于 const char *p, char * const p and const char * const p的区别

            ​​​ 先决条件:指针 当 char、const、*、p 都以不同的排列方式使用时,会产生很多混淆,并且含义会根据放置的位置而变化。以下文章重点介绍所有这些的区分和使用。 限定符 const 可以应用于任何变量的声明,以指定其值不会改变。 const 关键字适用于紧邻其左侧的任何内容。如果它的左边没有任何东西,它适用于它右边的任何东西。

    3.1. const char *ptr :

            这是一个指向常量字符的指针.您无法更改 ptr 指向的值,但可以更改指针本身。 “const char *”是指向 const char 的(非 const)指针。

    • C

    // C program to illustrate

    // char const *p

    #include

    #include

    int main()

    {

        char a ='A', b ='B';

        const char *ptr = &a;

         

        //*ptr = b; illegal statement (assignment of read-only location *ptr)

         

        // ptr can be changed

        printf( "value pointed to by ptr: %c\n", *ptr);

        ptr = &b;

        printf( "value pointed to by ptr: %c\n", *ptr);

    }

    1. Output: 
    value pointed to by ptr:A
    value pointed to by ptr:B

            注意:const char *p 和 char const *p 之间没有区别,因为它们都是指向 const char 的指针,并且“*”(asterik)的位置也相同。

    3.2. char *const ptr :

             这是一个指向非常量字符的常量指针。您不能更改指针 p,但可以更改 ptr 指向的值。

    • C

    // C program to illustrate

    // char* const p

    #include

    #include

    int main()

    {

        char a ='A', b ='B';

        char *const ptr = &a;

        printf( "Value pointed to by ptr: %c\n", *ptr);

        printf( "Address ptr is pointing to: %d\n\n", ptr);

        //ptr = &b; illegal statement (assignment of read-only variable ptr)

        // changing the value at the address ptr is pointing to

        *ptr = b;

        printf( "Value pointed to by ptr: %c\n", *ptr);

        printf( "Address ptr is pointing to: %d\n", ptr);

    }

    Output: 

    Value pointed to by ptr: A
    Address ptr is pointing to: -1443150762
    
    Value pointed to by ptr: B
    Address ptr is pointing to: -1443150762

    NOTE: 指针总是指向同一个地址,只是该位置的值改变了。

     
    

    3.3. const char * const ptr : 

            这是一个指向常量字符的常量指针。您既不能更改 ptr 指向的值,也不能更改指针 ptr。

    // C program to illustrate

    //const char * const ptr

    #include

    #include

    int main()

    {

        char a ='A', b ='B';

        const char *const ptr = &a;

         

        printf( "Value pointed to by ptr: %c\n", *ptr);

        printf( "Address ptr is pointing to: %d\n\n", ptr);

        // ptr = &b; illegal statement (assignment of read-only variable ptr)

        // *ptr = b; illegal statement (assignment of read-only location *ptr)

    }

    Output: 

    Value pointed to by ptr: A
    Address ptr is pointing to: -255095482

    NOTE: char const * const ptr const char *const ptr相同
    Quiz on const keyword

            本文由 Yash Singla 提供。如果您喜欢 GeeksforGeeks 并愿意做出贡献,您还可以使用 write.geeksforgeeks.org 撰写文章或将您的文章邮寄至 review-team@geeksforgeeks.org。在 GeeksforGeeks 主页上查看您的文章并帮助其他 Geeks。
            如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

    四、 “char a” 和 “char a[1]”的不同?

    Question Source: Aricent Interview

            虽然这两个表达式都可以用来创建一个变量来存储一个字符,但它们有以下不同之处。

    1.  “char a”代表一个字符变量,“char a[1]”代表一个大小为1的char数组。
    2.  如果我们打印 char a 的值,我们得到字符的 ASCII 值(如果使用 %d)。如果我们打印 char a[1] 的值,我们会得到数组中唯一元素的地址。

    #include

      

    int main ()

    {

      char a1 = 'A';

      char a2[1] = {'A'};

      printf("%d  %d", a1, a2);

      return 0;

    }

    Output:

    65
    An address
    

            本文由 Abhishek 提供。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

    五、char datatype和 char array  长度

            给定一个 char 变量和一个 char 数组,任务是编写一个程序来在 C 中找到这个 char 变量和 char 数组的大小。

    Examples:

    Input: ch = 'G', arr[] = {'G', 'F', 'G'}
    Output: 
    Size of char datatype is: 1 byte
    Size of char array is: 3 byte
    
    Input: ch = 'G', arr[] = {'G', 'F'}
    Output: 
    Size of char datatype is: 1 byte
    Size of char array is: 2 byte
    

    Recommended: Please try your approach on {IDE} first, before moving on to the solution.

    IDE | GeeksforGeeks | A computer science portal for geeks

    Approach:

            在下面的程序中,要查找 char 变量和 char 数组的大小:

            首先,char 变量在 charType 中定义,char 数组在 arr 中定义。然后,使用 sizeof() 运算符计算 char 变量的大小。然后通过将完整数组的大小除以第一个变量的大小来找到 char 数组的大小。
    下面是查找 char 变量和 char 数组大小的 C 程序:

    // C program to find the size of

    // char data type and char array

      

    #include

      

    int main()

    {

      

        char charType = 'G';

        char arr[] = { 'G', 'F', 'G' };

      

        // Calculate and Print

        // the size of charType

        printf("Size of char datatype is: %ld byte\n",

               sizeof(charType));

      

        // Calculate the size of char array

        size_t size = sizeof(arr) / sizeof(arr[0]);

      

        // Print the size of char array

        printf("Size of char array is: %ld byte",

               size);

      

        return 0;

    }

    Output:

    Size of char datatype is: 1 byte
    Size of char array is: 3 byte
  • 相关阅读:
    记一次 .NET某设备监控自动化系统 CPU爆高分析
    redis集群之主从复制集群的原理和部署
    【AD9361 数字接口CMOS &LVDS&SPI】B 并行数据之CMOS 续
    单行函数,聚合函数课后练习
    WKWebView iOS17设置UserAgent
    力扣、752-打开转盘锁
    VR全景打造亮眼吸睛创意内容:三维模型、实景建模
    Lombok工具 : 常用注解介绍 (全)
    模电 01
    Spark AQE
  • 原文地址:https://blog.csdn.net/gongdiwudu/article/details/126831382