User Tools

Site Tools


cplusplus:c_versus_cplusplus

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_versus_cplusplus [2020/05/04 01:15] – [Structs] mithatcplusplus:c_versus_cplusplus [2020/05/04 01:40] – [Some C Resources] mithat
Line 55: Line 55:
  
 ===== Strings ===== ===== Strings =====
-  * Use null-terminated character arrays. 
-  * Use functions in ''[[https://pubs.opengroup.org/onlinepubs/009695399/basedefs/string.h.html|string.h]]'' to manipulate strings. 
-    * C's ''[[https://pubs.opengroup.org/onlinepubs/009695399/basedefs/string.h.html|string.h]]'' and C++'s ''[[http://www.cplusplus.com/reference/cstring/|cstring]]'' are essentially duplicates. 
 <code c> <code c>
-#include <string.h>+#include <string.h>       /* needed to use strlen() function */
  
 int main(void) { int main(void) {
Line 72: Line 69:
   ...   ...
 </code> </code>
 +  * Use null-terminated character arrays. 
 +  * Use functions in ''[[https://pubs.opengroup.org/onlinepubs/009695399/basedefs/string.h.html|string.h]]'' to operate on strings. 
 +    * C's ''[[https://pubs.opengroup.org/onlinepubs/009695399/basedefs/string.h.html|string.h]]'' and C++'s ''[[http://www.cplusplus.com/reference/cstring/|cstring]]'' are essentially duplicates.
 ===== Defining variables ===== ===== Defining variables =====
-  * In older versions of C, you //must// define variables at the beginning of a function. 
-  * Common practice today anyway. 
 <code c> <code c>
 int  main( ) int  main( )
Line 81: Line 78:
     int a,b,c;     int a,b,c;
     float x,y,z;     float x,y,z;
 +    
     ...     ...
 +}
 </code> </code>
-   +  * In older versions of C, you must define all variables at the beginning of a function. 
 +  * Common practice today anyway.
 ===== Prototypes ===== ===== Prototypes =====
-  * Function prototypes are not required. 
-  * Best practice to use them anyway. 
 <code c> <code c>
 /* int foo(); <--optional */ /* int foo(); <--optional */
Line 103: Line 100:
 </code> </code>
  
 +  * Function prototypes are not required. 
 +  * Best practice to use them anyway.
 ===== Named constants and macros ===== ===== Named constants and macros =====
-  * ''const'' is available only in newer versions of C.+  * ''const'' modifier is available only in newer versions of C.
   * ''#define'' preprocessor directive typically used instead.<code c>   * ''#define'' preprocessor directive typically used instead.<code c>
 #define PI 3.1415 #define PI 3.1415
Line 115: Line 113:
  
 ===== Casting ===== ===== Casting =====
-  * No ''static_cast<>'', etc. 
-  * Place the type you want a value cast to inside parenthesis before the value. 
 <code c> <code c>
 int myInt = 42; int myInt = 42;
Line 123: Line 119:
 myDouble = (double)myInt/3;  /* cast myInt to a double */ myDouble = (double)myInt/3;  /* cast myInt to a double */
 </code> </code>
 +  * Place the type you want a value cast to inside parenthesis before the value.
 +  * No ''static_cast<>'', etc.
  
 ===== Dynamic memory ===== ===== Dynamic memory =====
Line 138: Line 136:
  
 ===== Structs ===== ===== Structs =====
-  * Only member variables are allowed. No member functions. 
-  * Everything is public. No access specifiers. 
-  * You must use the ''struct'' keyword when defining ''struct'' variables. 
- 
 <code c> <code c>
 struct MyStruct struct MyStruct
Line 151: Line 145:
 struct MyStruct aStructInstance;  /* requires struct keyword */ struct MyStruct aStructInstance;  /* requires struct keyword */
 </code> </code>
 +  * Only member variables are allowed.
 +    * No member functions.
 +  * Everything is public.
 +    * No access specifiers.
 +  * You must use the ''struct'' keyword when defining ''struct'' variables.
 +
 +
  
 ===== Classes ===== ===== Classes =====
Line 156: Line 157:
 ===== Some C Resources ===== ===== Some C Resources =====
  
-  * [[https://www.math.brown.edu/~jhs/ReferenceCards/CRefCard.v2.2.pdf|C Reference Card (ANSI)]] +  * [[https://www.math.brown.edu/~jhs/ReferenceCards/CRefCard.v2.2.pdf|C Reference Card (ANSI)]] Highly recommended! 
-  * [[http://www.tutorialspoint.com/cprogramming/|C Tutorial]] from Tutorials Point+  * [[C examples|C code examples]]
  
 ===== C Examples ===== ===== C Examples =====
   * Look [[C examples|here]].   * Look [[C examples|here]].
cplusplus/c_versus_cplusplus.txt · Last modified: 2020/05/04 01:40 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki