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
cplusplus:c_vs._c [2016/04/21 23:15] – [Dynamic memory is different] mithatcplusplus: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. +
-    * ''cin'' +
-    * ''cout'' +
-    * ''string'' +
-    * ''class'' +
-    * etc. +
- +
-===== Input and output ===== +
-  * Input/output is generally more complicated in C. +
- +
-===== Console I/O ===== +
-  * ''printf()'' write a formatted string to the display  +
-  * ''scanf()'' read a formatted string from the keyboard +
-  * ''putchar()'' write a single character to the display +
-  * ''getchar()'' read a single character from the keyboard +
-  * ''puts()'' write strings to the display +
-  * ''gets()''  read a string from the keyboard +
- +
-===== File I/O ===== +
-  * ''fopen()'', ''fclose()'' open/close a text file. +
-  * ''feof()'' detect end-of-file marker in a file. +
-  * ''fscanf()'' read formatted string from a file. +
-  * ''fprintf()'' write formatted string to a file. +
-  * ''fgets()'' read a string from a file. +
-  * ''fputs()'' write a string to a file. +
-  * ''fgetc()'' read single character from a file. +
-  * ''fputc()'' write a single character to a file. +
-  +
-===== Different header files ===== +
-  * ''<stdio.h>'' input/output +
-  * ''<stdlib.h>'' standard utility functions +
-  * ''<string.h>'' string operations +
-  * ''<ctype.h>'' character class tests +
-  * ''<math.h>'' mathematical functions +
- +
-===== C does not have ===== +
-  * boolean type +
-  * reference variables +
-  * function overloading +
-  * ''%%//%% single line comments'' (in older versions of C). +
- +
-===== Defining variables ===== +
-  * You must define variables at the beginning of a function.<code c> +
-int  main( ) +
-+
-    int a,b,c; +
-    float x,y,z; +
-    ... +
-</code> +
- +
-===== Prototypes ===== +
-  * You don’t need function prototypes. <code c> +
-int main() +
-+
-    foo(); +
-    return 0; +
-+
- +
-int foo() +
-+
-    printf( "Hello world" ); +
-+
-</code> +
-  * Best practice to use them anyway. +
- +
-===== Named constants and macros ===== +
-  * ''const'' is available only in newer versions of C. +
-  * ''#define'' precompiler directive typically used instead for named constants.<code c> +
-#define PI 3.1415 +
-#define TAX_RATE 0.065 +
-</code> +
-  * ''#define'' can also be used to create macros.<code c> +
-#define square(x) ((x) * (x)) +
-</code> +
- +
-===== Dynamic memory is different ===== +
- +
-  * Allocating memory:<code c> +
-int *x = malloc(sizeof(int));          /* allocate a single int */ +
-int *x_arr = malloc(sizeof(int) * 10); /* allocate array of 10 ints */ +
-</code> +
- +
-  * Deallocating memory:<code c> +
-free( x ); +
-free( x_arr ); +
-</code> +
-===== Structs ===== +
-  * Need to use ''struct'' keyword when defining ''struct'' variables.<code c> +
-struct MyStruct +
-+
-    int x; +
-}; +
- +
-struct MyStruct aStructInstance; +
-</code> +
- +
-===== C Examples ===== +
-  * Look [[C examples|here]]. +
- +
-===== Some C Resources ===== +
- +
-  * [[https://www.math.brown.edu/~jhs/ReferenceCards/CRefCard.v2.2.pdf|C Reference Card (ANSI)]] +
-  * A [[http://www.tutorialspoint.com/cprogramming/|C Tutorial]] from Tutorials Point+
cplusplus/c_vs._c.1461280502.txt.gz · Last modified: 2016/04/21 23:15 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki