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:03] – [Console commands] mithat | cplusplus:c_vs._c [2016/04/21 23:15] – [Dynamic memory is different] mithat | ||
---|---|---|---|
Line 29: | Line 29: | ||
* '' | * '' | ||
- | ===== File commands | + | ===== File I/O ===== |
- | * '' | + | * '' |
- | * '' | + | |
* '' | * '' | ||
* '' | * '' | ||
Line 51: | 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 64: | 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 78: | 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