C++与C语言常用的语法对比

前言 本人在校学习的第一门语言是C++,由于操作系统这门课程实验的需要,要求在linux下使用GCC编译器编译C程序代码,为了写代码的方便,本人先采用VS201

前言

本人在校学习的第一门语言是C++,由于操作系统这门课程实验的需要,要求在linux下使用GCC编译器编译C程序代码,为了写代码的方便,本人先采用VS2017写了C++版本的代码,再根据C++和C语言两个语法的不同将程序进行修改成C程序。由于本人没有学过C语言,对C语言的语法也不是很熟悉,写本文的目的是记录下修改过程的遇到的几个注意点,方面以后参考,

1.头文件

c++

#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <random>

C

#include <time.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include <malloc.h>

注:以上两种语言的头文件没有给出直接对应的关系,在使用时若不知道需要哪些头文件可以直接全部复制使用。

2.结构体struct

C++ 说明: C++的struct成员包含成员函数

struct Hello{
	void sayHello(char *name){
		printf("你好,%s\n",name);
	}
};

int main()
{
	Hello hello;
	hello.sayHello();
	exit(0);
}

说明: C语言的struct的成员只能是数据,不支持函数,因此若要定义结构体的函数需要使用函数指针实现

struct Hello{
	void (*sayHello)(char* name);   //函数名为指针类型
};
void sayHello(char* name){
	printf("你好,%s\n",name);
}
int main(){
	struct Hello hello;  //声明结构体的变量,需要加struct
	hello.sayHello=sayHello;  //C语言需要使用函数指针指向函数的声明!!!
	hello.sayHello("浅若清风");
	return 0;
}

3.动态数组的创建与删除

以一维和二维动态数组为例: C++ 创建: C++使用new自动为动态数组分配空间 删除: C++使用delete为动态数组释放内存

void f()
{
	int n;
	int m;
	int *Array1;  //声明一维动态数组
	Array1=new int(n);  //为一维动态数组分配空间,元素个数为n
	int **Array2;  //声明二维动态数组
	Array2=new int*[n];  //为二维动态数组分配空间(n个指针空间),n行
	for(int i;i<n;++i)
	{
		Array2[i]=new int[m];  //为每一行分配内存空间(m个整数空间),m列
	}
	//释放内存
	delete []Array1;
	for(int i=0;i<n;++i){
		delete[]Array2[i];
	}
	delete[]Array2;
}

创建: C使用calloc为动态数组分配空间 删除: C使用free()为动态数组释放内存

void f()
{
	int n;
	int m;
	int *Array1;  //声明一维动态数组
	Array1=(int *)calloc(n,sizeof(int));  //为一维动态数组分配空间,元素个数为n
	int **Array2;  //声明二维动态数组
	Array2=(int **)calloc(n,sizeof(int*));  //为二维动态数组分配空间(n个指针空间),n行
	for(int i;i<n;++i)
	{
		Array2[i]=(int*)calloc(m,sizeof(int));  //为每一行分配内存空间(m个整数空间),m列
	}
	//释放内存
	free(Array1);
	for(int i=0;i<n;++i){
		free(Array2[i]);
	}
	free(Array2);
}

malloc函数与calloc函数的区别:

  • malloc函数:不能初始化所分配的内存空间,在动态分配完内存后,里边数据是随机的垃圾数据
  • calloc函数:能初始化所分配的内存空间,在动态分配完内存后,自动初始化该内存空间为零

4.函数顺序问题

  • C++中,写在前面的函数可以直接调用写在后面的函数
  • C中,被调用的函数需要写在调用函数之前

5.类(class)

类是C++引入的类型,可以包含数据,也可以包含函数,解决了C语言struct只能包含数据的缺陷。 另外一个不同点是struct是默认public,而class默认private

  • 下面以一个链队的例子,来体现C++的 class的用法,并与C语言用struct实现进行对比

C++

#include<iostream>
using namespace std;

class QueueNode  //队列结点
{
public:
	QueueNode() :page(-1), next(NULL) {};
	QueueNode(int m_page, QueueNode* m_next = NULL) :page(m_page), next(m_next) {};
	~QueueNode() { next = NULL; }
public:
	int page;
	QueueNode* next;  //储存下一个结点的指针
};

class LinkQueue  //队列
{
public:
	LinkQueue() { front = rear = new QueueNode(); count = 0; }
	void push(int m_page);  //加到队尾
	int pop();  //取出队首结点(此例是包含头指针的链队,头指针的下一个结点才是队首结点),并返回该结点值
	bool exist(int page);  //判断队列中是否有结点的值为page
	void print(int page);  //打印队列

	QueueNode* front;  //队首指针
	QueueNode* rear;  //队尾指针
	int count;  //统计结点数量
};

void LinkQueue::push(int m_page)
{
	QueueNode* p = new QueueNode(m_page);
	p->next = NULL;
	rear->next = p;
	rear = p;
	count++;
}

int LinkQueue::pop()
{
	if (front == rear) {
		printf("队列为空!\n");
		return 0;
	}
	int top = front->next->page;  //记录队首,作为返回值
	QueueNode* tmp = front->next;
	front->next = tmp->next;
	if (rear == tmp)  //只剩下一个元素,此题不会出现这种情况
	{
		rear = front;
	}
	delete tmp;
	count--;
	return top;
}

void LinkQueue::print()  //打印队列
{
	QueueNode* p = front;
	while (p->next != NULL)
	{
			printf("%d ", p->next->page);
			p = p->next;
	}
	printf("\n");
}

bool LinkQueue::exist(int page) {  //判断队列中是否存在元素page
	QueueNode*p = front;
	while (p->next != NULL) {
		if (p->next->page == page) {
			return true;
		}
		else {
			p = p->next;
		}
	}
	return false;
}

int main()
{
	LinkQueue queue;
	for(int i=0;i<5;++i)
	{
		queue.push(i);
	}
	queue.print();
	if(queue.exit(6)) printf("yes!\n");
	else printf("no!\n");
	int top=queue.pop();  //获得队首元素值
	printf("队首元素的值为%d\n",top);
}
# include<stdio.h>
# include<stdlib.h>
# include<stdbool.h>

struct QueueNode
{
	int page;
	struct QueueNode *next;
};

typedef struct LinkQueue {
	struct QueueNode* front;
	struct QueueNode* rear;
	int count;
};

void initQueue(struct LinkQueue *q)
{
	q->front = q->rear=(struct QueueNode *)malloc(sizeof(struct QueueNode));
	q->front->next = NULL;
	q->count=0;
}

void push(struct LinkQueue *q, int page)
{
	struct QueueNode* node = (struct QueueNode*)malloc(sizeof(struct QueueNode));
	node->next = NULL;
	node->page = page;
	q->rear->next = node;  //与新增结点链接
	q->rear=node;  //尾指针后移
	q->count++;
}

int pop(struct LinkQueue*q)  //队首元素出队,并返回数值
{
	int top = q->front->next->page;
	struct QueueNode *node = q->front->next;
	q->front->next = q->front->next->next;
	free(node);
	return top;
}

bool exist(struct LinkQueue*q, int page)  //判断队列是否有结点的值等于page
{
	struct QueueNode *node = q->front;
	while (node->next != NULL)
	{
		if (node->next->page == page) return true;
		else node = node->next;
	}
	return false;
}

void print(struct LinkQueue *q)
{	
	struct QueueNode *node =q->front;
	while (node->next != NULL)
	{
		printf("%d ", node->next->page);
		node = node->next;
	}
	printf("\n");
}

int main()
{
	struct LinkQueue queue;
	initQueue(&queue);
	for (int i = 1; i < 5; ++i)
	{
		push(&queue, i);
	}
	print(&queue);
	if (exist(&queue, 3)) printf("yes!\n");
	printf("======\n");
	if (exist(&queue, 6)) printf("yes!\n");
	else printf("no!\n");
	int top = pop(&queue);
	printf("队首元素的值为%d\n", top);
}

到此这篇关于C++与C语言常用的语法对比的文章就介绍到这了,更多相关C++与C语言语法对比内容请搜索好代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好代码网!