User Tools

Site Tools


cplusplus:c_vs._c

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
cplusplus:c_vs._c [2016/04/21 23:03] – [Console commands] mithatcplusplus:c_vs._c [2016/04/21 23:35] – [Structs] mithat
Line 29: Line 29:
   * ''gets()''  read a string from the keyboard   * ''gets()''  read a string from the keyboard
  
-===== File commands ===== +===== File I/O ===== 
-  * ''fopen()'' open a text file. +  * ''fopen()''''fclose()'' open/close a text file.
-  * ''fclose()'' close a text file.+
   * ''feof()'' detect end-of-file marker in a file.   * ''feof()'' detect end-of-file marker in a file.
   * ''fscanf()'' read formatted string from a file.   * ''fscanf()'' read formatted string from a file.
Line 51: Line 50:
   * reference variables   * reference variables
   * function overloading   * function overloading
-  * ''%%//%%'' single line comments in older versions of C.+  * ''%%//%% single line comments'' (in older versions of C).
  
 ===== 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>
-<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:
 } }
 </code> </code>
 +  * Best practice to use them anyway.
  
-===== Named constants (macros=====+===== Named constants and macros =====
   * ''const'' is available only in newer versions of C.   * ''const'' is available only in newer versions of C.
-  * ''#define'' precompiler directive typically used instead of ''const''. +  * ''#define'' precompiler directive typically used instead.<code c>
-  * ''#define'' can also be used to create macros. +
-<code c>+
 #define PI 3.1415 #define PI 3.1415
 #define TAX_RATE 0.065 #define TAX_RATE 0.065
 +</code>
 +  * ''#define'' can also be used to create macros.<code c>
 #define square(x) ((x) * (x)) #define square(x) ((x) * (x))
 </code> </code>
  
-===== Dynamic memory =====+===== Dynamic memory is different =====
  
-  * Different way to allocate/deallocate dynamic memory: +  * Allocating memory:<code c> 
-<code c> +int *x = malloc(sizeof(int));          /* allocate a single int */ 
-int *x = malloc(sizeof(int));            /* allocate a single int */ +int *xArry = malloc(sizeof(int) * 10); /* allocate array of ints */
-int *x_array = malloc(sizeof(int) * 10); /* allocate array of 10 ints */ +
- +
-/* release storage that was allocated */ +
-free( x ); +
-free( x_array );+
 </code> </code>
  
 +  * Deallocating memory:<code c>
 +free(x);
 +free(xArry);
 +</code>
 ===== Structs ===== ===== Structs =====
-  * Need to use ''struct'' keyword when defining ''struct'' variables +  * You must use the ''struct'' keyword when defining ''struct'' variables.<code c>
-<code c>+
 struct MyStruct struct MyStruct
 { {
cplusplus/c_vs._c.txt · Last modified: 2016/04/22 01:10 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki