C++與C語言常用的語法對(duì)比
前言
本人在校學(xué)習(xí)的第一門語言是C++,由于操作系統(tǒng)這門課程實(shí)驗(yàn)的需要,要求在linux下使用GCC編譯器編譯C程序代碼,為了寫代碼的方便,本人先采用VS2017寫了C++版本的代碼,再根據(jù)C++和C語言兩個(gè)語法的不同將程序進(jìn)行修改成C程序。由于本人沒有學(xué)過C語言,對(duì)C語言的語法也不是很熟悉,寫本文的目的是記錄下修改過程的遇到的幾個(gè)注意點(diǎn),方面以后參考,
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>
注:以上兩種語言的頭文件沒有給出直接對(duì)應(yīng)的關(guān)系,在使用時(shí)若不知道需要哪些頭文件可以直接全部復(fù)制使用。
2.結(jié)構(gòu)體struct
C++ 說明: C++的struct成員包含成員函數(shù)
struct Hello{ void sayHello(char *name){ printf("你好,%s\n",name); } }; int main() { Hello hello; hello.sayHello(); exit(0); }
C 說明: C語言的struct的成員只能是數(shù)據(jù),不支持函數(shù),因此若要定義結(jié)構(gòu)體的函數(shù)需要使用函數(shù)指針實(shí)現(xiàn)
struct Hello{ void (*sayHello)(char* name); //函數(shù)名為指針類型 }; void sayHello(char* name){ printf("你好,%s\n",name); } int main(){ struct Hello hello; //聲明結(jié)構(gòu)體的變量,需要加struct hello.sayHello=sayHello; //C語言需要使用函數(shù)指針指向函數(shù)的聲明?。?! hello.sayHello("淺若清風(fēng)"); return 0; }
3.動(dòng)態(tài)數(shù)組的創(chuàng)建與刪除
以一維和二維動(dòng)態(tài)數(shù)組為例: C++ 創(chuàng)建: C++使用new
自動(dòng)為動(dòng)態(tài)數(shù)組分配空間 刪除: C++使用delete為動(dòng)態(tài)數(shù)組釋放內(nèi)存
void f() { int n; int m; int *Array1; //聲明一維動(dòng)態(tài)數(shù)組 Array1=new int(n); //為一維動(dòng)態(tài)數(shù)組分配空間,元素個(gè)數(shù)為n int **Array2; //聲明二維動(dòng)態(tài)數(shù)組 Array2=new int*[n]; //為二維動(dòng)態(tài)數(shù)組分配空間(n個(gè)指針空間),n行 for(int i;i<n;++i) { Array2[i]=new int[m]; //為每一行分配內(nèi)存空間(m個(gè)整數(shù)空間),m列 } //釋放內(nèi)存 delete []Array1; for(int i=0;i<n;++i){ delete[]Array2[i]; } delete[]Array2; }
C 創(chuàng)建: C使用calloc
為動(dòng)態(tài)數(shù)組分配空間 刪除: C使用free()為動(dòng)態(tài)數(shù)組釋放內(nèi)存
void f() { int n; int m; int *Array1; //聲明一維動(dòng)態(tài)數(shù)組 Array1=(int *)calloc(n,sizeof(int)); //為一維動(dòng)態(tài)數(shù)組分配空間,元素個(gè)數(shù)為n int **Array2; //聲明二維動(dòng)態(tài)數(shù)組 Array2=(int **)calloc(n,sizeof(int*)); //為二維動(dòng)態(tài)數(shù)組分配空間(n個(gè)指針空間),n行 for(int i;i<n;++i) { Array2[i]=(int*)calloc(m,sizeof(int)); //為每一行分配內(nèi)存空間(m個(gè)整數(shù)空間),m列 } //釋放內(nèi)存 free(Array1); for(int i=0;i<n;++i){ free(Array2[i]); } free(Array2); }
malloc函數(shù)與calloc函數(shù)的區(qū)別:
- malloc函數(shù):不能初始化所分配的內(nèi)存空間,在動(dòng)態(tài)分配完內(nèi)存后,里邊數(shù)據(jù)是隨機(jī)的垃圾數(shù)據(jù)。
- calloc函數(shù):能初始化所分配的內(nèi)存空間,在動(dòng)態(tài)分配完內(nèi)存后,自動(dòng)初始化該內(nèi)存空間為零。
4.函數(shù)順序問題
- C++中,寫在前面的函數(shù)可以直接調(diào)用寫在后面的函數(shù)
- C中,被調(diào)用的函數(shù)需要寫在調(diào)用函數(shù)之前
5.類(class)
類是C++引入的類型,可以包含數(shù)據(jù),也可以包含函數(shù),解決了C語言struct只能包含數(shù)據(jù)的缺陷。 另外一個(gè)不同點(diǎn)是struct是默認(rèn)public,而class默認(rèn)private
- 下面以一個(gè)鏈隊(duì)的例子,來體現(xiàn)C++的 class的用法,并與C語言用struct實(shí)現(xiàn)進(jìn)行對(duì)比
C++
#include<iostream> using namespace std; class QueueNode //隊(duì)列結(jié)點(diǎn) { 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; //儲(chǔ)存下一個(gè)結(jié)點(diǎn)的指針 }; class LinkQueue //隊(duì)列 { public: LinkQueue() { front = rear = new QueueNode(); count = 0; } void push(int m_page); //加到隊(duì)尾 int pop(); //取出隊(duì)首結(jié)點(diǎn)(此例是包含頭指針的鏈隊(duì),頭指針的下一個(gè)結(jié)點(diǎn)才是隊(duì)首結(jié)點(diǎn)),并返回該結(jié)點(diǎn)值 bool exist(int page); //判斷隊(duì)列中是否有結(jié)點(diǎn)的值為page void print(int page); //打印隊(duì)列 QueueNode* front; //隊(duì)首指針 QueueNode* rear; //隊(duì)尾指針 int count; //統(tǒng)計(jì)結(jié)點(diǎn)數(shù)量 }; 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("隊(duì)列為空!\n"); return 0; } int top = front->next->page; //記錄隊(duì)首,作為返回值 QueueNode* tmp = front->next; front->next = tmp->next; if (rear == tmp) //只剩下一個(gè)元素,此題不會(huì)出現(xiàn)這種情況 { rear = front; } delete tmp; count--; return top; } void LinkQueue::print() //打印隊(duì)列 { QueueNode* p = front; while (p->next != NULL) { printf("%d ", p->next->page); p = p->next; } printf("\n"); } bool LinkQueue::exist(int page) { //判斷隊(duì)列中是否存在元素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(); //獲得隊(duì)首元素值 printf("隊(duì)首元素的值為%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; //與新增結(jié)點(diǎn)鏈接 q->rear=node; //尾指針后移 q->count++; } int pop(struct LinkQueue*q) //隊(duì)首元素出隊(duì),并返回?cái)?shù)值 { 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) //判斷隊(duì)列是否有結(jié)點(diǎn)的值等于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("隊(duì)首元素的值為%d\n", top); }
到此這篇關(guān)于C++與C語言常用的語法對(duì)比的文章就介紹到這了,更多相關(guān)C++與C語言語法對(duì)比內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Cocos2dx實(shí)現(xiàn)數(shù)字跳動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Cocos2dx實(shí)現(xiàn)數(shù)字跳動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09C++?OpenGL實(shí)現(xiàn)旋轉(zhuǎn)立方體的繪制
這篇文章主要主要為大家詳細(xì)介紹了如何利用C++和OpenGL實(shí)現(xiàn)旋轉(zhuǎn)立方體的繪制,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起動(dòng)手嘗試一下2022-07-07C語言中結(jié)構(gòu)體與內(nèi)存對(duì)齊實(shí)例解析
C語言結(jié)構(gòu)體對(duì)齊也是老生常談的話題了,基本上是面試題的必考題,這篇文章主要給大家介紹了關(guān)于C語言中結(jié)構(gòu)體與內(nèi)存對(duì)齊的相關(guān)資料,需要的朋友可以參考下2021-07-07Qt+QWidget實(shí)現(xiàn)簡(jiǎn)約美觀的加載動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Qt如何結(jié)合QWidget實(shí)現(xiàn)簡(jiǎn)約美觀的加載動(dòng)畫,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02C++實(shí)現(xiàn)設(shè)計(jì)模式之裝飾者模式詳解
這篇文章主要介紹了C++設(shè)計(jì)模式之裝飾模式,裝飾模式能夠?qū)崿F(xiàn)動(dòng)態(tài)的為對(duì)象添加功能,是從一個(gè)對(duì)象外部來給對(duì)象添加功能,需要的朋友可以參考下2021-09-09C語言循環(huán)隊(duì)列與用隊(duì)列實(shí)現(xiàn)棧問題解析
循環(huán)隊(duì)列又叫環(huán)形隊(duì)列,是一種特殊的隊(duì)列。循環(huán)隊(duì)列解決了隊(duì)列出隊(duì)時(shí)需要將所有數(shù)據(jù)前移一位的問題,本篇帶你一起看看循環(huán)隊(duì)列的問題和怎樣用隊(duì)列實(shí)現(xiàn)棧2022-04-04C++中關(guān)于constexpr函數(shù)使用及說明
這篇文章主要介紹了C++中關(guān)于constexpr函數(shù)使用及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11