cplusplus:c_vs._c
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cplusplus:c_vs._c [2016/04/21 23:16] – [Dynamic memory is different] mithat | cplusplus:c_vs._c [2016/04/22 01:10] (current) – mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ~~SLIDESHOW~~ | + | Moved to [[cplusplus:c_versus_cplusplus|C versus C++]]. |
- | + | ||
- | ====== 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. | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * etc. | + | |
- | + | ||
- | ===== Input and output ===== | + | |
- | * Input/ | + | |
- | + | ||
- | ===== Console I/O ===== | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | + | ||
- | ===== File I/O ===== | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | + | ||
- | ===== Different header files ===== | + | |
- | * ''< | + | |
- | * ''< | + | |
- | * ''< | + | |
- | * ''< | + | |
- | * ''< | + | |
- | + | ||
- | ===== C does not have ===== | + | |
- | * boolean type | + | |
- | * reference variables | + | |
- | * function overloading | + | |
- | * '' | + | |
- | + | ||
- | ===== 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. <code c> | + | |
- | int main() | + | |
- | { | + | |
- | foo(); | + | |
- | return 0; | + | |
- | } | + | |
- | + | ||
- | int foo() | + | |
- | { | + | |
- | printf( "Hello world" ); | + | |
- | } | + | |
- | </ | + | |
- | * Best practice to use them anyway. | + | |
- | + | ||
- | ===== Named constants and macros ===== | + | |
- | * '' | + | |
- | * ''# | + | |
- | #define PI 3.1415 | + | |
- | #define TAX_RATE 0.065 | + | |
- | </ | + | |
- | * ''# | + | |
- | #define square(x) ((x) * (x)) | + | |
- | </ | + | |
- | + | ||
- | ===== Dynamic memory is different ===== | + | |
- | + | ||
- | * Allocating memory:< | + | |
- | int *x = malloc(sizeof(int)); | + | |
- | int *x_arr = malloc(sizeof(int) * 10); /* allocate array of ints */ | + | |
- | </ | + | |
- | + | ||
- | * Deallocating memory:< | + | |
- | free(x); | + | |
- | free(x_arr); | + | |
- | </ | + | |
- | ===== Structs ===== | + | |
- | * Need to use '' | + | |
- | struct MyStruct | + | |
- | { | + | |
- | int x; | + | |
- | }; | + | |
- | + | ||
- | struct MyStruct aStructInstance; | + | |
- | </ | + | |
- | + | ||
- | ===== C Examples ===== | + | |
- | * Look [[C examples|here]]. | + | |
- | + | ||
- | ===== Some C Resources ===== | + | |
- | + | ||
- | * [[https:// | + | |
- | * A [[http:// | + |
cplusplus/c_vs._c.1461280576.txt.gz · Last modified: 2016/04/21 23:16 by mithat