Comparing C with C++

Feature C C++
Comments /* comment text */ // comment text
Header file #include <stdio.h> #include <iostream.h>
Constants (for initializing arrays)
#define N_ELEMENTS	10
 . . .
int intArr[N_ELEMENTS];
const int N_ELEMENTS = 10;
 . . .
int intArr[N_ELEMENTS];
Typical main function prototype int main(void) int main()
Main function prototype with arguments int main(int argc, char * argv []) int main(int argc, char ** argv)
Variable declarations must be located at top of each function may be placed anywhere in a function
References
int * ref;
 . . .
*ref = 10;
(by pointer only)
int & ref;
 . . .
ref = 10;
(true reference)
Input/output streams stdin
stdout
stderr
(FILE *)
cin
cout
cerr
(iostream)
Displaying output to the screen printf()
puts()
putc()
putchar()
cout <<
Getting input from the keyboard scanf()
gets()
getc()
getchar()
cin <<
cin.getline()
cin.get()
File I/O #include <stdio.h>
FILE *
fopen()
fclose()
fprintf()
fscanf()
fgets()
fputs()
fgetc()
fputc()
fread()
fwrite()
fread()
fwrite()
feof()
#include <fstream.h>
fstream, ifstream, ofstream
fstream::open()
fstream::close()
fstream::operator<<()
fstream::operator>>()
fstream::getline()
fstream::get()
fstream::put()
fstream::read()
fstream::write()
fstream::eof()
Structure variables
struct somestruct {
	. . .
};
. . .
struct somestruct aSomeStruct;
struct somestruct {
	. . .
};
. . .
somestruct aSomeStruct;
Dynamic memory #include <stdlib.h>
malloc()
free()
struct somestruct * pSomeStruct;
char * p;
. . .
p = malloc(10 * sizeof(char));
. . .
pSomeStruct = malloc(sizeof(struct somestruct));
. . .
free(p);
free(pSomeStruct);

operator new()
operator delete()
somestruct * pSomeStruct;
char * p;
. . .
p = new char [10];
. . .
pSomeStruct = new somestruct;
. . .
delete [] p;
delete pSomeStruct;

Null pointer NULL
(#define'd in stdio.h)
0


Back to the CTEC1638 page