关于C与C++的区别

笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:《手把手教你架构3D游戏引擎》电子工业出版社和《Unity3D实战核心技术详解》电子工业出版社等。

CSDN视频网址:http://edu.csdn.net/lecturer/144

项目开发中,经常会遇到C与C++之间互相调用问题,但是有时会遇到在C语言没啥问题,但是将其放到C++中就会出现问题,本篇博客在此给读者总结一下,在遇到下面这些情况时就要注意了。

一、在C++使用函数时,事先要先声明否则就会报错,但是在C语言就不存在这种问题,如下所示:

 

#include <stdio.h>
int main(void)
{
    printf("%d\n", fun());
    return 0;
}
 
int fun()
{
   return 10;
}

二、在C++中,使一个常量指针指向一个常量变量,编译时会发生报错,但它是在C中没有任何问题。

 

#include <stdio.h> 
int main(void)
{
    int i = 10;   
    int j = 20;
    const int *ptr = &i;    /* ptr is pointer to constant */
  
    printf("ptr: %d\n", *ptr); 
    *ptr = 100;        /* error: object pointed cannot be modified
                     using the pointer ptr */
  
    ptr = &j;          /* valid */
    printf("ptr: %d\n", *ptr);
  
    return 0;
}
 error: assignment of read-only location ‘*ptr’

再看另一个例子:

#include <stdio.h> 
 
int main(void)
{  
    int const i = 10;    /* i is stored in read only area*/
    int j = 20;
 
    int const *ptr = &i;        /* pointer to integer constant. Here i 
                                 is of type "const int", and &i is of 
                                 type "const int *".  And p is of type                            
                                "const int", types are matching no issue */
 
    printf("ptr: %d\n", *ptr); 
 
    *ptr = 100;        /* error */
 
    ptr = &j;          /* valid. We call it as up qualification. In 
                         C/C++, the type of "int *" is allowed to up 
                         qualify to the type "const int *". The type of 
                         &j is "int *" and is implicitly up qualified by 
                         the compiler to "cons tint *" */
 
    printf("ptr: %d\n", *ptr);
 
    return 0;
}

error: assignment of read-only location ‘*ptr’

为了加深读者印象再来一个:

#include <stdio.h>
  
int main(void)
{
   int i = 10;
   int j = 20;
   int *const ptr = &i;    /* constant pointer to integer */
  
   printf("ptr: %d\n", *ptr);
  
   *ptr = 100;    /* valid */
   printf("ptr: %d\n", *ptr);
  
   ptr = &j;        /* error */
   return 0;
}
 error: assignment of read-only variable ‘ptr’

最后一个例子:

 

#include <stdio.h>
  
int main(void)
{
    int i = 10;
    int j = 20;
    const int *const ptr = &i;        /* constant pointer to constant integer */
  
    printf("ptr: %d\n", *ptr);
  
    ptr = &j;            /* error */
    *ptr = 100;        /* error */
  
    return 0;
}
  error: assignment of read-only variable ‘ptr’
     error: assignment of read-only location ‘*ptr’

以上读者在编写代码时就要注意了。

三、在C中,一个void指针可以直接分配给其他一些指针,如int *,char *。 但是在C ++中,一个void指针必须被明确地指定类型。

 

int main()
{
   void *vptr;
   int *iptr = vptr; //In C++, it must be replaced with int *iptr=(int *)vptr; 
   return 0;
}

四、以下程序在C编译和运行良好,但在C ++编译失败。 C ++中的const变量必须被初始化,但是在c中是没有必要的。 

 

int main()
{
    const int a;   // LINE 4
    return 0;
}

五、我们可以使用一个C ++特定的关键字作为变量名。 该程序将不会在C ++中编译,但会在C中编译。

 

int main(void)
{
    int new = 5;  // new is a keyword in C++, but not in C
    printf("%d", new);
}

同样,我们可以使用其他关键字,如delete, explicit, class, .. 等。
六、C ++比C更严格的类型检查,例如,以下程序在C中编译,但不在C ++中编译。 在C ++中,我们得到编译器错误“从'int'到'char *'”的无效转换。 

 

#include <stdio.h>
int main()
{
    char *c = 333;
    printf("c = %u", c);
    return 0;
}

以上是在编程时经常遇到的,在此给读者列出来,供参考。。。。。。。。。

 

 

posted @ 2018-07-17 22:12  lightmare  阅读(219)  评论(0编辑  收藏  举报