cplusplus:c_vs._c
This is an old revision of the document!
Table of Contents
C versus C++
Adapted from “Differences Between C and C++”
by Robert Niemann (Century College), 11/22/2015
Adapted by Mithat Konar
C is not object-oriented
- You can't create classes or objects in C.
- You can't use any of C++'s predefined classes and objects.
cin
cout
string
class
- etc.
Input and output
- Input/output is generally more complicated in C.
Console commands
printf()
write a formatted string to the displayscanf()
read a formatted string from the keyboardputchar()
write a single character to the displaygetchar()
read a single character from the keyboardputs()
write strings to the displaygets()
read a string from the keyboard
File commands
fopen()
open a text file.fclose()
close a text file.feof()
detect end-of-file marker in a file.fscanf()
read formatted string from a file.fprintf()
write formatted string to a file.fgets()
read a string from a file.fputs()
write a string to a file.fgetc()
read single character from a file.fputc()
write a single character to a file.
Different header files
<stdio.h>
input/output<stdlib.h>
standard utility functions<string.h>
string operations<ctype.h>
character class tests<math.h>
mathematical functions
C does not have
- boolean type
- reference variables
- function overloading
//
single line comments in older versions of C.
Defining variables
- You must define variables at the beginning of a function.
int main( ) { int a,b,c; float x,y,z; ...
Prototypes
- You don’t need function prototypes.
- Best practice to use them anyway.
int main() { foo(); return 0; } int foo() { printf( "Hello world" ); }
Named constants (macros)
const
is available only in newer versions of C.#define
precompiler directive typically used instead ofconst
.#define
can also be used to create macros.
#define PI 3.1415 #define TAX_RATE 0.065 #define square(x) ((x) * (x))
Dynamic memory
- Different way to allocate/deallocate dynamic memory:
int *x = malloc(sizeof(int)); /* allocate a single int */ int *x_array = malloc(sizeof(int) * 10); /* allocate array of 10 ints */ /* release storage that was allocated */ free( x ); free( x_array );
Structs
- Need to use
struct
keyword when definingstruct
variables
struct MyStruct { int x; }; struct MyStruct aStructInstance;
C Examples
- Look here.
Some C Resources
- A C Tutorial from Tutorials Point
cplusplus/c_vs._c.1461279647.txt.gz · Last modified: 2016/04/21 23:00 by mithat