cplusplus:c_vs._c
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
cplusplus:c_vs._c [2016/04/21 23:00] – [C versus C++] mithat | cplusplus:c_vs._c [2016/04/21 23:15] – [Dynamic memory is different] mithat | ||
---|---|---|---|
Line 2: | Line 2: | ||
====== C versus C++ ====== | ====== C versus C++ ====== | ||
- | Adapted from "Differences Between C and C++" | + | Adapted from \\ |
- | by Robert Niemann (Century College), 11/ | + | **Differences Between C and C++** \\ |
+ | by Robert Niemann (Century College) | ||
+ | 11/ | ||
Adapted by Mithat Konar | Adapted by Mithat Konar | ||
Line 19: | Line 21: | ||
* Input/ | * Input/ | ||
- | ===== Console | + | ===== Console |
* '' | * '' | ||
* '' | * '' | ||
Line 27: | Line 29: | ||
* '' | * '' | ||
- | ===== File commands | + | ===== File I/O ===== |
- | * '' | + | * '' |
- | * '' | + | |
* '' | * '' | ||
* '' | * '' | ||
Line 49: | Line 50: | ||
* reference variables | * reference variables | ||
* function overloading | * function overloading | ||
- | * '' | + | * '' |
===== Defining variables ===== | ===== Defining variables ===== | ||
- | * You must define variables at the beginning of a function. | + | * You must define variables at the beginning of a function.< |
- | <code c> | + | |
int main( ) | int main( ) | ||
{ | { | ||
Line 62: | Line 62: | ||
===== Prototypes ===== | ===== Prototypes ===== | ||
- | * You don’t need function prototypes. | + | * You don’t need function prototypes. <code c> |
- | * Best practice to use them anyway. | + | |
- | <code c> | + | |
int main() | int main() | ||
{ | { | ||
Line 76: | Line 74: | ||
} | } | ||
</ | </ | ||
+ | * Best practice to use them anyway. | ||
- | ===== Named constants | + | ===== Named constants |
* '' | * '' | ||
- | * ''# | + | * ''# |
- | * ''# | + | |
- | <code c> | + | |
#define PI 3.1415 | #define PI 3.1415 | ||
#define TAX_RATE 0.065 | #define TAX_RATE 0.065 | ||
+ | </ | ||
+ | * ''# | ||
#define square(x) ((x) * (x)) | #define square(x) ((x) * (x)) | ||
</ | </ | ||
- | ===== Dynamic memory ===== | + | ===== Dynamic memory |
- | * Different way to allocate/ | + | * Allocating |
- | <code c> | + | int *x = malloc(sizeof(int)); |
- | int *x = malloc(sizeof(int)); | + | int *x_arr = malloc(sizeof(int) * 10); /* allocate array of 10 ints */ |
- | int *x_array | + | |
- | + | ||
- | /* release storage that was allocated */ | + | |
- | free( x ); | + | |
- | free( x_array ); | + | |
</ | </ | ||
+ | * Deallocating memory:< | ||
+ | free(x); | ||
+ | free(x_arr); | ||
+ | </ | ||
===== Structs ===== | ===== Structs ===== | ||
- | * Need to use '' | + | * Need to use '' |
- | <code c> | + | |
struct MyStruct | struct MyStruct | ||
{ | { |
cplusplus/c_vs._c.txt · Last modified: 2016/04/22 01:10 by mithat